@blueking/bk-weweb 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.min.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});let e=null;function t(){return e}function n(t){e=t}const o=new Map,s=new Map,r="noModule"in document.createElement("script"),i=document.createElement("style");i.setAttribute("id","SCOPED_CSS_STYLE_ID"),document.body.appendChild(i),i.sheet.disabled=!0;const c=i;var a;function l(e){return"function"==typeof e}!function(e){e.UNSET="UNSET",e.LOADING="LOADING",e.LOADED="LOADED",e.ERROR="ERROR",e.MOUNTING="MOUNTING",e.MOUNTED="MOUNTED",e.ACTIVATED="ACTIVATED",e.DEACTIVATED="DEACTIVATED",e.UNMOUNT="UNMOUNT"}(a||(a={}));const h="function"==typeof window.Zone?setTimeout:e=>Promise.resolve().then(e);function p(e){return e.startsWith("//")?`${location.protocol}${e}`:e}function u(e,t){return!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e)?e:new URL(e,function(e){const{origin:t,pathname:n}=new URL(e);if(/\.(\w+)$/.test(n)){const e=`${t}${n}`.split("/");return e.pop(),`${e.join("/")}/`}return`${t}${n}/`.replace(/\/\/$/,"/")}(p(t))).toString()}const d="undefined"!=typeof navigator&&-1!==navigator.userAgent.indexOf("Trident");function m(){return`inline-${E(16)}`}const f=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout((()=>{e({didTimeout:!1,handle:()=>Math.max(0,50-(Date.now()-t))})}),1)};function y(e,t){const n=document.createElement(e,t);return n.__BK_WEWEB_APP_KEY__&&delete n.__BK_WEWEB_APP_KEY__,n}function w(e){return/^(body|head|html)$/i.test(e)}const E=(e,t="abcdefghijklmnopqrstuvwxyz0123456789")=>{let n="";for(let o=0;o<e;o++)n+=t[parseInt((Math.random()*t.length).toString(),10)];return n};function A(e,t={}){return Te.fetchSource?Te.fetchSource(e,t):window.fetch(e,t).then((e=>e.text()))}var _;!function(e){e[e.STYLE_RULE=1]="STYLE_RULE",e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(_||(_={}));const S=["System","__cjsWrapper","__REACT_ERROR_OVERLAY_GLOBAL_HOOK__"],g=["location"],b=["webpackJsonp","__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],C=["location","history"];var v;!function(e){e.TARGET="TARGET",e.WINDOW="WINDOW"}(v||(v={}));const L={undefined:!0,Array:!0,Object:!0,String:!0,Boolean:!0,Math:!0,Reflect:!0,Function:!0,Number:!0,Symbol:!0,parseFloat:!0,parseInt:!0,Float32Array:!0,Set:!0,Infinity:!0,navigator:!0,devicePixelRatio:!0,Promise:!0,HTMLElement:!0,Map:!0,Element:!0,performance:!0,console:!0,isFinite:!0,RegExp:!0,JSON:!0,Date:!0,Proxy:!0,isNaN:!0,WeakMap:!0,WeakSet:!0,Uint8Array:!0},T={__VUE_OPTIONS_API__:!0,__VUE_I18N_LEGACY_API__:!0,__VUE_I18N_FULL_INSTALL__:!0,__VUE_DEVTOOLS_GLOBAL_HOOK__:!0,__VUE_DEVTOOLS_HOOK_REPLAY__:!0,__VUE_DEVTOOLS_PLUGINS__:!0,__DEV__:!0,webpackChunktrace:!0,i18n:!0,"__core-js_shared__":!0},x=new WeakMap;const P=new WeakMap;function R(e,t){if(P.has(t))return P.get(t);if(l(t)&&!function(e){if(x.has(e))return x.get(e);const t=e.toString(),n=e.prototype&&e.prototype.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1||/^function\s+[A-Z]/.test(t)||/^class\s+/.test(t);return x.set(e,n),n}(t)){const n=t.bind(e);return Object.keys(t).forEach((e=>n[e]=t[e])),t.hasOwnProperty("prototype")&&!n.hasOwnProperty("prototype")&&(n.prototype=t.prototype),P.set(t,n),n}return t}class B{app;resetWindowFunction;resetDocumentAndBodyEvent;injectedKeySet=new Set;escapedKeySet=new Set;active=!1;rawWindow;rawDocument;proxyWindow;fakeWindow={};windowSymbolKey;constructor(e){this.app=e;const n=new Map,r=window,i=window.document;this.fakeWindow.__POWERED_BY_BK_WEWEB__=!0,this.fakeWindow.__BK_WEWEB_APP_KEY__=e.appCacheKey,this.rawWindow=r,this.rawDocument=i,this.fakeWindow.rawWindow=r,this.fakeWindow.rawDocument=i;const{resetWindowFunction:c}=function(e){const n=t(),r=new Map,i=new Map,c=new Map,a=window,l=window.document,{addEventListener:h,removeEventListener:p,setInterval:u,setTimeout:d,clearInterval:m,clearTimeout:f}=window,{removeEventListener:y}=window.document;return e.addEventListener=function(e,t,n){const o=r.get(e);o?o.add(t):r.set(e,new Set([t])),h.call(a,e,t,n)},e.removeEventListener=function(e,t,n){const o=r.get(e);o?.size&&o.has(t)&&o.delete(t),p.call(a,e,t,n)},e.setInterval=function(e,t,...n){const o=u.call(a,e,t,...n);return i.set(o,{handler:e,timeout:t,args:n}),o},e.setTimeout=function(e,t,...n){const o=d.call(a,e,t,...n);return c.set(o,{handler:e,timeout:t,args:n}),o},e.clearInterval=function(e){i.delete(e),m.call(a,e)},e.clearTimeout=function(e){c.delete(e),f.call(a,e)},{resetWindowFunction:()=>{if(r.size&&(r.forEach(((e,t)=>{for(const n of e)p.call(a,t,n)})),r.clear()),i.size&&(i.forEach(((e,t)=>{m.call(a,t)})),i.clear()),c.size&&(c.forEach(((e,t)=>{f.call(a,t)})),c.clear()),n){o.delete(n.appCacheKey);const e=s.get(n.appCacheKey);e&&(e.forEach(((e,t)=>{for(const n of e)y.call(l,t,n)})),e.clear())}}}}(this.fakeWindow);this.resetWindowFunction=c,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${E(10)}__`,this.proxyWindow=new Proxy(this.fakeWindow,{get:(t,n)=>{if(L[n]||n===Symbol.unscopables)return r[n];if(T[n])return this.fakeWindow[n];if(["window","self","globalThis"].includes(n))return this.proxyWindow;if("document"===n||"eval"===n)return e.registerRunningApp(),"document"===n?i:eval;if(C.includes(n)&&this.app instanceof G&&this.app.iframe&&this.app.scopeLocation)return this.app.iframe.contentWindow?.[n];if("hasOwnProperty"===n)return e=>this.fakeWindow.hasOwnProperty(e)||r.hasOwnProperty(e);if("top"===n||"parent"===n)return r===r.parent?this.proxyWindow:Reflect.get(r,n);if(Reflect.has(t,n)||b.includes(n))return Reflect.get(t,n);const o=Reflect.get(r,n);return R(r,o)},set:(e,t,n)=>{if(this.active){if(C.includes(t)&&this.app instanceof G&&this.app.iframe&&this.app.scopeLocation)return Reflect.set(this.app.iframe.contentWindow,t,n);if(g.includes(t))Reflect.set(r,t,n);else if(e.hasOwnProperty(t)||!r.hasOwnProperty(t)||b.includes(t))Reflect.set(e,t,n),this.injectedKeySet.add(t);else{const o=Object.getOwnPropertyDescriptor(r,t),{writable:s,configurable:i,enumerable:c}=o;s&&(Object.defineProperty(e,t,{configurable:i,enumerable:c,writable:s,value:n}),this.injectedKeySet.add(t))}!S.includes(t)||Reflect.has(r,t)||b.includes(t)||(Reflect.set(r,t,n),this.escapedKeySet.add(t))}return!0},has:(e,t)=>L[t]||t in e||t in r,getOwnPropertyDescriptor:(e,t)=>{if(e.hasOwnProperty(t))return Object.getOwnPropertyDescriptor(e,t);if(r.hasOwnProperty(t)){n.set(t,v.WINDOW);const e=Object.getOwnPropertyDescriptor(r,t);return e&&!e.configurable&&(e.configurable=!0),e}},defineProperty:(e,t,o)=>n.get(t)===v.WINDOW?Reflect.defineProperty(r,t,o):Reflect.defineProperty(e,t,o),ownKeys:e=>{return t=Reflect.ownKeys(r).concat(Reflect.ownKeys(e)),Array.from(new Set(t));var t},deleteProperty:(e,t)=>!e.hasOwnProperty(t)||(this.injectedKeySet.has(t)&&this.injectedKeySet.delete(t),this.escapedKeySet.has(t)&&Reflect.deleteProperty(r,t),Reflect.deleteProperty(e,t))}),e.showSourceCode&&(r[this.windowSymbolKey]=this.proxyWindow)}activeated(e){if(!this.active){this.active=!0,this.fakeWindow.__BK_WEWEB_DATA__=e??{};const{resetDocumentAndBodyEvent:n}=function(){const{addEventListener:e,removeEventListener:n}=window.document,{addEventListener:o,removeEventListener:r}=window.document.body;return document.addEventListener=function(n,o,r){const i=t();if(i){const e=s.get(i.appCacheKey);if(e){const t=e.get(n);t?t.add(o):e.set(n,new Set([o]))}else s.set(i.appCacheKey,new Map([[n,new Set([o])]]))}e.call(i?.container instanceof ShadowRoot?i.container:this,n,o,r)},document.body.addEventListener=document.addEventListener,document.removeEventListener=function(e,o,r){const i=t();if(i){const t=s.get(i.appCacheKey);if(t){const n=t.get(e);n?.size&&n.has(o)&&n.delete(o)}}n.call(i?.container instanceof ShadowRoot?i.container:this,e,o,r)},document.body.removeEventListener=document.removeEventListener,{resetDocumentAndBodyEvent:function(){document.addEventListener=e,document.body.addEventListener=o,document.removeEventListener=n,document.body.removeEventListener=r}}}();this.resetDocumentAndBodyEvent=n}}deactivated(){this.active&&(this.active=!1,this.resetWindowFunction(),this.injectedKeySet.clear(),this.escapedKeySet.forEach((e=>Reflect.deleteProperty(window,e))),this.escapedKeySet.clear(),this.resetDocumentAndBodyEvent?.())}}function M(e,t){Object.defineProperties(e,{currentTarget:{get:()=>t},srcElement:{get:()=>t},target:{get:()=>t}})}function O(e){const t=new CustomEvent("load");M(t,e),l(e.onload)?e.onload(t):e.dispatchEvent(t)}const{appendChild:D}=HTMLBodyElement.prototype;function W(e,t,n){if(t instanceof HTMLStyleElement){if(t.hasAttribute("exclude"))return document.createComment("【bk-weweb】style with exclude attribute is ignored");if(!t.hasAttribute("ignore")){const e=new U({code:t.textContent||"",url:"",fromHtml:!1});n.source?.setStyle(m(),e),e.scopedStyleCSS(n,t)}return t}if(t instanceof HTMLLinkElement){const o=n.source?.collectLink(t,e,!0);return o?(o.style&&o.style.scopedLinkCSS(n,t),o.replace!==t?o.replace:t):t}if(t instanceof HTMLScriptElement){const o=n.source.collectScript(t,e,!0);if(!o)return t;if(o.script&&o.script.excuteCode(n),o.replace!==t)return o.replace;if(n.scopeJs&&!t.getAttribute("src")&&!t.textContent){const o=new MutationObserver((()=>{if(t.getAttribute("src")){o.disconnect();const s=n.source.collectScript(t,e,!0);s?.replace&&D.call(n.container,s.replace),s?.script&&s.script.excuteCode(n),t.remove()}else if(t.textContent){o.disconnect();const e=new Y({code:t.textContent,async:!1,defer:"module"===t.type,isModule:"module"===t.type,fromHtml:!1});n.source.scripts.set(m(),e);try{e.excuteCode(n)}catch(e){console.error(e)}finally{!e.isModule&&O(t),t.remove()}}}));return o.observe(t,{attributeFilter:["src"],childList:!0,subtree:!1}),document.createComment("【bk-weweb】dynamic script or module")}return t}return t}function K(e){return e instanceof HTMLScriptElement||e instanceof HTMLStyleElement||e instanceof HTMLLinkElement}function k(e,t,n){if(t.__BK_WEWEB_APP_KEY__){const o=Z.getApp(t.__BK_WEWEB_APP_KEY__);if(o?.container){const s=W(e,t,o),r=K(t)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),i=r?document.head:o?.container;return H(s,o,r),n.call(i,s)}}return n.call(e,t)}function H(e,t,n){return n&&(e.__KEEP_ALIVE__=t.appCacheKey,e.setAttribute("data-from",t.name),e.setAttribute("data-keep-alive","true")),e.setAttribute?.("powered-by","bk-weweb"),e}class U{scopedCode="";code="";prefetch=!1;preload=!1;url;scoped;fromHtml;initial;constructor({code:e,prefetch:t,preload:n,url:o,fromHtml:s,initial:r}){this.scoped=!1,this.code=e,this.prefetch=t??!1,this.preload=n??!1,this.url=o,this.fromHtml=s,this.initial=r??!1}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";if(e?.source?.styles?.has(this.url)&&(t=e.source.styles.get(this.url)?.code||""),!t&&Z.getCacheStyle(this.url)){t=Z.getCacheStyle(this.url)?.code||""}return t||(t=await A(this.url).catch((()=>""))),this.code=t,t}async excuteCode(e){e.registerRunningApp();let t=y("style");t.setAttribute("type","text/css"),t.textContent=this.code;try{this.code||await this.getCode(e),t=this.scopedStyleCSS(e,t),this.scoped=!0}catch(e){console.error("scoped style error",e)}return t}scopedStyleCSS(e,t){const n=!(!e.keepAlive||e.container instanceof ShadowRoot);if(H(t,e,n),this.code||t.textContent)t.textContent&&(t.textContent="",t.innerHTML=""),c.textContent=this.code,this.commonScoped(c,t,e),c.textContent="";else{const n=new MutationObserver((()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.commonScoped(t,t,e))}));n.observe(t,{attributes:!1,childList:!0,subtree:!0,characterData:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}scopedLinkCSS(e,t){const n=y("style");n.setAttribute("type","text/css");const o=!(!e.keepAlive||e.container instanceof ShadowRoot);H(n,e,o);const s=o?document.head:e.container;try{if(this.code)c.textContent=n.textContent||this.code,this.commonScoped(c,n,e),s?.prepend(n),t&&O(t),c.textContent="";else if(t.getAttribute("href"))this.url=u(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),s?.prepend(n),t&&O(t),this.scoped=!0}));else{const o=new MutationObserver((()=>{t.href&&(o.disconnect(),this.url=u(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),s?.prepend(n),t&&O(t),this.scoped=!0})))}));o.observe(t,{attributeFilter:["href"],subtree:!1,childList:!1})}}catch{t&&function(e){const t=new CustomEvent("error");M(t,e),l(e.onerror)?e.onerror(t):e.dispatchEvent(t)}(t)}return n}commonScoped(e,t,n){if(!n.scopeCss||n.container instanceof ShadowRoot){const o=this.resetUrlHost(t.textContent||e.textContent||"",n.url,this.url);t.textContent=o}else{const o=Array.from(e.sheet?.cssRules??[]),s=`#${n.name}`,r=this.scopeRule(o,s);let i=this.resetUrlHost(r,n.url,this.url);/Safari/.test(navigator.userAgent)&&(i=i.replace(/([;{]\s*content:\s*)([^\s"][^";}]*)/gm,((e,t,n)=>"none"===n||/^(url\()|(counter\()|(attr\()|(open-quote)|(close-quote)/.test(n)?e:`${t}"${n}"`))),t.textContent=i,this.scopedCode=i}this.scoped=!0}scopeRule(e,t){let n="";for(const o of e)switch(o.type){case _.STYLE_RULE:n+=this.scopeStyleRule(o,t);break;case _.MEDIA_RULE:n+=this.resetPackRule(o,t,"media");break;case _.SUPPORTS_RULE:n+=this.resetPackRule(o,t,"supports");break;default:n+=o.cssText}return n.replace(/^\s+/,"")}resetPackRule(e,t,n){const o=this.scopeRule(Array.from(e.cssRules),t);return`@${n} ${e.conditionText} {${o}}`}resetUrlHost(e,t,n){return e.replace(/url\(["']?([^)"']+)["']?\)/gm,((e,o)=>/^(data|blob):/.test(o)||/^(https?:)?\/\//.test(o)?e:(/^((\.\.?\/)|[^/])/.test(o)&&n&&(t=function(e){const t=e.split("/");return t.pop(),p(`${t.join("/")}/`)}(n)),`url("${u(o,t)}")`)))}scopeStyleRule(e,t){const{selectorText:n,cssText:o}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if("*"===n)return o.replace("*",`${t} *`);const s=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,(e=>e.replace(/(^|,)([^,]+)/g,((e,n,o)=>s.test(o)?e.replace(s,t):`${n} ${t} ${o.replace(/^\s*/,"")}`))))}}async function N(e,t){const n=Array.from(e.source.styles.values()),o=[];n.forEach((t=>{o.push(t.excuteCode(e))})),await Promise.all(o).then((n=>{const o=t||e.container;!e.keepAlive||o instanceof ShadowRoot?o?.prepend(...n):document.head.append(...n)}))}class I{state=a.UNSET;isPreLoad=!1;appCacheKey;url;container;scopeJs=!1;source;sandBox;name;showSourceCode=!0;scopeCss=!0;keepAlive;initSource;data;constructor(e){this.name=e.id!==e.url?e.id:E(5),this.appCacheKey=e.id||this.name,this.url=e.url,this.container=e.container??void 0,this.scopeJs=e.scopeJs??!0,this.showSourceCode=e.showSourceCode??!0,this.scopeCss=e.scopeCss??!0,this.keepAlive=e.keepAlive??!1,this.data=e.data??{},this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new B(this))}get status(){return this.state}set status(e){this.state=e}async start(){this.source&&![a.UNSET,a.ERROR].includes(this.status)||(this.source=new z(this.url),await this.source.importEntery(this))}onMount(){this.isPreLoad||(this.state=a.LOADED,this.mount())}onError(){this.state=a.ERROR}mount(e,t){this.isPreLoad=!1,this.container=e??this.container,this.state=a.MOUNTING,this.container instanceof HTMLElement&&this.container.setAttribute("id",this.name),this.container.innerHTML="";const n=document.createElement("div"),o=`${this.name}-wrapper`;n.setAttribute("id",o),this.source?.styles.size&&N(this,this.container),this.container.appendChild(n),this.sandBox?.activeated(),F(this).finally((()=>{this.state=a.MOUNTED;const e=this.source?.getScript(this.url);"function"==typeof e?.exportInstance?.render&&e.exportInstance.render(n,this.data),t?.(this,e?.exportInstance)}))}activated(e,t){if(this.isPreLoad=!1,this.state=a.ACTIVATED,this.container&&e){e instanceof Element&&e.setAttribute("id",this.name);const n=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{n.appendChild(e)})),e.appendChild(n),this.container=e,this.sandBox?.activeated();const o=this.source?.getScript(this.url);t?.(this,o?.exportInstance)}}deactivated(){this.state=a.DEACTIVATED,this.sandBox?.deactivated()}unmount(e){this.state=a.UNMOUNT,this.sandBox?.deactivated(),e&&Z.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}registerRunningApp(){n(this),Promise.resolve().then((()=>n(null)))}}let $,j,V;class Y{code="";async=!1;defer=!1;isModule=!1;url;exportInstance;scoped;fromHtml;initial;constructor({code:e,async:t,defer:n,isModule:o,url:s,fromHtml:r,initial:i}){this.code=e,this.async=t,this.defer=n,this.isModule=o,this.url=s,this.scoped=!1,this.fromHtml=r??!1,this.initial=i??!1}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.getScript(this.url)&&(t=e.source.getScript(this.url)?.code||""),!t&&Z.getCacheScript(this.url)&&(t=Z.getCacheScript(this.url)?.code||""),t||(t=await A(this.url).catch((e=>(console.error(`fetch script ${this.url} error`,e),"")))),this.code=t,t}async excuteCode(e,t=!1){try{this.code||await this.getCode(e),e instanceof I&&function(e){j=void 0,$=j;for(const t in e)J(e,t)||($?j||(j=t):$=t,V=t)}(e.sandBox?.proxyWindow||window);let n=this.code;if(n=this.transformCode(e),e.showSourceCode||this.isModule){const o=y("script");if(e.registerRunningApp(),this.executeSourceScript(o,n),t)return o;const s=!(!e.keepAlive||e.container instanceof ShadowRoot),r=s?document.head:e.container;H(o,e,s),r.appendChild(o)}else if(this.executeMemoryScript(e,n),t)return document.createComment("【bk-weweb】dynamic script");if(e instanceof I){const t=function(e,t){let n,o,s=0;for(const r in e)if(!J(e,r)){if(0===s&&r!==$||1===s&&r!==j)return r;n?(V=r,o=t&&o||r):n=r===V,s+=1}return o}(e.sandBox?.proxyWindow||window);t&&(this.exportInstance=(e.sandBox?.proxyWindow||window)[t])}}catch(e){console.error("execute script code error",e)}}transformCode(e){return e.sandBox?e.showSourceCode||this.isModule?`;(function(window, self){\n with(window){\n ;${this.code}\n\n }\n }).call(window.${e.sandBox.windowSymbolKey},\n window.${e.sandBox.windowSymbolKey});`:`\n with(window) {\n try {\n ${this.code}\n }\n catch(e) {\n console.error(e)\n }\n }\n `:this.code}executeSourceScript(e,t){if(this.isModule){const n=new Blob([t],{type:"text/javascript"});e.src=URL.createObjectURL(n),e.setAttribute("type","module")}else e.textContent=t;this.url&&e.setAttribute("origin-src",this.url)}executeMemoryScript(e,t){try{const n=e instanceof G&&e.scopeLocation;e.registerRunningApp(),new Function("window","location","history",t)(e.sandBox.proxyWindow,n?e.iframe?.contentWindow?.location:window.location,n?e.iframe?.contentWindow?.history:window.history)}catch(e){console.error(e)}}}function J(e,t){return!e.hasOwnProperty(t)||!isNaN(t)&&t<e.length||d&&e[t]&&"undefined"!=typeof window&&e[t].parent===window}async function F(e){const t=Array.from(e.source.scripts.values()).filter((e=>e.initial));t.length&&await Promise.all(t.map((t=>t.excuteCode(e))));const n=Array.from(e.source.scripts.values()).filter((e=>e.fromHtml&&!e.initial)),o=n.filter((e=>!e.async&&!e.defer||e.isModule));await Promise.all(o.map((t=>t.excuteCode(e))));const s=[],r=[];n.forEach((t=>{(t.defer||t.async)&&(!t.code&&t.defer?s.push(t.excuteCode(e)):r.push(t.excuteCode(e)))})),await Promise.all([...r,...s]).catch((e=>{console.error(e)}))}class G{state=a.UNSET;isPreLoad=!1;mode="app";name;url;container;showSourceCode;scopeCss;scopeJs;source;sandBox;scopeLocation;keepAlive;initSource;iframe=null;data;constructor(e){this.name=e.id!==e.url?e.id:E(5),this.mode=e.mode??"app",this.container=e.container??void 0,this.showSourceCode=e.showSourceCode??!1,this.url=e.url,this.data=e.data||{},this.scopeJs=e.scopeJs??!0,this.scopeCss=e.scopeCss??!0,this.scopeLocation=e.scopeLocation??!1,this.isPreLoad=e.isPreLoad??!1,this.keepAlive=e.keepAlive??!1,this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new B(this)),this.container instanceof HTMLElement&&this.container.setAttribute("id",this.name)}get appCacheKey(){return this.url}get status(){return this.state}set status(e){this.state=e}async start(){if(!this.source||[a.UNSET,a.ERROR].includes(this.status)){if(this.state=a.LOADING,this.scopeLocation){const e=await this.createIframe();this.iframe=e}this.source=new z(this.url),await this.source.importEntery(this)}}onMount(){this.isPreLoad||(this.state=a.LOADED,this.mount())}onError(){this.state=a.ERROR}mount(e,t){if(this.isPreLoad=!1,this.container=e??this.container,this.state=a.MOUNTING,this.container){this.container instanceof Element&&this.container.setAttribute("id",this.name);const e=this.source.html.cloneNode(!0),n=document.createDocumentFragment();Array.from(e.childNodes).forEach((e=>{n.appendChild(e)})),this.container.innerHTML="",this.container.appendChild(n),this.sandBox?.activeated(this.data),F(this).finally((()=>{this.state=a.MOUNTED,t?.(this)}))}}activated(e,t){if(this.isPreLoad=!1,this.state=a.ACTIVATED,e&&this.container){e instanceof Element&&e.setAttribute("id",this.name);const n=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{n.appendChild(e)})),e.innerHTML="",e.appendChild(n),this.container=e,this.sandBox?.activeated(this.data),t?.(this)}}deactivated(){this.state=a.DEACTIVATED,this.sandBox?.deactivated()}unmount(e=!1){this.state=a.UNMOUNT,this.sandBox?.deactivated(),e&&Z.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}createIframe(){return new Promise((e=>{const t=document.createElement("iframe"),n=new URL(this.url);t.setAttribute("src",`about:blank${n.pathname||"/"}${n.search}${n.hash}`),t.style.cssText="position: fixed; top: -9999px; width: 100%; height: 1px;",document.body.appendChild(t),setTimeout((()=>e(t)),100)}))}registerRunningApp(){n(this),Promise.resolve().then((()=>n(null)))}}async function q(e){let t=[];if(t="function"==typeof e?await e():e||[],!t.length)return{};const n=new Map,o=new Map;return t.forEach((e=>{try{const t=new URL(e);if(t.pathname.match(/\.js$/)){const t=Z.getCacheScript(e);n.set(e,new Y({code:t?.code||"",async:!1,defer:!1,url:e,isModule:!1,initial:!0,fromHtml:!1}))}else if(t.pathname.match(/\.css$/)){const t=Z.getCacheStyle(e);o.set(e,new U({code:t?.code||"",prefetch:!1,preload:!1,url:e,fromHtml:!0,initial:!0}))}}catch{console.error(`【bk-weweb】: ${e} is invalid URL`)}})),{collectScript:n,collectStyle:o}}class z{url;scripts;styles;html=null;rawHtml;constructor(e){this.url=e,this.scripts=new Map,this.styles=new Map}async importEntery(e){let t;if(e.initSource?.length){const{collectScript:t,collectStyle:n}=await q(e.initSource);t&&(this.scripts=t),n&&(this.styles=n)}e instanceof G?t=this.importHtmlEntry(e):e instanceof I&&(t=this.importInstanceEntry()),t&&await t}async importHtmlEntry(e){let t=Z.getCacheHtml(this.url);if(!t&&(t=await A(this.url,{cache:"no-cache"}),!t))return console.error("load app entry error, pleace check"),Promise.reject();this.rawHtml=t;const n=y("div");n.innerHTML=t.replace(/<\/?head>/gim,"").replace(/<\/?body>/i,""),this.collectScriptAndStyle(n,e),await N(e,n),this.html=n}async importInstanceEntry(){let e=Z.getCacheScript(this.url)?.code;if(e||(e=await A(this.url,{cache:"no-cache"})),!e)return console.error("load app entry error, pleace check"),Promise.reject();this.scripts.set(this.url,new Y({code:e,async:!1,defer:!1,url:this.url,isModule:!1,fromHtml:!0}))}collectScriptAndStyle(e,t){const n=Array.from(e.children);n.length&&n.forEach((e=>{this.collectScriptAndStyle(e,t)}));for(const t of n)t instanceof HTMLLinkElement?this.collectLink(t,e):t instanceof HTMLStyleElement?t.hasAttribute("exclude")||t.hasAttribute("ignore")||(this.styles.set(m(),new U({code:t.textContent||"",url:"",fromHtml:!0})),t.remove()):t instanceof HTMLScriptElement?this.collectScript(t,e):t instanceof HTMLMetaElement||t instanceof HTMLTitleElement?e.removeChild(t):t instanceof HTMLImageElement&&t.hasAttribute("src")&&t.setAttribute("src",u(t.getAttribute("src"),this.url))}collectLink(e,t,n=!1){if(e.hasAttribute("exclude"))return{replace:document.createComment("【bk-weweb】style with exclude attribute is ignored")};if(e.hasAttribute("ignore"))return{replace:e};const o=e.getAttribute("rel");let s,r=e.getAttribute("href");if("stylesheet"===o&&r){r=u(r,this.url),s=document.createComment(`【bk-weweb】style with href=${r}`);let o=this.getStyle(r);return o||(o=new U({code:"",url:r,prefetch:!!e.getAttribute("prefetch"),preload:!!e.getAttribute("preload"),fromHtml:!n}),this.styles.set(r,o)),!n&&t.replaceChild(s,e),{replace:s,style:o}}return o&&["prefetch","preload","prerender","icon","apple-touch-icon"].includes(o)?(s=document.createComment(`【bk-weweb】style with rel=${o}${r?` & href=${r}`:""}`),!n&&t.removeChild(e),{replace:s}):(r&&e.setAttribute("href",u(r,this.url)),{replace:e})}collectScript(e,t,n=!1){if(e.hasAttribute("ignore")||e.hasAttribute("type")&&!["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"].includes(e.type)||!r&&"module"===e.type)return;let o=null;if(e.hasAttribute("exclude"))return o=document.createComment("【bk-weweb】script element with exclude attribute is removed"),!n&&t.replaceChild(o,e),{replace:o};let s=e.getAttribute("src");if(s){s=u(s,this.url);let r=this.getScript(s);return r||(r=new Y({code:"",async:e.hasAttribute("async"),defer:e.defer||"module"===e.type,isModule:"module"===e.type,url:s,fromHtml:!n}),this.scripts.set(s,r)),o=document.createComment(`【bk-weweb】script with src='${s}'`),!n&&t.replaceChild(o,e),{replace:o,script:r}}if(e.textContent){const s=m(),r=new Y({code:e.textContent,async:!1,defer:"module"===e.type,isModule:"module"===e.type,url:s,fromHtml:!n});return this.scripts.set(s,r),o=document.createComment("【bk-weweb】script with texcontent"),!n&&t.replaceChild(o,e),{replace:o,script:r}}return{replace:e}}setScript(e,t){this.scripts.set(e,new Y(t))}getScript(e){return this.scripts.get(e)}setStyle(e,t){this.styles.set(e,t)}getStyle(e){return this.styles.get(e)}}const Z=new class{cache;baseSource;constructor(){this.cache=new Map,this.baseSource=new z(location.href)}get hasActiveApp(){return Array.from(this.cache.values()).some((e=>e.status!==a.UNMOUNT))}setApp(e){this.cache.set(e.appCacheKey,e)}getApp(e){if(!e)return;const t=this.cache.get(e);return t||Array.from(this.cache.values()).find((t=>t.name===e))}getCacheHtml(e){const t=Array.from(this.cache.values()).find((t=>t.url===e));return t&&t.source?.rawHtml||""}getCacheStyle(e){let t=this.baseSource.getStyle(e);if(t)return;return Array.from(this.cache.values()).some((n=>(t=n.source?.getStyle(e),!!t))),t}getCacheScript(e){let t=this.baseSource.getScript(e);if(t)return;return Array.from(this.cache.values()).some((n=>(t=n.source?.getScript(e),!!t))),t}deleteApp(e){this.cache.delete(e)}setBaseAppStyle(e,t){this.baseSource.setStyle(e,t)}setBaseAppScript(e,t){this.baseSource.setScript(e,t)}};window.__getAppOrInstance__=function(e){return e?Z.getApp(e):Z};const{document:Q}=window,{createElement:X,querySelector:ee,querySelectorAll:te,getElementById:ne,getElementsByClassName:oe,getElementsByTagName:se,getElementsByName:re}=Document.prototype;function ie(){Document.prototype.createElement=X,Document.prototype.querySelector=ee,Document.prototype.querySelectorAll=te,Document.prototype.getElementById=ne,Document.prototype.getElementsByClassName=oe,Document.prototype.getElementsByTagName=se,Document.prototype.getElementsByName=re}const{setAttribute:ce}=Element.prototype,{appendChild:ae,removeChild:le,append:he}=HTMLBodyElement.prototype,{appendChild:pe,removeChild:ue,insertBefore:de}=HTMLHeadElement.prototype,me=document.head;function fe(){Element.prototype.setAttribute=function(e,t){const n=this.tagName.toLocaleUpperCase();(["src","srcset"].includes(e)&&["SCRIPT","IMG"].includes(n)||"href"===e&&["LINK"].includes(n))&&this.__BK_WEWEB_APP_KEY__&&Z.getApp(this.__BK_WEWEB_APP_KEY__||"")?ce.call(this,e,u(t,Z.getApp(this.__BK_WEWEB_APP_KEY__).url)):ce.call(this,e,t)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&K(e)?pe.call(me,e):k(this,e,ae)},HTMLBodyElement.prototype.append=function(...e){let t=0;const{length:n}=e;for(;t<n;){if(e[t].__KEEP_ALIVE__&&K(e[t]))return pe.call(me,e[t]);k(this,e[t],ae),t+=1}},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(e,t){return function(e,t,n,o){if(t.__BK_WEWEB_APP_KEY__){const s=Z.getApp(t.__BK_WEWEB_APP_KEY__);if(s?.container){const r=K(t)&&s.keepAlive&&!(s.container instanceof ShadowRoot),i=r?document.head:s?.container,c=W(e,t,s);return r&&(c.__KEEP_ALIVE__=s.appCacheKey,c.setAttribute("data-from",s.name),c.setAttribute("data-keep-alive","true")),n&&!i.contains(n)?D.call(i,c):o.call(i,c,n)}}return o.call(e,t,n)}(this,e,t,de)},HTMLBodyElement.prototype.removeChild=function(e){const t=Z.getApp(e.__BK_WEWEB_APP_KEY__);if(t?.container?.contains(e)){return le.call(t.container,e)}return this.contains(e)?le.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild}function ye(){n(null),Element.prototype.setAttribute=ce,HTMLBodyElement.prototype.appendChild=ae,HTMLBodyElement.prototype.append=he,HTMLBodyElement.prototype.removeChild=le,HTMLHeadElement.prototype.appendChild=pe,HTMLHeadElement.prototype.insertBefore=de,HTMLHeadElement.prototype.removeChild=ue}function we(){fe(),function(){function e(e){const n=t();return n&&e&&!w(e)&&Q===this?n?.container?.querySelector(e)??null:ee.call(this,e)}function n(e){const n=t();return n&&e&&!w(e)&&Q===this?n?.container?.querySelectorAll(e)??[]:te.call(this,e)}Document.prototype.createElement=function(e,n){const o=X.call(this,e,n),s=t();return s&&(o.__BK_WEWEB_APP_KEY__=s.appCacheKey),o},Document.prototype.querySelector=e,Document.prototype.querySelectorAll=n,Document.prototype.getElementById=function(n){if(!t())return ne.call(this,n);try{return e.call(this,`#${n}`)}catch{return ne.call(this,n)}},Document.prototype.getElementsByClassName=function(e){if(!t())return oe.call(this,e);try{return n.call(this,`.${e}`)}catch{return oe.call(this,e)}},Document.prototype.getElementsByTagName=function(e){const o=t();if(!o||w(e)||!o?.showSourceCode&&"script"===e.toLocaleLowerCase())return se.call(this,e);try{return n.call(this,e)}catch{return se.call(this,e)}},Document.prototype.getElementsByName=function(e){if(!t())return re.call(this,e);try{return n.call(this,`[name=${e}]`)}catch{return re.call(this,e)}}}()}async function Ee(e){return"app"===e.mode?await Ae(e):"js"===e.mode?await _e(e):await Ae(e)}async function Ae(e){let t=Z.getApp(e.id);return t||(t=new G(e),Z.setApp(t)),await t.start(),t}function _e(e){return new Promise((t=>{let n=Z.getApp(e.id);if(n)if([a.UNSET,a.MOUNTING].includes(n.status)){const e=setInterval((()=>{[a.ERROR,a.MOUNTED].includes(n.status)&&(t(n),clearInterval(e))}),300)}else t(n);else n=new I(e),Z.setApp(n),n.start().then((()=>t(n)))}))}function Se(e){const t=Z.getApp(e);t?.status!==a.UNMOUNT&&t?.unmount(),Z.hasActiveApp||(ye(),ie())}function ge(e,t,n){const o=Z.getApp(e);o?.status===a.DEACTIVATED&&o.keepAlive?h((()=>o?.activated(t,n))):h((()=>o?.mount(t,n)))}function be(e){const t=Z.getApp(e);t&&[a.ACTIVATED,a.MOUNTED].includes(t.status)&&(t.keepAlive?t.deactivated():t.unmount()),Z.hasActiveApp||(ye(),ie())}var Ce;!function(e){e.URL="url",e.showSourceCode="showSourceCode",e.scopeLocation="scopeLocation",e.setShodowDom="setShodowDom",e.mode="mode",e.data="data",e.id="id",e.scopeJs="scopeJs",e.scopeCss="scopeCss",e.keepAlive="keepAlive"}(Ce||(Ce={}));class ve extends HTMLElement{static get observedAttributes(){return[Ce.URL]}connected=!1;appUrl="";get appKey(){return this.getAttribute(Ce.id)||this.getAttribute(Ce.URL)}get appData(){if(this.hasAttribute(Ce.data))try{return JSON.parse(this.getAttribute(Ce.data))}catch{}return{}}get appProps(){return"js"===this.getAttribute("mode")?{url:this.getAttribute(Ce.URL),mode:"js",id:this.appKey,data:this.appData,container:this.shadowRoot??this,showSourceCode:this.getBooleanAttr(Ce.showSourceCode),scopeCss:this.getBooleanAttr(Ce.scopeCss)&&!this.getBooleanAttr(Ce.setShodowDom),scopeJs:this.getBooleanAttr(Ce.scopeJs),keepAlive:this.getBooleanAttr(Ce.keepAlive)}:{url:this.getAttribute(Ce.URL),mode:"app",id:this.appKey,data:this.appData,container:this.shadowRoot??this,scopeLocation:this.getBooleanAttr(Ce.scopeLocation),showSourceCode:this.getBooleanAttr(Ce.showSourceCode),scopeCss:!this.getBooleanAttr(Ce.setShodowDom),scopeJs:!this.getBooleanAttr(Ce.scopeJs),keepAlive:this.getBooleanAttr(Ce.keepAlive)}}async connectedCallback(){we(),this.getBooleanAttr(Ce.setShodowDom)&&!this.shadowRoot&&this.attachShadow({mode:"open",delegatesFocus:!0}),await Ee(this.appProps),ge(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?be(this.appKey):Se(this.appKey)}attributeChangedCallback(e,t,n){e===Ce.URL&&this[e]!==n&&(this.appUrl=n,(this.connected||Z.getApp(this.appKey))&&this.handleAttributeChanged())}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr(Ce.setShodowDom)&&this.attachShadow({mode:"open"});const e=Z.getApp(this.appKey);!e||e.url!==this.appUrl||!e.isPreLoad&&e.status!==a.UNMOUNT?await Ee(this.appProps):ge(this.appKey,this.shadowRoot??this)}getBooleanAttr(e){return this.hasAttribute(e)?"false"!==this.getAttribute(e):void 0}}class Le{fetchSource;webcomponentTag="bk-weweb";constructor(){window.customElements.get("bk-weweb")||window.customElements.define("bk-weweb",ve)}start(e){"function"==typeof e?.fetchSource&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||"bk-weweb",this.setWebComponentTag()}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,ve)}}const Te=new Le;exports.IframeApp=Le,exports.activated=ge,exports.beforeLoad=we,exports.deactivated=be,exports.default=Te,exports.load=Ee,exports.loadApp=Ae,exports.loadInstance=_e,exports.mount=function(e,t,n){const o=Z.getApp(e);h((()=>o?.mount(t,n)))},exports.preLoadApp=function(e){f((()=>Ae({...e,isPreLoad:!0})))},exports.preLoadInstance=function(e){f((()=>_e({...e,isPreLoad:!0})))},exports.preLoadSource=function(e){f((()=>{!async function(e){const{collectScript:t,collectStyle:n}=await q(e),o=[];t&&Array.from(t.entries()).forEach((e=>{o.push(e[1].getCode()),Z.setBaseAppScript(...e)})),n&&Array.from(n.entries()).forEach((e=>{o.push(e[1].getCode()),Z.setBaseAppStyle(...e)})),await Promise.all(o).catch((e=>{throw e}))}(e)}))},exports.unload=function(e){Z.deleteApp(e)},exports.unmount=Se;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});let e=null;function t(){return e}function n(t){e=t}const o=new Map,s=new Map,r="noModule"in document.createElement("script"),i=document.createElement("style");i.setAttribute("id","SCOPED_CSS_STYLE_ID"),document.body.appendChild(i),i.sheet.disabled=!0;const c=i;var a;function l(e){return"function"==typeof e}!function(e){e.UNSET="UNSET",e.LOADING="LOADING",e.LOADED="LOADED",e.ERROR="ERROR",e.MOUNTING="MOUNTING",e.MOUNTED="MOUNTED",e.ACTIVATED="ACTIVATED",e.DEACTIVATED="DEACTIVATED",e.UNMOUNT="UNMOUNT"}(a||(a={}));const h="function"==typeof window.Zone?setTimeout:e=>Promise.resolve().then(e);function p(e){return e.startsWith("//")?`${location.protocol}${e}`:e}function u(e,t){return!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e)?e:new URL(e,function(e){const{origin:t,pathname:n}=new URL(e);if(/\.(\w+)$/.test(n)){const e=`${t}${n}`.split("/");return e.pop(),`${e.join("/")}/`}return`${t}${n}/`.replace(/\/\/$/,"/")}(p(t))).toString()}const d="undefined"!=typeof navigator&&-1!==navigator.userAgent.indexOf("Trident");function m(){return`inline-${E(16)}`}const f=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout((()=>{e({didTimeout:!1,handle:()=>Math.max(0,50-(Date.now()-t))})}),1)};function y(e,t){const n=document.createElement(e,t);return n.__BK_WEWEB_APP_KEY__&&delete n.__BK_WEWEB_APP_KEY__,n}function w(e){return/^(body|head|html)$/i.test(e)}const E=(e,t="abcdefghijklmnopqrstuvwxyz0123456789")=>{let n="";for(let o=0;o<e;o++)n+=t[parseInt((Math.random()*t.length).toString(),10)];return n};function A(e,t={}){return Te.fetchSource?Te.fetchSource(e,t):window.fetch(e,t).then((e=>e.text()))}var _;!function(e){e[e.STYLE_RULE=1]="STYLE_RULE",e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(_||(_={}));const S=["System","__cjsWrapper","__REACT_ERROR_OVERLAY_GLOBAL_HOOK__"],g=["location"],b=["webpackJsonp","__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],C=["location","history"];var v;!function(e){e.TARGET="TARGET",e.WINDOW="WINDOW"}(v||(v={}));const L={undefined:!0,Array:!0,Object:!0,String:!0,Boolean:!0,Math:!0,Reflect:!0,Function:!0,Number:!0,Symbol:!0,parseFloat:!0,parseInt:!0,Float32Array:!0,Set:!0,Infinity:!0,navigator:!0,devicePixelRatio:!0,Promise:!0,HTMLElement:!0,Map:!0,Element:!0,performance:!0,console:!0,isFinite:!0,RegExp:!0,JSON:!0,Date:!0,Proxy:!0,isNaN:!0,WeakMap:!0,WeakSet:!0,Uint8Array:!0},T={__VUE_OPTIONS_API__:!0,__VUE_I18N_LEGACY_API__:!0,__VUE_I18N_FULL_INSTALL__:!0,__VUE_DEVTOOLS_GLOBAL_HOOK__:!0,__VUE_DEVTOOLS_HOOK_REPLAY__:!0,__VUE_DEVTOOLS_PLUGINS__:!0,__DEV__:!0,webpackChunktrace:!0,webpackChunkpc:!0,webpackChunkapm:!0,i18n:!0,"__core-js_shared__":!0},x=new WeakMap;const P=new WeakMap;function R(e,t){if(P.has(t))return P.get(t);if(l(t)&&!function(e){if(x.has(e))return x.get(e);const t=e.toString(),n=e.prototype&&e.prototype.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1||/^function\s+[A-Z]/.test(t)||/^class\s+/.test(t);return x.set(e,n),n}(t)){const n=t.bind(e);return Object.keys(t).forEach((e=>n[e]=t[e])),t.hasOwnProperty("prototype")&&!n.hasOwnProperty("prototype")&&(n.prototype=t.prototype),P.set(t,n),n}return t}class B{app;resetWindowFunction;resetDocumentAndBodyEvent;injectedKeySet=new Set;escapedKeySet=new Set;active=!1;rawWindow;rawDocument;proxyWindow;fakeWindow={};windowSymbolKey;constructor(e){this.app=e;const n=new Map,r=window,i=window.document;this.fakeWindow.__POWERED_BY_BK_WEWEB__=!0,this.fakeWindow.__BK_WEWEB_APP_KEY__=e.appCacheKey,this.rawWindow=r,this.rawDocument=i,this.fakeWindow.rawWindow=r,this.fakeWindow.rawDocument=i;const{resetWindowFunction:c}=function(e){const n=t(),r=new Map,i=new Map,c=new Map,a=window,l=window.document,{addEventListener:h,removeEventListener:p,setInterval:u,setTimeout:d,clearInterval:m,clearTimeout:f}=window,{removeEventListener:y}=window.document;return e.addEventListener=function(e,t,n){const o=r.get(e);o?o.add(t):r.set(e,new Set([t])),h.call(a,e,t,n)},e.removeEventListener=function(e,t,n){const o=r.get(e);o?.size&&o.has(t)&&o.delete(t),p.call(a,e,t,n)},e.setInterval=function(e,t,...n){const o=u.call(a,e,t,...n);return i.set(o,{handler:e,timeout:t,args:n}),o},e.setTimeout=function(e,t,...n){const o=d.call(a,e,t,...n);return c.set(o,{handler:e,timeout:t,args:n}),o},e.clearInterval=function(e){i.delete(e),m.call(a,e)},e.clearTimeout=function(e){c.delete(e),f.call(a,e)},{resetWindowFunction:()=>{if(r.size&&(r.forEach(((e,t)=>{for(const n of e)p.call(a,t,n)})),r.clear()),i.size&&(i.forEach(((e,t)=>{m.call(a,t)})),i.clear()),c.size&&(c.forEach(((e,t)=>{f.call(a,t)})),c.clear()),n){o.delete(n.appCacheKey);const e=s.get(n.appCacheKey);e&&(e.forEach(((e,t)=>{for(const n of e)y.call(l,t,n)})),e.clear())}}}}(this.fakeWindow);this.resetWindowFunction=c,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${E(10)}__`,this.proxyWindow=new Proxy(this.fakeWindow,{get:(t,n)=>{if(L[n]||n===Symbol.unscopables)return r[n];if(T[n])return this.fakeWindow[n];if(["window","self","globalThis"].includes(n))return this.proxyWindow;if("document"===n||"eval"===n)return e.registerRunningApp(),"document"===n?i:eval;if(C.includes(n)&&this.app instanceof G&&this.app.iframe&&this.app.scopeLocation)return this.app.iframe.contentWindow?.[n];if("hasOwnProperty"===n)return e=>this.fakeWindow.hasOwnProperty(e)||r.hasOwnProperty(e);if("top"===n||"parent"===n)return r===r.parent?this.proxyWindow:Reflect.get(r,n);if(Reflect.has(t,n)||b.includes(n))return Reflect.get(t,n);const o=Reflect.get(r,n);return R(r,o)},set:(e,t,n)=>{if(this.active){if(C.includes(t)&&this.app instanceof G&&this.app.iframe&&this.app.scopeLocation)return Reflect.set(this.app.iframe.contentWindow,t,n);if(g.includes(t))Reflect.set(r,t,n);else if(e.hasOwnProperty(t)||!r.hasOwnProperty(t)||b.includes(t))Reflect.set(e,t,n),this.injectedKeySet.add(t);else{const o=Object.getOwnPropertyDescriptor(r,t),{writable:s,configurable:i,enumerable:c}=o;s&&(Object.defineProperty(e,t,{configurable:i,enumerable:c,writable:s,value:n}),this.injectedKeySet.add(t))}!S.includes(t)||Reflect.has(r,t)||b.includes(t)||(Reflect.set(r,t,n),this.escapedKeySet.add(t))}return!0},has:(e,t)=>L[t]||t in e||t in r,getOwnPropertyDescriptor:(e,t)=>{if(e.hasOwnProperty(t))return Object.getOwnPropertyDescriptor(e,t);if(r.hasOwnProperty(t)){n.set(t,v.WINDOW);const e=Object.getOwnPropertyDescriptor(r,t);return e&&!e.configurable&&(e.configurable=!0),e}},defineProperty:(e,t,o)=>n.get(t)===v.WINDOW?Reflect.defineProperty(r,t,o):Reflect.defineProperty(e,t,o),ownKeys:e=>{return t=Reflect.ownKeys(r).concat(Reflect.ownKeys(e)),Array.from(new Set(t));var t},deleteProperty:(e,t)=>!e.hasOwnProperty(t)||(this.injectedKeySet.has(t)&&this.injectedKeySet.delete(t),this.escapedKeySet.has(t)&&Reflect.deleteProperty(r,t),Reflect.deleteProperty(e,t))}),e.showSourceCode&&(r[this.windowSymbolKey]=this.proxyWindow)}activeated(e){if(!this.active){this.active=!0,this.fakeWindow.__BK_WEWEB_DATA__=e??{};const{resetDocumentAndBodyEvent:n}=function(){const{addEventListener:e,removeEventListener:n}=window.document,{addEventListener:o,removeEventListener:r}=window.document.body;return document.addEventListener=function(n,o,r){const i=t();if(i){const e=s.get(i.appCacheKey);if(e){const t=e.get(n);t?t.add(o):e.set(n,new Set([o]))}else s.set(i.appCacheKey,new Map([[n,new Set([o])]]))}e.call(i?.container instanceof ShadowRoot?i.container:this,n,o,r)},document.body.addEventListener=document.addEventListener,document.removeEventListener=function(e,o,r){const i=t();if(i){const t=s.get(i.appCacheKey);if(t){const n=t.get(e);n?.size&&n.has(o)&&n.delete(o)}}n.call(i?.container instanceof ShadowRoot?i.container:this,e,o,r)},document.body.removeEventListener=document.removeEventListener,{resetDocumentAndBodyEvent:function(){document.addEventListener=e,document.body.addEventListener=o,document.removeEventListener=n,document.body.removeEventListener=r}}}();this.resetDocumentAndBodyEvent=n}}deactivated(){this.active&&(this.active=!1,this.resetWindowFunction(),this.injectedKeySet.clear(),this.escapedKeySet.forEach((e=>Reflect.deleteProperty(window,e))),this.escapedKeySet.clear(),this.resetDocumentAndBodyEvent?.())}}function M(e,t){Object.defineProperties(e,{currentTarget:{get:()=>t},srcElement:{get:()=>t},target:{get:()=>t}})}function O(e){const t=new CustomEvent("load");M(t,e),l(e.onload)?e.onload(t):e.dispatchEvent(t)}const{appendChild:D}=HTMLBodyElement.prototype;function W(e,t,n){if(t instanceof HTMLStyleElement){if(t.hasAttribute("exclude"))return document.createComment("【bk-weweb】style with exclude attribute is ignored");if(!t.hasAttribute("ignore")){const e=new U({code:t.textContent||"",url:"",fromHtml:!1});n.source?.setStyle(m(),e),e.scopedStyleCSS(n,t)}return t}if(t instanceof HTMLLinkElement){const o=n.source?.collectLink(t,e,!0);return o?(o.style&&o.style.scopedLinkCSS(n,t),o.replace!==t?o.replace:t):t}if(t instanceof HTMLScriptElement){const o=n.source.collectScript(t,e,!0);if(!o)return t;if(o.script&&o.script.excuteCode(n),o.replace!==t)return o.replace;if(n.scopeJs&&!t.getAttribute("src")&&!t.textContent){const o=new MutationObserver((()=>{if(t.getAttribute("src")){o.disconnect();const s=n.source.collectScript(t,e,!0);s?.replace&&D.call(n.container,s.replace),s?.script&&s.script.excuteCode(n),t.remove()}else if(t.textContent){o.disconnect();const e=new Y({code:t.textContent,async:!1,defer:"module"===t.type,isModule:"module"===t.type,fromHtml:!1});n.source.scripts.set(m(),e);try{e.excuteCode(n)}catch(e){console.error(e)}finally{!e.isModule&&O(t),t.remove()}}}));return o.observe(t,{attributeFilter:["src"],childList:!0,subtree:!1}),document.createComment("【bk-weweb】dynamic script or module")}return t}return t}function k(e){return e instanceof HTMLScriptElement||e instanceof HTMLStyleElement||e instanceof HTMLLinkElement}function K(e,t,n){if(t.__BK_WEWEB_APP_KEY__){const o=Z.getApp(t.__BK_WEWEB_APP_KEY__);if(o?.container){const s=W(e,t,o),r=k(t)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),i=r?document.head:o?.container;return H(s,o,r),n.call(i,s)}}return n.call(e,t)}function H(e,t,n){return n&&(e.__KEEP_ALIVE__=t.appCacheKey,e.setAttribute("data-from",t.name),e.setAttribute("data-keep-alive","true")),e.setAttribute?.("powered-by","bk-weweb"),e}class U{scopedCode="";code="";prefetch=!1;preload=!1;url;scoped;fromHtml;initial;constructor({code:e,prefetch:t,preload:n,url:o,fromHtml:s,initial:r}){this.scoped=!1,this.code=e,this.prefetch=t??!1,this.preload=n??!1,this.url=o,this.fromHtml=s,this.initial=r??!1}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";if(e?.source?.styles?.has(this.url)&&(t=e.source.styles.get(this.url)?.code||""),!t&&Z.getCacheStyle(this.url)){t=Z.getCacheStyle(this.url)?.code||""}return t||(t=await A(this.url).catch((()=>""))),this.code=t,t}async excuteCode(e){e.registerRunningApp();let t=y("style");t.setAttribute("type","text/css"),t.textContent=this.code;try{this.code||await this.getCode(e),t=this.scopedStyleCSS(e,t),this.scoped=!0}catch(e){console.error("scoped style error",e)}return t}scopedStyleCSS(e,t){const n=!(!e.keepAlive||e.container instanceof ShadowRoot);if(H(t,e,n),this.code||t.textContent)t.textContent&&(t.textContent="",t.innerHTML=""),c.textContent=this.code,this.commonScoped(c,t,e),c.textContent="";else{const n=new MutationObserver((()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.commonScoped(t,t,e))}));n.observe(t,{attributes:!1,childList:!0,subtree:!0,characterData:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}scopedLinkCSS(e,t){const n=y("style");n.setAttribute("type","text/css");const o=!(!e.keepAlive||e.container instanceof ShadowRoot);H(n,e,o);const s=o?document.head:e.container;try{if(this.code)c.textContent=n.textContent||this.code,this.commonScoped(c,n,e),s?.prepend(n),t&&O(t),c.textContent="";else if(t.getAttribute("href"))this.url=u(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),s?.prepend(n),t&&O(t),this.scoped=!0}));else{const o=new MutationObserver((()=>{t.href&&(o.disconnect(),this.url=u(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),s?.prepend(n),t&&O(t),this.scoped=!0})))}));o.observe(t,{attributeFilter:["href"],subtree:!1,childList:!1})}}catch{t&&function(e){const t=new CustomEvent("error");M(t,e),l(e.onerror)?e.onerror(t):e.dispatchEvent(t)}(t)}return n}commonScoped(e,t,n){if(!n.scopeCss||n.container instanceof ShadowRoot){const o=this.resetUrlHost(t.textContent||e.textContent||"",n.url,this.url);t.textContent=o}else{const o=Array.from(e.sheet?.cssRules??[]),s=`#${n.name}`,r=this.scopeRule(o,s);let i=this.resetUrlHost(r,n.url,this.url);/Safari/.test(navigator.userAgent)&&(i=i.replace(/([;{]\s*content:\s*)([^\s"][^";}]*)/gm,((e,t,n)=>"none"===n||/^(url\()|(counter\()|(attr\()|(open-quote)|(close-quote)/.test(n)?e:`${t}"${n}"`))),t.textContent=i,this.scopedCode=i}this.scoped=!0}scopeRule(e,t){let n="";for(const o of e)switch(o.type){case _.STYLE_RULE:n+=this.scopeStyleRule(o,t);break;case _.MEDIA_RULE:n+=this.resetPackRule(o,t,"media");break;case _.SUPPORTS_RULE:n+=this.resetPackRule(o,t,"supports");break;default:n+=o.cssText}return n.replace(/^\s+/,"")}resetPackRule(e,t,n){const o=this.scopeRule(Array.from(e.cssRules),t);return`@${n} ${e.conditionText} {${o}}`}resetUrlHost(e,t,n){return e.replace(/url\(["']?([^)"']+)["']?\)/gm,((e,o)=>/^(data|blob):/.test(o)||/^(https?:)?\/\//.test(o)?e:(/^((\.\.?\/)|[^/])/.test(o)&&n&&(t=function(e){const t=e.split("/");return t.pop(),p(`${t.join("/")}/`)}(n)),`url("${u(o,t)}")`)))}scopeStyleRule(e,t){const{selectorText:n,cssText:o}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if("*"===n)return o.replace("*",`${t} *`);const s=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,(e=>e.replace(/(^|,)([^,]+)/g,((e,n,o)=>s.test(o)?e.replace(s,t):`${n} ${t} ${o.replace(/^\s*/,"")}`))))}}async function N(e,t){const n=Array.from(e.source.styles.values()),o=[];n.forEach((t=>{o.push(t.excuteCode(e))})),await Promise.all(o).then((n=>{const o=t||e.container;!e.keepAlive||o instanceof ShadowRoot?o?.prepend(...n):document.head.append(...n)}))}class I{state=a.UNSET;isPreLoad=!1;appCacheKey;url;container;scopeJs=!1;source;sandBox;name;showSourceCode=!0;scopeCss=!0;keepAlive;initSource;data;constructor(e){this.name=e.id!==e.url?e.id:E(5),this.appCacheKey=e.id||this.name,this.url=e.url,this.container=e.container??void 0,this.scopeJs=e.scopeJs??!0,this.showSourceCode=e.showSourceCode??!0,this.scopeCss=e.scopeCss??!0,this.keepAlive=e.keepAlive??!1,this.data=e.data??{},this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new B(this))}get status(){return this.state}set status(e){this.state=e}async start(){this.source&&![a.UNSET,a.ERROR].includes(this.status)||(this.source=new z(this.url),await this.source.importEntery(this))}onMount(){this.isPreLoad||(this.state=a.LOADED,this.mount())}onError(){this.state=a.ERROR}mount(e,t){this.isPreLoad=!1,this.container=e??this.container,this.state=a.MOUNTING,this.container instanceof HTMLElement&&this.container.setAttribute("id",this.name),this.container.innerHTML="";const n=document.createElement("div"),o=`${this.name}-wrapper`;n.setAttribute("id",o),this.source?.styles.size&&N(this,this.container),this.container.appendChild(n),this.sandBox?.activeated(),F(this).finally((()=>{this.state=a.MOUNTED;const e=this.source?.getScript(this.url);"function"==typeof e?.exportInstance?.render&&e.exportInstance.render(n,this.data),t?.(this,e?.exportInstance)}))}activated(e,t){if(this.isPreLoad=!1,this.state=a.ACTIVATED,this.container&&e){e instanceof Element&&e.setAttribute("id",this.name);const n=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{n.appendChild(e)})),e.appendChild(n),this.container=e,this.sandBox?.activeated();const o=this.source?.getScript(this.url);t?.(this,o?.exportInstance)}}deactivated(){this.state=a.DEACTIVATED,this.sandBox?.deactivated()}unmount(e){this.state=a.UNMOUNT,this.sandBox?.deactivated(),e&&Z.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}registerRunningApp(){n(this),Promise.resolve().then((()=>n(null)))}}let $,j,V;class Y{code="";async=!1;defer=!1;isModule=!1;url;exportInstance;scoped;fromHtml;initial;constructor({code:e,async:t,defer:n,isModule:o,url:s,fromHtml:r,initial:i}){this.code=e,this.async=t,this.defer=n,this.isModule=o,this.url=s,this.scoped=!1,this.fromHtml=r??!1,this.initial=i??!1}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.getScript(this.url)&&(t=e.source.getScript(this.url)?.code||""),!t&&Z.getCacheScript(this.url)&&(t=Z.getCacheScript(this.url)?.code||""),t||(t=await A(this.url).catch((e=>(console.error(`fetch script ${this.url} error`,e),"")))),this.code=t,t}async excuteCode(e,t=!1){try{this.code||await this.getCode(e),e instanceof I&&function(e){j=void 0,$=j;for(const t in e)J(e,t)||($?j||(j=t):$=t,V=t)}(e.sandBox?.proxyWindow||window);let n=this.code;if(n=this.transformCode(e),e.showSourceCode||this.isModule){const o=y("script");if(e.registerRunningApp(),this.executeSourceScript(o,n),t)return o;const s=!(!e.keepAlive||e.container instanceof ShadowRoot),r=s?document.head:e.container;H(o,e,s),r.appendChild(o)}else if(this.executeMemoryScript(e,n),t)return document.createComment("【bk-weweb】dynamic script");if(e instanceof I){const t=function(e,t){let n,o,s=0;for(const r in e)if(!J(e,r)){if(0===s&&r!==$||1===s&&r!==j)return r;n?(V=r,o=t&&o||r):n=r===V,s+=1}return o}(e.sandBox?.proxyWindow||window);t&&(this.exportInstance=(e.sandBox?.proxyWindow||window)[t])}}catch(e){console.error("execute script code error",e)}}transformCode(e){return e.sandBox?e.showSourceCode||this.isModule?`;(function(window, self){\n with(window){\n ;${this.code}\n\n }\n }).call(window.${e.sandBox.windowSymbolKey},\n window.${e.sandBox.windowSymbolKey});`:`\n with(window) {\n try {\n ${this.code}\n }\n catch(e) {\n console.error(e)\n }\n }\n `:this.code}executeSourceScript(e,t){if(this.isModule){const n=new Blob([t],{type:"text/javascript"});e.src=URL.createObjectURL(n),e.setAttribute("type","module")}else e.textContent=t;this.url&&e.setAttribute("origin-src",this.url)}executeMemoryScript(e,t){try{const n=e instanceof G&&e.scopeLocation;e.registerRunningApp(),new Function("window","location","history",t)(e.sandBox.proxyWindow,n?e.iframe?.contentWindow?.location:window.location,n?e.iframe?.contentWindow?.history:window.history)}catch(e){console.error(e)}}}function J(e,t){return!e.hasOwnProperty(t)||!isNaN(t)&&t<e.length||d&&e[t]&&"undefined"!=typeof window&&e[t].parent===window}async function F(e){const t=Array.from(e.source.scripts.values()).filter((e=>e.initial));t.length&&await Promise.all(t.map((t=>t.excuteCode(e))));const n=Array.from(e.source.scripts.values()).filter((e=>e.fromHtml&&!e.initial)),o=n.filter((e=>!e.async&&!e.defer||e.isModule));await Promise.all(o.map((t=>t.excuteCode(e))));const s=[],r=[];n.forEach((t=>{(t.defer||t.async)&&(!t.code&&t.defer?s.push(t.excuteCode(e)):r.push(t.excuteCode(e)))})),await Promise.all([...r,...s]).catch((e=>{console.error(e)}))}class G{state=a.UNSET;isPreLoad=!1;mode="app";name;url;container;showSourceCode;scopeCss;scopeJs;source;sandBox;scopeLocation;keepAlive;initSource;iframe=null;data;constructor(e){this.name=e.id!==e.url?e.id:E(5),this.mode=e.mode??"app",this.container=e.container??void 0,this.showSourceCode=e.showSourceCode??!1,this.url=e.url,this.data=e.data||{},this.scopeJs=e.scopeJs??!0,this.scopeCss=e.scopeCss??!0,this.scopeLocation=e.scopeLocation??!1,this.isPreLoad=e.isPreLoad??!1,this.keepAlive=e.keepAlive??!1,this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new B(this)),this.container instanceof HTMLElement&&this.container.setAttribute("id",this.name)}get appCacheKey(){return this.url}get status(){return this.state}set status(e){this.state=e}async start(){if(!this.source||[a.UNSET,a.ERROR].includes(this.status)){if(this.state=a.LOADING,this.scopeLocation){const e=await this.createIframe();this.iframe=e}this.source=new z(this.url),await this.source.importEntery(this)}}onMount(){this.isPreLoad||(this.state=a.LOADED,this.mount())}onError(){this.state=a.ERROR}mount(e,t){if(this.isPreLoad=!1,this.container=e??this.container,this.state=a.MOUNTING,this.container){this.container instanceof Element&&this.container.setAttribute("id",this.name);const e=this.source.html.cloneNode(!0),n=document.createDocumentFragment();Array.from(e.childNodes).forEach((e=>{n.appendChild(e)})),this.container.innerHTML="",this.container.appendChild(n),this.sandBox?.activeated(this.data),F(this).finally((()=>{this.state=a.MOUNTED,t?.(this)}))}}activated(e,t){if(this.isPreLoad=!1,this.state=a.ACTIVATED,e&&this.container){e instanceof Element&&e.setAttribute("id",this.name);const n=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{n.appendChild(e)})),e.innerHTML="",e.appendChild(n),this.container=e,this.sandBox?.activeated(this.data),t?.(this)}}deactivated(){this.state=a.DEACTIVATED,this.sandBox?.deactivated()}unmount(e=!1){this.state=a.UNMOUNT,this.sandBox?.deactivated(),e&&Z.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}createIframe(){return new Promise((e=>{const t=document.createElement("iframe"),n=new URL(this.url);t.setAttribute("src",`about:blank${n.pathname||"/"}${n.search}${n.hash}`),t.style.cssText="position: fixed; top: -9999px; width: 100%; height: 1px;",document.body.appendChild(t),setTimeout((()=>e(t)),100)}))}registerRunningApp(){n(this),Promise.resolve().then((()=>n(null)))}}async function q(e){let t=[];if(t="function"==typeof e?await e():e||[],!t.length)return{};const n=new Map,o=new Map;return t.forEach((e=>{try{const t=new URL(e);if(t.pathname.match(/\.js$/)){const t=Z.getCacheScript(e);n.set(e,new Y({code:t?.code||"",async:!1,defer:!1,url:e,isModule:!1,initial:!0,fromHtml:!1}))}else if(t.pathname.match(/\.css$/)){const t=Z.getCacheStyle(e);o.set(e,new U({code:t?.code||"",prefetch:!1,preload:!1,url:e,fromHtml:!0,initial:!0}))}}catch{console.error(`【bk-weweb】: ${e} is invalid URL`)}})),{collectScript:n,collectStyle:o}}class z{url;scripts;styles;html=null;rawHtml;constructor(e){this.url=e,this.scripts=new Map,this.styles=new Map}async importEntery(e){let t;if(e.initSource?.length){const{collectScript:t,collectStyle:n}=await q(e.initSource);t&&(this.scripts=t),n&&(this.styles=n)}e instanceof G?t=this.importHtmlEntry(e):e instanceof I&&(t=this.importInstanceEntry()),t&&await t}async importHtmlEntry(e){let t=Z.getCacheHtml(this.url);if(!t&&(t=await A(this.url,{cache:"no-cache"}),!t))return console.error("load app entry error, pleace check"),Promise.reject();this.rawHtml=t;const n=y("div");n.innerHTML=t.replace(/<\/?head>/gim,"").replace(/<\/?body>/i,""),this.collectScriptAndStyle(n,e),await N(e,n),this.html=n}async importInstanceEntry(){let e=Z.getCacheScript(this.url)?.code;if(e||(e=await A(this.url,{cache:"no-cache"})),!e)return console.error("load app entry error, pleace check"),Promise.reject();this.scripts.set(this.url,new Y({code:e,async:!1,defer:!1,url:this.url,isModule:!1,fromHtml:!0}))}collectScriptAndStyle(e,t){const n=Array.from(e.children);n.length&&n.forEach((e=>{this.collectScriptAndStyle(e,t)}));for(const t of n)t instanceof HTMLLinkElement?this.collectLink(t,e):t instanceof HTMLStyleElement?t.hasAttribute("exclude")||t.hasAttribute("ignore")||(this.styles.set(m(),new U({code:t.textContent||"",url:"",fromHtml:!0})),t.remove()):t instanceof HTMLScriptElement?this.collectScript(t,e):t instanceof HTMLMetaElement||t instanceof HTMLTitleElement?e.removeChild(t):t instanceof HTMLImageElement&&t.hasAttribute("src")&&t.setAttribute("src",u(t.getAttribute("src"),this.url))}collectLink(e,t,n=!1){if(e.hasAttribute("exclude"))return{replace:document.createComment("【bk-weweb】style with exclude attribute is ignored")};if(e.hasAttribute("ignore"))return{replace:e};const o=e.getAttribute("rel");let s,r=e.getAttribute("href");if("stylesheet"===o&&r){r=u(r,this.url),s=document.createComment(`【bk-weweb】style with href=${r}`);let o=this.getStyle(r);return o||(o=new U({code:"",url:r,prefetch:!!e.getAttribute("prefetch"),preload:!!e.getAttribute("preload"),fromHtml:!n}),this.styles.set(r,o)),!n&&t.replaceChild(s,e),{replace:s,style:o}}return o&&["prefetch","preload","prerender","icon","apple-touch-icon"].includes(o)?(s=document.createComment(`【bk-weweb】style with rel=${o}${r?` & href=${r}`:""}`),!n&&t.removeChild(e),{replace:s}):(r&&e.setAttribute("href",u(r,this.url)),{replace:e})}collectScript(e,t,n=!1){if(e.hasAttribute("ignore")||e.hasAttribute("type")&&!["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"].includes(e.type)||!r&&"module"===e.type)return;let o=null;if(e.hasAttribute("exclude"))return o=document.createComment("【bk-weweb】script element with exclude attribute is removed"),!n&&t.replaceChild(o,e),{replace:o};let s=e.getAttribute("src");if(s){s=u(s,this.url);let r=this.getScript(s);return r||(r=new Y({code:"",async:e.hasAttribute("async"),defer:e.defer||"module"===e.type,isModule:"module"===e.type,url:s,fromHtml:!n}),this.scripts.set(s,r)),o=document.createComment(`【bk-weweb】script with src='${s}'`),!n&&t.replaceChild(o,e),{replace:o,script:r}}if(e.textContent){const s=m(),r=new Y({code:e.textContent,async:!1,defer:"module"===e.type,isModule:"module"===e.type,url:s,fromHtml:!n});return this.scripts.set(s,r),o=document.createComment("【bk-weweb】script with texcontent"),!n&&t.replaceChild(o,e),{replace:o,script:r}}return{replace:e}}setScript(e,t){this.scripts.set(e,new Y(t))}getScript(e){return this.scripts.get(e)}setStyle(e,t){this.styles.set(e,t)}getStyle(e){return this.styles.get(e)}}const Z=new class{cache;baseSource;constructor(){this.cache=new Map,this.baseSource=new z(location.href)}get hasActiveApp(){return Array.from(this.cache.values()).some((e=>e.status!==a.UNMOUNT))}setApp(e){this.cache.set(e.appCacheKey,e)}getApp(e){if(!e)return;const t=this.cache.get(e);return t||Array.from(this.cache.values()).find((t=>t.name===e))}getCacheHtml(e){const t=Array.from(this.cache.values()).find((t=>t.url===e));return t&&t.source?.rawHtml||""}getCacheStyle(e){let t=this.baseSource.getStyle(e);if(t)return;return Array.from(this.cache.values()).some((n=>(t=n.source?.getStyle(e),!!t))),t}getCacheScript(e){let t=this.baseSource.getScript(e);if(t)return;return Array.from(this.cache.values()).some((n=>(t=n.source?.getScript(e),!!t))),t}deleteApp(e){this.cache.delete(e)}setBaseAppStyle(e,t){this.baseSource.setStyle(e,t)}setBaseAppScript(e,t){this.baseSource.setScript(e,t)}};window.__getAppOrInstance__=function(e){return e?Z.getApp(e):Z};const{document:Q}=window,{createElement:X,querySelector:ee,querySelectorAll:te,getElementById:ne,getElementsByClassName:oe,getElementsByTagName:se,getElementsByName:re}=Document.prototype;function ie(){Document.prototype.createElement=X,Document.prototype.querySelector=ee,Document.prototype.querySelectorAll=te,Document.prototype.getElementById=ne,Document.prototype.getElementsByClassName=oe,Document.prototype.getElementsByTagName=se,Document.prototype.getElementsByName=re}const{setAttribute:ce}=Element.prototype,{appendChild:ae,removeChild:le,append:he}=HTMLBodyElement.prototype,{appendChild:pe,removeChild:ue,insertBefore:de}=HTMLHeadElement.prototype,me=document.head;function fe(){Element.prototype.setAttribute=function(e,t){const n=this.tagName.toLocaleUpperCase();(["src","srcset"].includes(e)&&["SCRIPT","IMG"].includes(n)||"href"===e&&["LINK"].includes(n))&&this.__BK_WEWEB_APP_KEY__&&Z.getApp(this.__BK_WEWEB_APP_KEY__||"")?ce.call(this,e,u(t,Z.getApp(this.__BK_WEWEB_APP_KEY__).url)):ce.call(this,e,t)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&k(e)?pe.call(me,e):K(this,e,ae)},HTMLBodyElement.prototype.append=function(...e){let t=0;const{length:n}=e;for(;t<n;){if(e[t].__KEEP_ALIVE__&&k(e[t]))return pe.call(me,e[t]);K(this,e[t],ae),t+=1}},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(e,t){return function(e,t,n,o){if(t.__BK_WEWEB_APP_KEY__){const s=Z.getApp(t.__BK_WEWEB_APP_KEY__);if(s?.container){const r=k(t)&&s.keepAlive&&!(s.container instanceof ShadowRoot),i=r?document.head:s?.container,c=W(e,t,s);return r&&(c.__KEEP_ALIVE__=s.appCacheKey,c.setAttribute("data-from",s.name),c.setAttribute("data-keep-alive","true")),n&&!i.contains(n)?D.call(i,c):o.call(i,c,n)}}return o.call(e,t,n)}(this,e,t,de)},HTMLBodyElement.prototype.removeChild=function(e){const t=Z.getApp(e.__BK_WEWEB_APP_KEY__);if(t?.container?.contains(e)){return le.call(t.container,e)}return this.contains(e)?le.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild}function ye(){n(null),Element.prototype.setAttribute=ce,HTMLBodyElement.prototype.appendChild=ae,HTMLBodyElement.prototype.append=he,HTMLBodyElement.prototype.removeChild=le,HTMLHeadElement.prototype.appendChild=pe,HTMLHeadElement.prototype.insertBefore=de,HTMLHeadElement.prototype.removeChild=ue}function we(){fe(),function(){function e(e){const n=t();return n&&e&&!w(e)&&Q===this?n?.container?.querySelector(e)??null:ee.call(this,e)}function n(e){const n=t();return n&&e&&!w(e)&&Q===this?n?.container?.querySelectorAll(e)??[]:te.call(this,e)}Document.prototype.createElement=function(e,n){const o=X.call(this,e,n),s=t();return s&&(o.__BK_WEWEB_APP_KEY__=s.appCacheKey),o},Document.prototype.querySelector=e,Document.prototype.querySelectorAll=n,Document.prototype.getElementById=function(n){if(!t())return ne.call(this,n);try{return e.call(this,`#${n}`)}catch{return ne.call(this,n)}},Document.prototype.getElementsByClassName=function(e){if(!t())return oe.call(this,e);try{return n.call(this,`.${e}`)}catch{return oe.call(this,e)}},Document.prototype.getElementsByTagName=function(e){const o=t();if(!o||w(e)||!o?.showSourceCode&&"script"===e.toLocaleLowerCase())return se.call(this,e);try{return n.call(this,e)}catch{return se.call(this,e)}},Document.prototype.getElementsByName=function(e){if(!t())return re.call(this,e);try{return n.call(this,`[name=${e}]`)}catch{return re.call(this,e)}}}()}async function Ee(e){return"app"===e.mode?await Ae(e):"js"===e.mode?await _e(e):await Ae(e)}async function Ae(e){let t=Z.getApp(e.id);return t||(t=new G(e),Z.setApp(t)),await t.start(),t}function _e(e){return new Promise((t=>{let n=Z.getApp(e.id);if(n)if([a.UNSET,a.MOUNTING].includes(n.status)){const e=setInterval((()=>{[a.ERROR,a.MOUNTED].includes(n.status)&&(t(n),clearInterval(e))}),300)}else t(n);else n=new I(e),Z.setApp(n),n.start().then((()=>t(n)))}))}function Se(e){const t=Z.getApp(e);t?.status!==a.UNMOUNT&&t?.unmount(),Z.hasActiveApp||(ye(),ie())}function ge(e,t,n){const o=Z.getApp(e);o?.status===a.DEACTIVATED&&o.keepAlive?h((()=>o?.activated(t,n))):h((()=>o?.mount(t,n)))}function be(e){const t=Z.getApp(e);t&&[a.ACTIVATED,a.MOUNTED].includes(t.status)&&(t.keepAlive?t.deactivated():t.unmount()),Z.hasActiveApp||(ye(),ie())}var Ce;!function(e){e.URL="url",e.showSourceCode="showSourceCode",e.scopeLocation="scopeLocation",e.setShodowDom="setShodowDom",e.mode="mode",e.data="data",e.id="id",e.scopeJs="scopeJs",e.scopeCss="scopeCss",e.keepAlive="keepAlive"}(Ce||(Ce={}));class ve extends HTMLElement{static get observedAttributes(){return[Ce.URL]}connected=!1;appUrl="";get appKey(){return this.getAttribute(Ce.id)||this.getAttribute(Ce.URL)}get appData(){if(this.hasAttribute(Ce.data))try{return JSON.parse(this.getAttribute(Ce.data))}catch{}return{}}get appProps(){return"js"===this.getAttribute("mode")?{url:this.getAttribute(Ce.URL),mode:"js",id:this.appKey,data:this.appData,container:this.shadowRoot??this,showSourceCode:this.getBooleanAttr(Ce.showSourceCode),scopeCss:this.getBooleanAttr(Ce.scopeCss)&&!this.getBooleanAttr(Ce.setShodowDom),scopeJs:this.getBooleanAttr(Ce.scopeJs),keepAlive:this.getBooleanAttr(Ce.keepAlive)}:{url:this.getAttribute(Ce.URL),mode:"app",id:this.appKey,data:this.appData,container:this.shadowRoot??this,scopeLocation:this.getBooleanAttr(Ce.scopeLocation),showSourceCode:this.getBooleanAttr(Ce.showSourceCode),scopeCss:!this.getBooleanAttr(Ce.setShodowDom),scopeJs:!this.getBooleanAttr(Ce.scopeJs),keepAlive:this.getBooleanAttr(Ce.keepAlive)}}async connectedCallback(){we(),this.getBooleanAttr(Ce.setShodowDom)&&!this.shadowRoot&&this.attachShadow({mode:"open",delegatesFocus:!0}),await Ee(this.appProps),ge(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?be(this.appKey):Se(this.appKey)}attributeChangedCallback(e,t,n){e===Ce.URL&&this[e]!==n&&(this.appUrl=n,(this.connected||Z.getApp(this.appKey))&&this.handleAttributeChanged())}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr(Ce.setShodowDom)&&this.attachShadow({mode:"open"});const e=Z.getApp(this.appKey);!e||e.url!==this.appUrl||!e.isPreLoad&&e.status!==a.UNMOUNT?await Ee(this.appProps):ge(this.appKey,this.shadowRoot??this)}getBooleanAttr(e){return this.hasAttribute(e)?"false"!==this.getAttribute(e):void 0}}class Le{fetchSource;webcomponentTag="bk-weweb";constructor(){window.customElements.get("bk-weweb")||window.customElements.define("bk-weweb",ve)}start(e){"function"==typeof e?.fetchSource&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||"bk-weweb",this.setWebComponentTag()}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,ve)}}const Te=new Le;exports.IframeApp=Le,exports.activated=ge,exports.beforeLoad=we,exports.deactivated=be,exports.default=Te,exports.load=Ee,exports.loadApp=Ae,exports.loadInstance=_e,exports.mount=function(e,t,n){const o=Z.getApp(e);h((()=>o?.mount(t,n)))},exports.preLoadApp=function(e){f((()=>Ae({...e,isPreLoad:!0})))},exports.preLoadInstance=function(e){f((()=>_e({...e,isPreLoad:!0})))},exports.preLoadSource=function(e){f((()=>{!async function(e){const{collectScript:t,collectStyle:n}=await q(e),o=[];t&&Array.from(t.entries()).forEach((e=>{o.push(e[1].getCode()),Z.setBaseAppScript(...e)})),n&&Array.from(n.entries()).forEach((e=>{o.push(e[1].getCode()),Z.setBaseAppStyle(...e)})),await Promise.all(o).catch((e=>{throw e}))}(e)}))},exports.unload=function(e){Z.deleteApp(e)},exports.unmount=Se;
2
2
  //# sourceMappingURL=index.min.js.map