@blueking/bk-weweb 0.0.2-5.beta.13 → 0.0.2-5.beta.14
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/collect-source.js +13 -0
- package/dist/collect-source.js.map +1 -1
- package/dist/index.esm.js +13 -0
- package/dist/index.esm.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
- package/typings/mode/app.d.ts +1 -0
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).bkIframe={})}(this,(function(e){"use strict";var t;!function(e){e.ACTIVATED="ACTIVATED",e.DEACTIVATED="DEACTIVATED",e.ERROR="ERROR",e.LOADED="LOADED",e.LOADING="LOADING",e.MOUNTED="MOUNTED",e.MOUNTING="MOUNTING",e.UNMOUNT="UNMOUNT",e.UNSET="UNSET"}(t||(t={}));let n=null;function o(){return n}function r(e){n=e}const i=new Map;Object.getOwnPropertyNames(window).forEach((e=>{!i.has(e)&&e.match(/^[A-Z]/)&&"function"==typeof window[e]&&window[e].toString().includes("[native code]")&&i.set(e,!0)}));const s="id";var c;!function(e){e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.STYLE_RULE=1]="STYLE_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(c||(c={}));const a=e=>Promise.resolve().then(e);function l(e){return e.startsWith("//")?`${location.protocol}${e}`:e.startsWith("http")?e:`${location.protocol}//${e}`}function d(e,t){if(!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e))return e;const{origin:n,pathname:o}=new URL(l(t));return new URL(e,`${n}${o}`.replace(/\.(\w+)$/,"/")).toString()}function u(){return`inline-${p(16)}`}const h=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout((()=>{e({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-t))})}),1)},p=(e,t="abcdefghijklmnopqrstuvwxyz0123456789")=>{let n="";for(let o=0;o<e;o++)n+=t[parseInt((Math.random()*t.length).toString(),10)];return n},f=e=>{if(!e)return!1;if(e.match(/\.js$/))return!1;const{pathname:t}=new URL(l(e));return!t.match(/\.js$/)},m=["currentTarget","srcElement","target"];function y(e,t="custom"){return Object.defineProperties(new CustomEvent(t),m.reduce(((t,n)=>(t[n]={get:()=>e},t)),{}))}function w(e){const t=y(e,"load");"function"!=typeof e.onload?e.dispatchEvent(t):e.onload(t)}function E(e){const t=y(e,"error");"function"!=typeof e.onerror?e.dispatchEvent(t):e.onerror(t)}function _(e,t={}){return Pe.fetchSource?Pe.fetchSource(e,t):window.fetch(e,t).then((e=>e.text()))}class A{code="";fromHtml;initial;prefetch=!1;preload=!1;scoped;scopedCode="";url;constructor({code:e,fromHtml:t,initial:n,prefetch:o,preload:r,url:i}){this.scoped=!1,this.code=e,this.prefetch=o??!1,this.preload=r??!1,this.url=i,this.fromHtml=t,this.initial=n??!1}commonScoped(e,t){if(!t.scopeCss||t.container instanceof ShadowRoot){const n=this.resetUrlHost(e.textContent||this.code||"",t.url,this.url);if(n&&t.container instanceof ShadowRoot){let e="";n.match(/@font-face\s*\{[^}]+\}/g)?.forEach((t=>{e+=`${t}\n`}));const o=t.sandBox?.rawDocument;if(o&&e){const t=o.createElement("style");t.setAttribute("type","text/css"),t.setAttribute("powered-by","bk-weweb"),t.textContent=e,o?.head?.append(t)}}e.textContent=n}else{const n=new CSSStyleSheet({disabled:!0});n.replaceSync(e.textContent||this.code);const o=Array.from(n?.cssRules??[]),r=`#${t.name}`,i=this.scopeRule(o,r),s=this.resetUrlHost(i,t.url,this.url);e.textContent=s,this.scopedCode=s}this.scoped=!0}createStyleElement(){const e=document.createElement("style");return e.__BK_WEWEB_APP_KEY__&&delete e.__BK_WEWEB_APP_KEY__,e}async excuteCode(e){e.registerRunningApp();let t=this.createStyleElement();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}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&&Se.getCacheStyle(this.url)){const e=Se.getCacheStyle(this.url);t=e?.code||""}return t||(t=await _(this.url).catch((()=>""))),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot||!e.textContent||!Se.getBaseAppStyle(e.textContent))&&(e.textContent="",e.innerHTML="",e.setAttribute("linked-from-base","true"),!0)}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)=>{if(/^(data|blob):/.test(o)||/^(https?:)?\/\//.test(o))return e;if(/^((\.\.?\/)|[^/])/.test(o)&&n){const e=n.split("/");e.pop(),t=l(`${e.join("/")}/`)}return`url("${d(o,t)}")`}))}scopeRule(e,t){let n="";for(const o of e)switch(o.type){case c.STYLE_RULE:n+=this.scopeStyleRule(o,t);break;case c.MEDIA_RULE:n+=this.resetPackRule(o,t,"media");break;case c.SUPPORTS_RULE:n+=this.resetPackRule(o,t,"supports");break;default:n+=o.cssText}return n.replace(/^\s+/,"")}scopeStyleRule(e,t){const{cssText:n,selectorText:o}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(o))return n.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if("*"===o)return n.replace("*",`${t} *`);const r=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return n.replace(/^[\s\S]+{/,(e=>e.replace(/(^|,)([^,]+)/g,((e,n,o)=>r.test(o)?e.replace(r,t):`${n} ${t} ${o.replace(/^\s*/,"")}`))))}scopedLinkCSS(e,t){const n=this.createStyleElement();n.setAttribute("type","text/css");const o=!(!e.keepAlive||e.container instanceof ShadowRoot);O(n,e,o);const r=o?document.head:e.container;try{if(this.code)this.commonScoped(n,e),r?.prepend(n),t&&w(t);else if(t.getAttribute("href"))this.url=d(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&w(t),this.scoped=!0}));else{const o=new MutationObserver((()=>{t.href&&(o.disconnect(),this.url=d(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&w(t),this.scoped=!0})))}));o.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&E(t)}return n}scopedStyleCSS(e,t){const n=!(!e.keepAlive||e.container instanceof ShadowRoot);if(O(t,e,n),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;this.commonScoped(t,e)}else{const n=new MutationObserver((()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,e))}));n.observe(t,{attributes:!1,characterData:!0,childList:!0,subtree:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}}async function S(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?.append(...n):document.head.append(...n)}))}class b{state=t.UNSET;appCacheKey;container;data;initSource;isPreLoad=!1;keepAlive;name;sandBox;scopeCss=!0;scopeJs=!1;showSourceCode=!0;source;url;constructor(e){this.name=e.id!==e.url?e.id:p(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 me(this))}activated(e,n){if(this.isPreLoad=!1,this.state=t.ACTIVATED,this.container&&e){e instanceof Element&&e.setAttribute(s,this.name);const t=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{t.appendChild(e)})),e.appendChild(t),this.container=e,this.sandBox?.activeated();const o=this.source?.getScript(this.url);n?.(this,o?.exportInstance)}}deactivated(){this.state=t.DEACTIVATED,this.sandBox?.deactivated()}mount(e,n){this.isPreLoad=!1,this.container=e??this.container,this.state=t.MOUNTING,this.container instanceof HTMLElement&&this.container.setAttribute(s,this.name),this.container.innerHTML="";const o=document.createElement("div"),r=`${this.name}-wrapper`;o.setAttribute("id",r),this.source?.styles.size&&S(this,this.container),this.container.appendChild(o),this.sandBox?.activeated(),P(this).finally((()=>{this.state=t.MOUNTED;const e=this.source?.getScript(this.url);"function"==typeof e?.exportInstance?.render&&e.exportInstance.render(o,this.data),n?.(this,e?.exportInstance)}))}onError(){this.state=t.ERROR}onMount(){this.isPreLoad||(this.state=t.LOADED,this.mount())}registerRunningApp(){r(this),Promise.resolve().then((()=>r(null)))}async start(){this.source&&![t.ERROR,t.UNSET].includes(this.status)||(this.source=new Ae(this.url),await this.source.importEntery(this))}unmount(e){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&Se.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}}let g,C,v;class L{async=!1;code="";defer=!1;exportInstance;fromHtml;initial;isModule=!1;scoped;url;constructor({async:e,code:t,defer:n,fromHtml:o,initial:r,isModule:i,url:s}){this.code=t,this.async=e,this.defer=n,this.isModule=i,this.url=s,this.scoped=!1,this.fromHtml=o??!1,this.initial=r??!1}async excuteCode(e,t=!1){try{if(this.code||await this.getCode(e),e instanceof b){!function(e){C=void 0,g=C;for(const t in e)T(e,t)||(g?C||(C=t):g=t,v=t)}(e.scopeJs&&e.sandBox?.proxyWindow||window)}let n=this.code;if(n=this.transformCode(e),e.showSourceCode||this.isModule){const o=document.createElement("script");if(o.__BK_WEWEB_APP_KEY__&&delete o.__BK_WEWEB_APP_KEY__,e.registerRunningApp(),this.executeSourceScript(o,n),t)return o;const r=!(!e.keepAlive||e.container instanceof ShadowRoot),i=r?document.head:e.container;O(o,e,r),i.appendChild(o)}else if(this.executeMemoryScript(e,n),t)return document.createComment("【bk-weweb】dynamic script");if(e instanceof b){const t=e.scopeJs&&e.sandBox?.proxyWindow||window,n=function(e,t){let n,o,r=0;for(const i in e)if(!T(e,i)){if(0===r&&i!==g||1===r&&i!==C)return i;n?(v=i,o=t&&o||i):n=i===v,r+=1}return o}(t);n&&(this.exportInstance=t[n],e.scopeJs||delete t[n])}}catch(e){console.error("execute script code error",e)}}executeMemoryScript(e,t){try{const n=e instanceof we&&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)}}executeSourceScript(e,t){this.isModule?(e.src=this.url+"?key="+Date.now(),e.setAttribute("type","module")):e.textContent=t,this.url&&e.setAttribute("origin-src",this.url)}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&&Se.getCacheScript(this.url)&&(t=Se.getCacheScript(this.url)?.code||""),t||(t=await _(this.url).catch((e=>(console.error(`fetch script ${this.url} error`,e),"")))),this.code=t,t}setCode(e){this.code=e}transformCode(e){return e.sandBox?this.isModule?` with(window.${e.sandBox.windowSymbolKey}){\n ;${this.code}\n\n }`:e.showSourceCode?`;(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}}function T(e,t){return!e.hasOwnProperty(t)||!isNaN(t)&&t<e.length||"undefined"!=typeof navigator&&-1!==navigator.userAgent.indexOf("Trident")&&e[t]&&"undefined"!=typeof window&&e[t].parent===window}async function P(e){const t=Array.from(e.source.scripts.values()).filter((e=>e.fromHtml||e.initial)),n=t.filter((e=>!e.async&&!e.defer||e.isModule));await Promise.all(n.map((t=>t.getCode(e)))),await Promise.all(n.map((t=>t.excuteCode(e))));const o=[],r=[];t.forEach((t=>{(t.defer||t.async)&&(!t.code&&t.defer?o.push(t.excuteCode(e)):r.push(t.excuteCode(e)))})),await Promise.all([...r,...o]).catch((e=>{console.error(e)}))}const{appendChild:B}=HTMLBodyElement.prototype;function M(e,t,n){if(t instanceof HTMLStyleElement){if(t.hasAttribute("exclude"))return document.createComment("【bk-weweb】style with exclude attribute is ignored");if(t.textContent&&!(n.container instanceof ShadowRoot)&&Se.getBaseAppStyle(t.textContent))return document.createComment("【bk-weweb】style is effective in base app");if(!t.hasAttribute("ignore")){const e=new A({code:t.textContent||"",fromHtml:!1,url:""});n.source?.setStyle(u(),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 r=n.source.collectScript(t,e,!0);if(r?.replace&&B.call(n.container,r.replace),f(t.getAttribute("src")))return void n.container?.append(t);r?.script&&r.script.excuteCode(n),t.remove()}else if(t.textContent){o.disconnect();const e=new L({async:!1,code:t.textContent,defer:"module"===t.type,fromHtml:!1,isModule:"module"===t.type});n.source.scripts.set(u(),e);try{e.excuteCode(n)}catch(e){console.error(e)}finally{!e.isModule&&w(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 R(e){return e instanceof HTMLScriptElement||e instanceof HTMLStyleElement||e instanceof HTMLLinkElement}function x(e,t,n){if(t.__BK_WEWEB_APP_KEY__){const o=Se.getApp(t.__BK_WEWEB_APP_KEY__);if(o?.container){const r=M(e,t,o),i=R(t)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),s=i?document.head:o?.container;return O(r,o,i),n.call(s,r)}}return n.call(e,t)}function W(e,t,n,o){if(t.__BK_WEWEB_APP_KEY__){const r=Se.getApp(t.__BK_WEWEB_APP_KEY__);if(r?.container){const i=R(t)&&r.keepAlive&&!(r.container instanceof ShadowRoot),s=i?document.head:r?.container,c=M(e,t,r);return i&&O(c,r,i),n&&!s.contains(n)?B.call(s,c):o.call(s,c,n)}}return o.call(e,t,n)}function O(e,t,n){return n&&t&&(e.__KEEP_ALIVE__=t.appCacheKey,e.setAttribute("data-from",t.name),e.setAttribute("data-keep-alive","true")),e.setAttribute?.("powered-by","bk-weweb"),e}const{setAttribute:H}=Element.prototype,{append:K,appendChild:k,insertBefore:D,removeChild:N}=HTMLBodyElement.prototype,{appendChild:I,insertBefore:U,removeChild:$}=HTMLHeadElement.prototype,j=document.head;let V=!1;function Y(){r(null),Element.prototype.setAttribute=H,HTMLBodyElement.prototype.appendChild=k,HTMLBodyElement.prototype.append=K,HTMLBodyElement.prototype.removeChild=N,HTMLBodyElement.prototype.insertBefore=D,HTMLHeadElement.prototype.appendChild=I,HTMLHeadElement.prototype.insertBefore=U,HTMLHeadElement.prototype.removeChild=$,V=!1}function J(){V||(V=!0,Element.prototype.setAttribute=function(e,t){const n=this.tagName.toLocaleUpperCase();(["src","srcset"].includes(e)&&["IMG","SCRIPT"].includes(n)||"href"===e&&["LINK"].includes(n))&&this.__BK_WEWEB_APP_KEY__&&Se.getApp(this.__BK_WEWEB_APP_KEY__||"")?H.call(this,e,d(t,Se.getApp(this.__BK_WEWEB_APP_KEY__).url)):H.call(this,e,t)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&R(e)?I.call(j,e):x(this,e,k)},HTMLBodyElement.prototype.append=function(...e){e.forEach((e=>{if(e.__KEEP_ALIVE__&&R(e))return I.call(j,e);x(this,e,k)}))},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(e,t){return W(this,e,t,U)},HTMLBodyElement.prototype.insertBefore=function(e,t){return W(this,e,t,U)},HTMLBodyElement.prototype.removeChild=function(e){const t=Se.getApp(e.__BK_WEWEB_APP_KEY__);return t?.container?.contains(e)?N.call(t.container,e):this.contains(e)?N.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild)}function F(e,n,o){const r=Se.getApp(e);r?.status===t.DEACTIVATED&&r.keepAlive?a((()=>{J(),r.activated(n,o)})):r&&a((()=>{J(),r.mount(n,o)}))}function q(e){const n=Se.getApp(e);n&&[t.ACTIVATED,t.MOUNTED].includes(n.status)&&(n.keepAlive?n.deactivated():n.unmount()),Se.hasActiveApp||Y()}async function G(t){return J(),t.mode===e.WewebMode.INSTANCE?await Z(t):await z(t)}async function z(e){J();let t=Se.getApp(e.id);return t||(t=new we(e),Se.setApp(t)),await t.start(),t}function Z(e){return J(),new Promise((n=>{let o=Se.getApp(e.id);if(o)if([t.MOUNTING,t.UNSET].includes(o.status)){const e=setInterval((()=>{[t.ERROR,t.MOUNTED].includes(o.status)&&(n(o),clearInterval(e))}),300)}else n(o);else o=new b(e),Se.setApp(o),o.start().then((()=>n(o)))}))}function Q(e){const n=Se.getApp(e);n&&n.status!==t.UNMOUNT&&n.unmount(),Se.hasActiveApp||Y()}var X,ee;!function(e){e.data="data",e.id="id",e.keepAlive="keepAlive",e.mode="mode",e.scopeCss="scopeCss",e.scopeJs="scopeJs",e.scopeLocation="scopeLocation",e.setShodowDom="setShodowDom",e.showSourceCode="showSourceCode",e.url="url"}(X||(X={}));class te extends HTMLElement{appUrl="";connected=!1;static get observedAttributes(){return[X.url]}getBooleanAttr(e){return this.hasAttribute(e)||this.hasAttribute(e.replace(/([A-Z])/g,"-$1").toLocaleLowerCase())?"false"!==this.getAttribute(e):void 0}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr(X.setShodowDom)&&this.attachShadow({mode:"open"});const e=Se.getApp(this.appKey);!e||e.url!==this.appUrl||!e.isPreLoad&&e.status!==t.UNMOUNT?await G(this.appProps):F(this.appKey,this.shadowRoot??this)}attributeChangedCallback(e,t,n){if(e!==X.url||this[e]===n||!this.connected)return;this.appUrl=n;const o=Se.getApp(this.appKey);(this.connected||o)&&this.handleAttributeChanged()}async connectedCallback(){this.getBooleanAttr(X.setShodowDom)&&!this.shadowRoot&&this.attachShadow({delegatesFocus:!1,mode:"open"}),await G(this.appProps),F(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?q(this.appKey):Q(this.appKey)}get appData(){if(this.hasAttribute(X.data))try{return JSON.parse(this.getAttribute(X.data))}catch{}return{}}get appKey(){return this.getAttribute(X.id)||this.getAttribute(X.url)}get appProps(){return this.getAttribute("mode")===e.WewebMode.INSTANCE?{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(X.keepAlive),mode:e.WewebMode.INSTANCE,scopeCss:this.getBooleanAttr(X.scopeCss)&&!this.getBooleanAttr(X.setShodowDom),scopeJs:this.getBooleanAttr(X.scopeJs),showSourceCode:this.getBooleanAttr(X.showSourceCode),url:this.getAttribute(X.url)}:{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(X.keepAlive),mode:e.WewebMode.APP,scopeCss:!this.getBooleanAttr(X.setShodowDom),scopeJs:!this.getBooleanAttr(X.scopeJs),scopeLocation:this.getBooleanAttr(X.scopeLocation),showSourceCode:this.getBooleanAttr(X.showSourceCode),url:this.getAttribute(X.url)}}}e.WewebMode=void 0,(ee=e.WewebMode||(e.WewebMode={})).APP="app",ee.CONFIG="config",ee.INSTANCE="js";const ne=["System","__cjsWrapper","production"!==process.env.NODE_ENV?"__REACT_DEVTOOLS_GLOBAL_HOOK__":""],oe=["__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],re=["window","self","globalThis"],ie=["location","history"],se={__bk_pop_manager:!0,__bk_zIndex_manager:!0,i18n:!0},ce="production"!==process.env.NODE_ENV?{__DEV__:!0,__VUE_DEVTOOLS_GLOBAL_HOOK__:!0,__VUE_DEVTOOLS_HOOK_REPLAY__:!0,__VUE_DEVTOOLS_PLUGINS__:!0,__VUE_I18N_FULL_INSTALL__:!0,__VUE_I18N_LEGACY_API__:!0,__VUE_OPTIONS_API__:!0,"__core-js_shared__":!0,webpackChunkapm:!0,webpackChunkpc:!0,webpackChunktrace:!0,webpackJsonp:!0,...se}:se,ae=["body","html","head"],le=(e,t)=>{const n=new Proxy({},{get(n,o){if(t.container instanceof ShadowRoot){const e=t.container[o];if("function"==typeof e)return e.bind(t.container);if(void 0!==e)return e}const r=Reflect.get(e.body,o);return"function"==typeof r?r.bind(e.body):r},set:(n,o,r)=>t.container instanceof ShadowRoot?(t.container[o]=r,!0):(Reflect.set(e.body,o,r),!0)});function o(n,o){const r=e.createElement(n,o);return r.__BK_WEWEB_APP_KEY__=t.appCacheKey,r}function r(o){if(o===n)return t.container instanceof ShadowRoot?t.container:e.body;if(ae.includes(o))return t?.container instanceof ShadowRoot?t?.container:e.querySelector.call(this,o);try{return t?.container?.querySelector(o)??null}catch{return null}}function i(n){return ae.includes(n)?t?.container instanceof ShadowRoot?t?.container:e.querySelector(n):t?.container?.querySelectorAll(n)??[]}function s(t){return r.call(e,`#${t}`)}function c(e){return i(`.${e}`)}function a(n){return ae.includes(n)||!t?.showSourceCode&&"script"===n.toLocaleLowerCase()?e.getElementsByTagName(n):i(n)}function l(e){return i(`[name=${e}]`)}return new Proxy({},{get(t,d){if("createElement"===d)return o.bind(e);if("querySelector"===d)return r.bind(e);if("querySelectorAll"===d)return i.bind(e);if("getElementById"===d)return s.bind(e);if("getElementsByClassName"===d)return c.bind(e);if("getElementsByTagName"===d)return a.bind(e);if("getElementsByName"===d)return l.bind(e);if("body"===d)return n;const u=Reflect.get(e,d);return"function"==typeof u?u.bind(e):u}})};const de=/^class\b/,ue=/^function\b\s[A-Z].*/,he=new WeakMap;const pe=new WeakMap;function fe(e,t){if(pe.has(t))return pe.get(t);if("function"==typeof t&&!function(e){if(e.prototype?.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1)return!0;if(he.has(e))return he.get(e);const t=ue.test(e.toString())||de.test(e.toString());return he.set(e,t),t}(t)){const n=Function.prototype.bind.call(t,e);for(const e in t)n[e]=t[e];if(t.hasOwnProperty("prototype")&&!n.hasOwnProperty("prototype")&&Object.defineProperty(n,"prototype",{enumerable:!1,value:t.prototype,writable:!0}),"function"==typeof t.toString){const e=t.hasOwnProperty("toString")&&!n.hasOwnProperty("toString"),o=n.toString===Function.prototype.toString;if(e||o){const o=Object.getOwnPropertyDescriptor(e?t:Function.prototype,"toString");Object.defineProperty(n,"toString",{...o,...o?.get?null:{value:()=>t.toString()}})}}return pe.set(t,n),n}return t}class me{app;active=!1;inRawWindowKeySet=new Set;resetDocumentAndBodyEvent;resetWindowFunction;sameRawWindowKeySet=new Set;fakeWindow;proxyDocument;proxyWindow;rawDocument;rawWindow;windowSymbolKey;constructor(e){this.app=e;const t=new Set,n=window;this.rawWindow=n,this.rawDocument=le(document,e);const o=Object.create({});o.__BK_WEWEB_APP_KEY__=e.appCacheKey,o.__POWERED_BY_BK_WEWEB__=!0,o.rawDocument=document,o.rawWindow=n,o.__proto__=Window,this.fakeWindow=o;const{resetWindowFunction:r}=function(e){const t=new Map,n=[],o=window,{addEventListener:r,clearInterval:i,removeEventListener:s,setInterval:c}=window;return e.addEventListener=function(e,n,i){t.set(e,[...t.get(e)||[],n]),r.call(o,e,n,i)},e.removeEventListener=function(e,n,r){const i=t.get(e);if(i?.length){const e=i.indexOf(n);e>-1&&i.splice(e,1)}s.call(o,e,n,r)},e.setInterval=function(e,t,...r){const i=c.call(o,e,t,...r);return n.push(i),i},e.clearInterval=function(e){const t=n.indexOf(e);t>-1&&n.splice(t,1),i.call(o,e)},{resetWindowFunction:function(){t.size&&(t.forEach(((e,t)=>{e.forEach((e=>s.call(o,t,e)))})),t.clear()),n.length&&n.forEach((e=>{i.call(o,e)}))}}}(this.fakeWindow);this.resetWindowFunction=r,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${p(10)}__`,this.proxyWindow=new Proxy(this.fakeWindow,{defineProperty:(e,o,r)=>t.has(o)?Reflect.defineProperty(n,o,r):Reflect.defineProperty(e,o,r),deleteProperty:(e,t)=>!e.hasOwnProperty(t)||(this.sameRawWindowKeySet.has(t)&&this.sameRawWindowKeySet.delete(t),this.inRawWindowKeySet.has(t)&&Reflect.deleteProperty(n,t),Reflect.deleteProperty(e,t)),get:(t,o)=>{if(i.has(o)||o===Symbol.unscopables)return n[o];if(ce[o])return this.fakeWindow[o];if(re.includes(o))return this.proxyWindow;if("document"===o)return e.registerRunningApp(),this.rawDocument;if("eval"===o)return e.registerRunningApp(),eval;if(ie.includes(o)&&this.app instanceof we&&this.app.iframe&&this.app.scopeLocation)return this.app.iframe.contentWindow?.[o];if("hasOwnProperty"===o)return e=>this.fakeWindow.hasOwnProperty(e)||n.hasOwnProperty(e);if("top"===o||"parent"===o)return n===n.parent?this.proxyWindow:Reflect.get(n,o);if("getComputedStyle"===o)return(e,t)=>e instanceof Element?n.getComputedStyle(e,t):n.getComputedStyle(document.body,t);if(Reflect.has(t,o)||oe.includes(o))return Reflect.get(t,o);const r=Reflect.get(n,o);return fe(n,r)},getOwnPropertyDescriptor:(e,o)=>{if(e.hasOwnProperty(o))return Object.getOwnPropertyDescriptor(e,o);if(n.hasOwnProperty(o)){t.add(o);const e=Object.getOwnPropertyDescriptor(n,o);return e&&!e.configurable&&(e.configurable=!0),e}},has:(e,t)=>i.has(t)||t in e||t in n,ownKeys:e=>Array.from(new Set(Reflect.ownKeys(n).concat(Reflect.ownKeys(e)))),set:(e,t,o)=>{if(this.active){if(ie.includes(t)&&this.app instanceof we&&this.app.iframe&&this.app.scopeLocation)return Reflect.set(this.app.iframe.contentWindow,t,o);if("location"===t)Reflect.set(n,t,o);else if(e.hasOwnProperty(t)||!n.hasOwnProperty(t)||oe.includes(t))Reflect.set(e,t,o),this.sameRawWindowKeySet.add(t);else{const r=Object.getOwnPropertyDescriptor(n,t),{configurable:i,enumerable:s,writable:c}=r;c&&(Object.defineProperty(e,t,{configurable:i,enumerable:s,value:o,writable:c}),this.sameRawWindowKeySet.add(t))}!ne.includes(t)||Reflect.has(n,t)||oe.includes(t)||(Reflect.set(n,t,o),this.inRawWindowKeySet.add(t))}return!0}}),e.showSourceCode&&(n[this.windowSymbolKey]=this.proxyWindow)}activeated(e){if(!this.active){this.active=!0,this.rawDocument=le(document,this.app),this.fakeWindow.__BK_WEWEB_DATA__=e??{};const{resetDocumentAndBodyEvent:t}=function(){const{addEventListener:e,removeEventListener:t}=window.document,{addEventListener:n,removeEventListener:r}=window.document.body,i=new Map;return document.addEventListener=function(t,n,r){const s=o();if(s?.keepAlive){const e=i.get(t)||[];i.set(t,[...e,n])}e.call(s?.container instanceof ShadowRoot?s.container:this,t,n,r)},document.body.addEventListener=document.addEventListener,document.removeEventListener=function(e,n,r){const s=o();if(s?.keepAlive){const t=i.get(e)||[];t.length&&t.some((e=>e===n))&&t.splice(t.indexOf(n),1)}t.call(s?.container instanceof ShadowRoot?s.container:this,e,n,r)},document.body.removeEventListener=document.removeEventListener,{resetDocumentAndBodyEvent:function(){const s=o();s?.keepAlive&&i.values()&&Array.from(i.entries()).forEach((([e,t])=>{t?.forEach((t=>{document.removeEventListener.call(document,e,t)}))})),document.addEventListener=e,document.body.addEventListener=n,document.removeEventListener=t,document.body.removeEventListener=r,i.clear()}}}();this.resetDocumentAndBodyEvent=t}}deactivated(){this.active&&(this.active=!1,this.resetWindowFunction(),this.inRawWindowKeySet.forEach((e=>Reflect.deleteProperty(window,e))),this.inRawWindowKeySet.clear(),this.resetDocumentAndBodyEvent?.())}}const ye="about:blank";class we{state=t.UNSET;container;data;iframe=null;initSource;isModuleApp=!1;isPreLoad=!1;keepAlive;mode=e.WewebMode.APP;name;sandBox;scopeCss;scopeJs;scopeLocation;showSourceCode;source;url;constructor(t){this.name=t.id!==t.url?t.id:p(5),this.mode=t.mode??e.WewebMode.APP,this.container=t.container??void 0,this.showSourceCode=t.showSourceCode??!1,this.url=t.url,this.data=t.data||{},this.scopeJs=t.scopeJs??!0,this.scopeCss=t.scopeCss??!0,this.scopeLocation=t.scopeLocation??!1,this.isPreLoad=t.isPreLoad??!1,this.keepAlive=t.keepAlive??!1,this.initSource=t.initSource??[],this.scopeJs&&(this.sandBox=new me(this)),this.container instanceof HTMLElement&&this.container.setAttribute(s,this.name)}activated(e,n){this.isPreLoad=!1,this.state=t.ACTIVATED;const o=this;if(e&&this.container){e instanceof Element&&e.setAttribute(s,this.name);const t=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{e.__BK_WEWEB_APP_KEY__=this.appCacheKey,Object.defineProperties(e,{ownerDocument:{get:()=>o.sandBox?.rawDocument}}),t.appendChild(e)})),e.innerHTML="",e.appendChild(t),this.container=e,this.sandBox?.activeated(this.data),n?.(this)}}createIframe(){return new Promise((e=>{const t=document.createElement("iframe"),n=new URL(l(this.url)),o=navigator.userAgent.indexOf("Chrome")>-1;if(t.setAttribute("src",`${o?ye:location.origin}${n.pathname||"/"}${n.search}${n.hash}`),t.style.cssText="display: none;",document.body.appendChild(t),o)setTimeout((()=>e(t)),0);else{const n=setInterval((()=>{t.contentWindow&&t.contentWindow.location.href!==ye&&(t.contentWindow.stop(),t.contentDocument.body.parentElement.innerHTML="<head></head><body></body>",clearInterval(n),e(t))}),0)}}))}deactivated(){this.state=t.DEACTIVATED,this.sandBox?.deactivated()}mount(e,n){this.isPreLoad=!1,this.container=e??this.container,this.state=t.MOUNTING;const o=this;if(this.container){this.container instanceof Element&&this.container.setAttribute(s,this.name);const e=this.source.html.cloneNode(!0),r=document.createDocumentFragment();Array.from(e.childNodes).forEach((e=>{e.__BK_WEWEB_APP_KEY__=this.appCacheKey,Object.defineProperties(e,{ownerDocument:{get:()=>o.sandBox?.rawDocument}}),r.appendChild(e)})),this.container.innerHTML="",this.container.appendChild(r),this.sandBox?.activeated(this.data),P(this).finally((()=>{this.state=t.MOUNTED,n?.(this)}))}}onError(){this.state=t.ERROR}onMount(){this.isPreLoad||(this.state=t.LOADED,this.mount())}registerRunningApp(){r(this),Promise.resolve().then((()=>r(null)))}async start(){if(!this.source||[t.ERROR,t.UNSET].includes(this.status)){if(this.state=t.LOADING,this.scopeLocation||this.isModuleApp){const e=await this.createIframe();this.iframe=e}this.source=new Ae(this.url),await this.source.importEntery(this)}}unmount(e=!1){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&Se.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}get appCacheKey(){return this.url}get status(){return this.state}set status(e){this.state=e}}async function Ee(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=Se.getCacheScript(e);n.set(e,new L({async:!1,code:t?.code||"",defer:!1,fromHtml:!1,initial:!0,isModule:!1,url:e}))}else if(t.pathname.match(/\.css$/)){const t=Se.getCacheStyle(e);o.set(e,new A({code:t?.code||"",fromHtml:!0,initial:!0,prefetch:!1,preload:!1,url:e}))}}catch{console.error(`【bk-weweb】: ${e} is invalid URL`)}})),{collectScript:n,collectStyle:o}}const _e=["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"];class Ae{url;html=null;rawHtml;scripts;styles;constructor(e){this.url=e,this.scripts=new Map,this.styles=new Map}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 r,i=e.getAttribute("href");if("stylesheet"===o&&i){i=d(i,this.url),r=document.createComment(`【bk-weweb】style with href=${i}`);let o=this.getStyle(i);return o||(o=new A({code:"",fromHtml:!n,prefetch:!!e.getAttribute("prefetch"),preload:!!e.getAttribute("preload"),url:i}),this.styles.set(i,o)),!n&&t.replaceChild(r,e),{replace:r,style:o}}return o&&["apple-touch-icon","icon","prefetch","preload","prerender"].includes(o)?(r=document.createComment(`【bk-weweb】style with rel=${o}${i?` & href=${i}`:""}`),!n&&t.removeChild(e),{replace:r}):(i&&e.setAttribute("href",d(i,this.url)),{replace:e})}collectScript(e,t,n=!1){if(e.hasAttribute("ignore")||"module"!==e.type&&f(e.getAttribute("src"))||e.hasAttribute("type")&&!_e.includes(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 r=e.getAttribute("src");if(r){r=d(r,this.url);let i=this.getScript(r);return i||(i=new L({async:e.hasAttribute("async"),code:"",defer:e.defer||"module"===e.type,fromHtml:!n,isModule:"module"===e.type,url:r}),this.scripts.set(r,i)),o=document.createComment(`【bk-weweb】script with src='${r}'`),!n&&t.replaceChild(o,e),{replace:o,script:i}}if(e.textContent){const r=u(),i=new L({async:!1,code:e.textContent,defer:"module"===e.type,fromHtml:!n,initial:!1,isModule:"module"===e.type,url:r});return this.scripts.set(r,i),o=document.createComment("【bk-weweb】script with texcontent"),!n&&t.replaceChild(o,e),{replace:o,script:i}}return{replace:e}}collectScriptAndStyle(e){const t=Array.from(e.querySelectorAll("link"));t?.forEach((e=>{this.collectLink(e,e.parentElement)}));const n=Array.from(e.querySelectorAll("style"));n?.forEach((e=>{e.hasAttribute("exclude")||e.hasAttribute("ignore")||(this.styles.set(u(),new A({code:e.textContent||"",fromHtml:!0,url:""})),e.remove())}));const o=Array.from(e.querySelectorAll("script"));o?.forEach((e=>{this.collectScript(e,e.parentElement)}));const r=Array.from(e.querySelectorAll("meta"));r?.forEach((e=>{e.parentElement.removeChild(e)}));const i=Array.from(e.querySelectorAll("img"));i?.forEach((e=>{e.hasAttribute("src")&&e.setAttribute("src",d(e.getAttribute("src"),this.url))}))}getScript(e){return this.scripts.get(e)}getStyle(e){return this.styles.get(e)||Array.from(this.styles.values()).find((t=>t.code===e))}async importEntery(e){if(e.initSource?.length){const{collectScript:t,collectStyle:n}=await Ee(e.initSource);t&&(this.scripts=t),n&&(this.styles=n)}e instanceof we?await this.importHtmlEntry(e):e instanceof b&&await this.importInstanceEntry()}async importHtmlEntry(e){let t=Se.getCacheHtml(this.url);if(!t&&(t=await _(l(this.url),{cache:"no-cache"}),!t))return console.error("load app entry error, pleace check"),Promise.reject();this.rawHtml=t;const n=document.createElement("div");n.__BK_WEWEB_APP_KEY__&&delete n.__BK_WEWEB_APP_KEY__,n.innerHTML=t.replace(/<\/?head>/gim,"").replace(/<\/?body>/i,""),this.collectScriptAndStyle(n),await S(e,n),this.html=n}async importInstanceEntry(){let e=Se.getCacheScript(this.url)?.code;if(e||(e=await _(this.url,{cache:"no-cache"})),!e)return console.error("load app entry error, pleace check"),Promise.reject();this.scripts.set(this.url,new L({async:!1,code:e,defer:!1,fromHtml:!0,isModule:!!this.url.match(/\.ts$/),url:this.url}))}setScript(e,t){this.scripts.set(e,t instanceof L?t:new L(t))}setStyle(e,t){this.styles.set(e,t)}}const Se=new class{baseSource;cache;constructor(){this.cache=new Map,this.baseSource=new Ae(location.href)}deleteApp(e){this.cache.delete(e)}getApp(e){if(!e)return;const t=this.cache.get(e);return t||Array.from(this.cache.values()).find((t=>t.name===e))}getBaseAppStyle(e){return this.baseSource.getStyle(e)}getCacheHtml(e){const t=Array.from(this.cache.values()).find((t=>t.url===e));return t&&t.source?.rawHtml||""}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}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}setApp(e){this.cache.set(e.appCacheKey,e)}setBaseAppScript(e,t){this.baseSource.setScript(e,t)}setBaseAppStyle(e,t){this.baseSource.setStyle(e,t)}get hasActiveApp(){return Array.from(this.cache.values()).some((e=>e.status!==t.UNMOUNT))}};function be(e){if(e instanceof HTMLLinkElement){const t=e.getAttribute("rel");let n=e.getAttribute("href");if("stylesheet"===t&&n){n=d(n,location.origin);const t=document.createElement("style");return function(e,t,n){const o=document.createElement("style");O(o),_(e).then((r=>{t.code=r,Se.setBaseAppStyle(e,t),o.textContent=r,w(n)})).catch((e=>{console.error(e),E(n)}))}(n,new A({code:"",fromHtml:!1,url:n}),e),t}}if(e instanceof HTMLScriptElement){let t=e.getAttribute("src");if(t&&"module"!==e.type){t=d(t,location.origin);const n=new L({async:e.hasAttribute("async"),code:"",defer:e.defer||"module"===e.type,fromHtml:!1,isModule:!1});Se.setBaseAppScript(t,n);const o=function(e,t,n){const o=document.createElement("script");return O(o),_(e).then((r=>{t.setCode(r);try{o.textContent=r,e.startsWith("inline-")||n.setAttribute("origin-src",e)}catch(t){console.error(t,e)}w(n)})).catch((e=>{console.error(e),E(n)})),o}(t,n,e);return o||e}}return e}function ge(e){return e instanceof HTMLLinkElement||e instanceof HTMLScriptElement}function Ce(e,t,n){if(ge(t)){const o=be(t);return n.call(e,o)}return n.call(e,t)}function ve(){const e=HTMLBodyElement.prototype.appendChild,t=HTMLHeadElement.prototype.appendChild,n=HTMLHeadElement.prototype.appendChild;HTMLBodyElement.prototype.appendChild=function(t){return Ce(this,t,e)},HTMLHeadElement.prototype.appendChild=function(e){return Ce(this,e,t)},HTMLHeadElement.prototype.insertBefore=function(e,t){return function(e,t,n,o){if(ge(t)){const r=be(t);return o.call(e,r,n)}return o.call(e,t,n)}(this,e,t,n)},window.addEventListener("load",(()=>{document.head.querySelectorAll("style").forEach((e=>{e.textContent&&Se.setBaseAppStyle(u(),new A({code:e.textContent,fromHtml:!1,url:""}))}))}))}window.__getAppOrInstance__=function(e){return e?Se.getApp(e):Se};const Le="bk-weweb";class Te{fetchSource;webcomponentTag=Le;constructor(){window.customElements.get(Le)||window.customElements.define(Le,te)}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,te)}start(e){e?.collectBaseSource&&ve(),"function"==typeof e?.fetchSource&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||Le,this.setWebComponentTag()}}const Pe=new Te;e.WeWeb=Te,e.activated=F,e.deactivated=q,e.default=Pe,e.load=G,e.loadApp=z,e.loadInstance=Z,e.mount=function(e,t,n){const o=Se.getApp(e);o&&a((()=>{J(),o.mount(t,n)}))},e.preLoadApp=function(e){h((()=>z({...e,isPreLoad:!0})))},e.preLoadInstance=function(e){h((()=>Z({...e,isPreLoad:!0})))},e.preLoadSource=function(e){h((()=>{!async function(e){const{collectScript:t,collectStyle:n}=await Ee(e),o=[];t&&Array.from(t.entries()).forEach((e=>{o.push(e[1].getCode()),Se.setBaseAppScript(...e)})),n&&Array.from(n.entries()).forEach((e=>{o.push(e[1].getCode()),Se.setBaseAppStyle(...e)})),await Promise.all(o).catch((e=>{throw e}))}(e)}))},e.unload=function(e){Se.deleteApp(e)},e.unmount=Q,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).bkIframe={})}(this,(function(e){"use strict";var t;!function(e){e.ACTIVATED="ACTIVATED",e.DEACTIVATED="DEACTIVATED",e.ERROR="ERROR",e.LOADED="LOADED",e.LOADING="LOADING",e.MOUNTED="MOUNTED",e.MOUNTING="MOUNTING",e.UNMOUNT="UNMOUNT",e.UNSET="UNSET"}(t||(t={}));let n=null;function o(){return n}function r(e){n=e}const i=new Map;Object.getOwnPropertyNames(window).forEach((e=>{!i.has(e)&&e.match(/^[A-Z]/)&&"function"==typeof window[e]&&window[e].toString().includes("[native code]")&&i.set(e,!0)}));const s="id";var c;!function(e){e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.STYLE_RULE=1]="STYLE_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(c||(c={}));const a=e=>Promise.resolve().then(e);function l(e){return e.startsWith("//")?`${location.protocol}${e}`:e.startsWith("http")?e:`${location.protocol}//${e}`}function d(e,t){if(!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e))return e;const{origin:n,pathname:o}=new URL(l(t));return new URL(e,`${n}${o}`.replace(/\.(\w+)$/,"/")).toString()}function u(){return`inline-${p(16)}`}const h=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout((()=>{e({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-t))})}),1)},p=(e,t="abcdefghijklmnopqrstuvwxyz0123456789")=>{let n="";for(let o=0;o<e;o++)n+=t[parseInt((Math.random()*t.length).toString(),10)];return n},f=e=>{if(!e)return!1;if(e.match(/\.js$/))return!1;const{pathname:t}=new URL(l(e));return!t.match(/\.js$/)},m=["currentTarget","srcElement","target"];function y(e,t="custom"){return Object.defineProperties(new CustomEvent(t),m.reduce(((t,n)=>(t[n]={get:()=>e},t)),{}))}function w(e){const t=y(e,"load");"function"!=typeof e.onload?e.dispatchEvent(t):e.onload(t)}function E(e){const t=y(e,"error");"function"!=typeof e.onerror?e.dispatchEvent(t):e.onerror(t)}function _(e,t={}){return Pe.fetchSource?Pe.fetchSource(e,t):window.fetch(e,t).then((e=>e.text()))}class A{code="";fromHtml;initial;prefetch=!1;preload=!1;scoped;scopedCode="";url;constructor({code:e,fromHtml:t,initial:n,prefetch:o,preload:r,url:i}){this.scoped=!1,this.code=e,this.prefetch=o??!1,this.preload=r??!1,this.url=i,this.fromHtml=t,this.initial=n??!1}commonScoped(e,t){if(!t.scopeCss||t.container instanceof ShadowRoot){const n=this.resetUrlHost(e.textContent||this.code||"",t.url,this.url);if(n&&t.container instanceof ShadowRoot){let e="";n.match(/@font-face\s*\{[^}]+\}/g)?.forEach((t=>{e+=`${t}\n`}));const o=t.sandBox?.rawDocument;if(o&&e){const t=o.createElement("style");t.setAttribute("type","text/css"),t.setAttribute("powered-by","bk-weweb"),t.textContent=e,o?.head?.append(t)}}e.textContent=n}else{const n=new CSSStyleSheet({disabled:!0});n.replaceSync(e.textContent||this.code);const o=Array.from(n?.cssRules??[]),r=`#${t.name}`,i=this.scopeRule(o,r),s=this.resetUrlHost(i,t.url,this.url);e.textContent=s,this.scopedCode=s}this.scoped=!0}createStyleElement(){const e=document.createElement("style");return e.__BK_WEWEB_APP_KEY__&&delete e.__BK_WEWEB_APP_KEY__,e}async excuteCode(e){e.registerRunningApp();let t=this.createStyleElement();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}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&&Se.getCacheStyle(this.url)){const e=Se.getCacheStyle(this.url);t=e?.code||""}return t||(t=await _(this.url).catch((()=>""))),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot||!e.textContent||!Se.getBaseAppStyle(e.textContent))&&(e.textContent="",e.innerHTML="",e.setAttribute("linked-from-base","true"),!0)}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)=>{if(/^(data|blob):/.test(o)||/^(https?:)?\/\//.test(o))return e;if(/^((\.\.?\/)|[^/])/.test(o)&&n){const e=n.split("/");e.pop(),t=l(`${e.join("/")}/`)}return`url("${d(o,t)}")`}))}scopeRule(e,t){let n="";for(const o of e)switch(o.type){case c.STYLE_RULE:n+=this.scopeStyleRule(o,t);break;case c.MEDIA_RULE:n+=this.resetPackRule(o,t,"media");break;case c.SUPPORTS_RULE:n+=this.resetPackRule(o,t,"supports");break;default:n+=o.cssText}return n.replace(/^\s+/,"")}scopeStyleRule(e,t){const{cssText:n,selectorText:o}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(o))return n.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if("*"===o)return n.replace("*",`${t} *`);const r=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return n.replace(/^[\s\S]+{/,(e=>e.replace(/(^|,)([^,]+)/g,((e,n,o)=>r.test(o)?e.replace(r,t):`${n} ${t} ${o.replace(/^\s*/,"")}`))))}scopedLinkCSS(e,t){const n=this.createStyleElement();n.setAttribute("type","text/css");const o=!(!e.keepAlive||e.container instanceof ShadowRoot);O(n,e,o);const r=o?document.head:e.container;try{if(this.code)this.commonScoped(n,e),r?.prepend(n),t&&w(t);else if(t.getAttribute("href"))this.url=d(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&w(t),this.scoped=!0}));else{const o=new MutationObserver((()=>{t.href&&(o.disconnect(),this.url=d(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&w(t),this.scoped=!0})))}));o.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&E(t)}return n}scopedStyleCSS(e,t){const n=!(!e.keepAlive||e.container instanceof ShadowRoot);if(O(t,e,n),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;this.commonScoped(t,e)}else{const n=new MutationObserver((()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,e))}));n.observe(t,{attributes:!1,characterData:!0,childList:!0,subtree:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}}async function S(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?.append(...n):document.head.append(...n)}))}class g{state=t.UNSET;appCacheKey;container;data;initSource;isPreLoad=!1;keepAlive;name;sandBox;scopeCss=!0;scopeJs=!1;showSourceCode=!0;source;url;constructor(e){this.name=e.id!==e.url?e.id:p(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 me(this))}activated(e,n){if(this.isPreLoad=!1,this.state=t.ACTIVATED,this.container&&e){e instanceof Element&&e.setAttribute(s,this.name);const t=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{t.appendChild(e)})),e.appendChild(t),this.container=e,this.sandBox?.activeated();const o=this.source?.getScript(this.url);n?.(this,o?.exportInstance)}}deactivated(){this.state=t.DEACTIVATED,this.sandBox?.deactivated()}mount(e,n){this.isPreLoad=!1,this.container=e??this.container,this.state=t.MOUNTING,this.container instanceof HTMLElement&&this.container.setAttribute(s,this.name),this.container.innerHTML="";const o=document.createElement("div"),r=`${this.name}-wrapper`;o.setAttribute("id",r),this.source?.styles.size&&S(this,this.container),this.container.appendChild(o),this.sandBox?.activeated(),P(this).finally((()=>{this.state=t.MOUNTED;const e=this.source?.getScript(this.url);"function"==typeof e?.exportInstance?.render&&e.exportInstance.render(o,this.data),n?.(this,e?.exportInstance)}))}onError(){this.state=t.ERROR}onMount(){this.isPreLoad||(this.state=t.LOADED,this.mount())}registerRunningApp(){r(this),Promise.resolve().then((()=>r(null)))}async start(){this.source&&![t.ERROR,t.UNSET].includes(this.status)||(this.source=new Ae(this.url),await this.source.importEntery(this))}unmount(e){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&Se.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}}let b,C,v;class L{async=!1;code="";defer=!1;exportInstance;fromHtml;initial;isModule=!1;scoped;url;constructor({async:e,code:t,defer:n,fromHtml:o,initial:r,isModule:i,url:s}){this.code=t,this.async=e,this.defer=n,this.isModule=i,this.url=s,this.scoped=!1,this.fromHtml=o??!1,this.initial=r??!1}async excuteCode(e,t=!1){try{if(this.code||await this.getCode(e),e instanceof g){!function(e){C=void 0,b=C;for(const t in e)T(e,t)||(b?C||(C=t):b=t,v=t)}(e.scopeJs&&e.sandBox?.proxyWindow||window)}let n=this.code;if(n=this.transformCode(e),e.showSourceCode||this.isModule){const o=document.createElement("script");if(o.__BK_WEWEB_APP_KEY__&&delete o.__BK_WEWEB_APP_KEY__,e.registerRunningApp(),this.executeSourceScript(o,n),t)return o;const r=!(!e.keepAlive||e.container instanceof ShadowRoot),i=r?document.head:e.container;O(o,e,r),i.appendChild(o)}else if(this.executeMemoryScript(e,n),t)return document.createComment("【bk-weweb】dynamic script");if(e instanceof g){const t=e.scopeJs&&e.sandBox?.proxyWindow||window,n=function(e,t){let n,o,r=0;for(const i in e)if(!T(e,i)){if(0===r&&i!==b||1===r&&i!==C)return i;n?(v=i,o=t&&o||i):n=i===v,r+=1}return o}(t);n&&(this.exportInstance=t[n],e.scopeJs||delete t[n])}}catch(e){console.error("execute script code error",e)}}executeMemoryScript(e,t){try{const n=e instanceof we&&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)}}executeSourceScript(e,t){this.isModule?(e.src=this.url+"?key="+Date.now(),e.setAttribute("type","module")):e.textContent=t,this.url&&e.setAttribute("origin-src",this.url)}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&&Se.getCacheScript(this.url)&&(t=Se.getCacheScript(this.url)?.code||""),t||(t=await _(this.url).catch((e=>(console.error(`fetch script ${this.url} error`,e),"")))),this.code=t,t}setCode(e){this.code=e}transformCode(e){return e.sandBox?this.isModule?` with(window.${e.sandBox.windowSymbolKey}){\n ;${this.code}\n\n }`:e.showSourceCode?`;(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}}function T(e,t){return!e.hasOwnProperty(t)||!isNaN(t)&&t<e.length||"undefined"!=typeof navigator&&-1!==navigator.userAgent.indexOf("Trident")&&e[t]&&"undefined"!=typeof window&&e[t].parent===window}async function P(e){const t=Array.from(e.source.scripts.values()).filter((e=>e.fromHtml||e.initial)),n=t.filter((e=>!e.async&&!e.defer||e.isModule));await Promise.all(n.map((t=>t.getCode(e)))),await Promise.all(n.map((t=>t.excuteCode(e))));const o=[],r=[];t.forEach((t=>{(t.defer||t.async)&&(!t.code&&t.defer?o.push(t.excuteCode(e)):r.push(t.excuteCode(e)))})),await Promise.all([...r,...o]).catch((e=>{console.error(e)}))}const{appendChild:B}=HTMLBodyElement.prototype;function R(e,t,n){if(t instanceof HTMLStyleElement){if(t.hasAttribute("exclude"))return document.createComment("【bk-weweb】style with exclude attribute is ignored");if(t.textContent&&!(n.container instanceof ShadowRoot)&&Se.getBaseAppStyle(t.textContent))return document.createComment("【bk-weweb】style is effective in base app");if(!t.hasAttribute("ignore")){const e=new A({code:t.textContent||"",fromHtml:!1,url:""});n.source?.setStyle(u(),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 r=n.source.collectScript(t,e,!0);if(r?.replace&&B.call(n.container,r.replace),f(t.getAttribute("src")))return void n.container?.append(t);r?.script&&r.script.excuteCode(n),t.remove()}else if(t.textContent){o.disconnect();const e=new L({async:!1,code:t.textContent,defer:"module"===t.type,fromHtml:!1,isModule:"module"===t.type});n.source.scripts.set(u(),e);try{e.excuteCode(n)}catch(e){console.error(e)}finally{!e.isModule&&w(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 M(e){return e instanceof HTMLScriptElement||e instanceof HTMLStyleElement||e instanceof HTMLLinkElement}function x(e,t,n){if(t.__BK_WEWEB_APP_KEY__){const o=Se.getApp(t.__BK_WEWEB_APP_KEY__);if(o?.container){const r=R(e,t,o),i=M(t)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),s=i?document.head:o?.container;return O(r,o,i),n.call(s,r)}}return n.call(e,t)}function W(e,t,n,o){if(t.__BK_WEWEB_APP_KEY__){const r=Se.getApp(t.__BK_WEWEB_APP_KEY__);if(r?.container){const i=M(t)&&r.keepAlive&&!(r.container instanceof ShadowRoot),s=i?document.head:r?.container,c=R(e,t,r);return i&&O(c,r,i),n&&!s.contains(n)?B.call(s,c):o.call(s,c,n)}}return o.call(e,t,n)}function O(e,t,n){return n&&t&&(e.__KEEP_ALIVE__=t.appCacheKey,e.setAttribute("data-from",t.name),e.setAttribute("data-keep-alive","true")),e.setAttribute?.("powered-by","bk-weweb"),e}const{setAttribute:H}=Element.prototype,{append:K,appendChild:k,insertBefore:D,removeChild:N}=HTMLBodyElement.prototype,{appendChild:I,insertBefore:U,removeChild:$}=HTMLHeadElement.prototype,j=document.head;let V=!1;function Y(){r(null),Element.prototype.setAttribute=H,HTMLBodyElement.prototype.appendChild=k,HTMLBodyElement.prototype.append=K,HTMLBodyElement.prototype.removeChild=N,HTMLBodyElement.prototype.insertBefore=D,HTMLHeadElement.prototype.appendChild=I,HTMLHeadElement.prototype.insertBefore=U,HTMLHeadElement.prototype.removeChild=$,V=!1}function J(){V||(V=!0,Element.prototype.setAttribute=function(e,t){const n=this.tagName.toLocaleUpperCase();(["src","srcset"].includes(e)&&["IMG","SCRIPT"].includes(n)||"href"===e&&["LINK"].includes(n))&&this.__BK_WEWEB_APP_KEY__&&Se.getApp(this.__BK_WEWEB_APP_KEY__||"")?H.call(this,e,d(t,Se.getApp(this.__BK_WEWEB_APP_KEY__).url)):H.call(this,e,t)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&M(e)?I.call(j,e):x(this,e,k)},HTMLBodyElement.prototype.append=function(...e){e.forEach((e=>{if(e.__KEEP_ALIVE__&&M(e))return I.call(j,e);x(this,e,k)}))},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(e,t){return W(this,e,t,U)},HTMLBodyElement.prototype.insertBefore=function(e,t){return W(this,e,t,U)},HTMLBodyElement.prototype.removeChild=function(e){const t=Se.getApp(e.__BK_WEWEB_APP_KEY__);return t?.container?.contains(e)?N.call(t.container,e):this.contains(e)?N.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild)}function F(e,n,o){const r=Se.getApp(e);r?.status===t.DEACTIVATED&&r.keepAlive?a((()=>{J(),r.activated(n,o)})):r&&a((()=>{J(),r.mount(n,o)}))}function q(e){const n=Se.getApp(e);n&&[t.ACTIVATED,t.MOUNTED].includes(n.status)&&(n.keepAlive?n.deactivated():n.unmount()),Se.hasActiveApp||Y()}async function G(t){return J(),t.mode===e.WewebMode.INSTANCE?await Z(t):await z(t)}async function z(e){J();let t=Se.getApp(e.id);return t||(t=new we(e),Se.setApp(t)),await t.start(),t}function Z(e){return J(),new Promise((n=>{let o=Se.getApp(e.id);if(o)if([t.MOUNTING,t.UNSET].includes(o.status)){const e=setInterval((()=>{[t.ERROR,t.MOUNTED].includes(o.status)&&(n(o),clearInterval(e))}),300)}else n(o);else o=new g(e),Se.setApp(o),o.start().then((()=>n(o)))}))}function Q(e){const n=Se.getApp(e);n&&n.status!==t.UNMOUNT&&n.unmount(),Se.hasActiveApp||Y()}var X,ee;!function(e){e.data="data",e.id="id",e.keepAlive="keepAlive",e.mode="mode",e.scopeCss="scopeCss",e.scopeJs="scopeJs",e.scopeLocation="scopeLocation",e.setShodowDom="setShodowDom",e.showSourceCode="showSourceCode",e.url="url"}(X||(X={}));class te extends HTMLElement{appUrl="";connected=!1;static get observedAttributes(){return[X.url]}getBooleanAttr(e){return this.hasAttribute(e)||this.hasAttribute(e.replace(/([A-Z])/g,"-$1").toLocaleLowerCase())?"false"!==this.getAttribute(e):void 0}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr(X.setShodowDom)&&this.attachShadow({mode:"open"});const e=Se.getApp(this.appKey);!e||e.url!==this.appUrl||!e.isPreLoad&&e.status!==t.UNMOUNT?await G(this.appProps):F(this.appKey,this.shadowRoot??this)}attributeChangedCallback(e,t,n){if(e!==X.url||this[e]===n||!this.connected)return;this.appUrl=n;const o=Se.getApp(this.appKey);(this.connected||o)&&this.handleAttributeChanged()}async connectedCallback(){this.getBooleanAttr(X.setShodowDom)&&!this.shadowRoot&&this.attachShadow({delegatesFocus:!1,mode:"open"}),await G(this.appProps),F(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?q(this.appKey):Q(this.appKey)}get appData(){if(this.hasAttribute(X.data))try{return JSON.parse(this.getAttribute(X.data))}catch{}return{}}get appKey(){return this.getAttribute(X.id)||this.getAttribute(X.url)}get appProps(){return this.getAttribute("mode")===e.WewebMode.INSTANCE?{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(X.keepAlive),mode:e.WewebMode.INSTANCE,scopeCss:this.getBooleanAttr(X.scopeCss)&&!this.getBooleanAttr(X.setShodowDom),scopeJs:this.getBooleanAttr(X.scopeJs),showSourceCode:this.getBooleanAttr(X.showSourceCode),url:this.getAttribute(X.url)}:{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(X.keepAlive),mode:e.WewebMode.APP,scopeCss:!this.getBooleanAttr(X.setShodowDom),scopeJs:!this.getBooleanAttr(X.scopeJs),scopeLocation:this.getBooleanAttr(X.scopeLocation),showSourceCode:this.getBooleanAttr(X.showSourceCode),url:this.getAttribute(X.url)}}}e.WewebMode=void 0,(ee=e.WewebMode||(e.WewebMode={})).APP="app",ee.CONFIG="config",ee.INSTANCE="js";const ne=["System","__cjsWrapper","production"!==process.env.NODE_ENV?"__REACT_DEVTOOLS_GLOBAL_HOOK__":""],oe=["__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],re=["window","self","globalThis"],ie=["location","history"],se={__bk_pop_manager:!0,__bk_zIndex_manager:!0,i18n:!0},ce="production"!==process.env.NODE_ENV?{__DEV__:!0,__VUE_DEVTOOLS_GLOBAL_HOOK__:!0,__VUE_DEVTOOLS_HOOK_REPLAY__:!0,__VUE_DEVTOOLS_PLUGINS__:!0,__VUE_I18N_FULL_INSTALL__:!0,__VUE_I18N_LEGACY_API__:!0,__VUE_OPTIONS_API__:!0,"__core-js_shared__":!0,webpackChunkapm:!0,webpackChunkpc:!0,webpackChunktrace:!0,webpackJsonp:!0,...se}:se,ae=["body","html","head"],le=(e,t)=>{const n=new Proxy({},{get(n,o){if(t.container instanceof ShadowRoot){const e=t.container[o];if("function"==typeof e)return e.bind(t.container);if(void 0!==e)return e}const r=Reflect.get(e.body,o);return"function"==typeof r?r.bind(e.body):r},set:(n,o,r)=>t.container instanceof ShadowRoot?(t.container[o]=r,!0):(Reflect.set(e.body,o,r),!0)});function o(n,o){const r=e.createElement(n,o);return r.__BK_WEWEB_APP_KEY__=t.appCacheKey,r}function r(o){if(o===n)return t.container instanceof ShadowRoot?t.container:e.body;if(ae.includes(o))return t?.container instanceof ShadowRoot?t?.container:e.querySelector.call(this,o);try{return t?.container?.querySelector(o)??null}catch{return null}}function i(n){return ae.includes(n)?t?.container instanceof ShadowRoot?t?.container:e.querySelector(n):t?.container?.querySelectorAll(n)??[]}function s(t){return r.call(e,`#${t}`)}function c(e){return i(`.${e}`)}function a(n){return ae.includes(n)||!t?.showSourceCode&&"script"===n.toLocaleLowerCase()?e.getElementsByTagName(n):i(n)}function l(e){return i(`[name=${e}]`)}return new Proxy({},{get(t,d){if("createElement"===d)return o.bind(e);if("querySelector"===d)return r.bind(e);if("querySelectorAll"===d)return i.bind(e);if("getElementById"===d)return s.bind(e);if("getElementsByClassName"===d)return c.bind(e);if("getElementsByTagName"===d)return a.bind(e);if("getElementsByName"===d)return l.bind(e);if("body"===d)return n;const u=Reflect.get(e,d);return"function"==typeof u?u.bind(e):u}})};const de=/^class\b/,ue=/^function\b\s[A-Z].*/,he=new WeakMap;const pe=new WeakMap;function fe(e,t){if(pe.has(t))return pe.get(t);if("function"==typeof t&&!function(e){if(e.prototype?.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1)return!0;if(he.has(e))return he.get(e);const t=ue.test(e.toString())||de.test(e.toString());return he.set(e,t),t}(t)){const n=Function.prototype.bind.call(t,e);for(const e in t)n[e]=t[e];if(t.hasOwnProperty("prototype")&&!n.hasOwnProperty("prototype")&&Object.defineProperty(n,"prototype",{enumerable:!1,value:t.prototype,writable:!0}),"function"==typeof t.toString){const e=t.hasOwnProperty("toString")&&!n.hasOwnProperty("toString"),o=n.toString===Function.prototype.toString;if(e||o){const o=Object.getOwnPropertyDescriptor(e?t:Function.prototype,"toString");Object.defineProperty(n,"toString",{...o,...o?.get?null:{value:()=>t.toString()}})}}return pe.set(t,n),n}return t}class me{app;active=!1;inRawWindowKeySet=new Set;resetDocumentAndBodyEvent;resetWindowFunction;sameRawWindowKeySet=new Set;fakeWindow;proxyDocument;proxyWindow;rawDocument;rawWindow;windowSymbolKey;constructor(e){this.app=e;const t=new Set,n=window;this.rawWindow=n,this.rawDocument=le(document,e);const o=Object.create({});o.__BK_WEWEB_APP_KEY__=e.appCacheKey,o.__POWERED_BY_BK_WEWEB__=!0,o.rawDocument=document,o.rawWindow=n,o.__proto__=Window,this.fakeWindow=o;const{resetWindowFunction:r}=function(e){const t=new Map,n=[],o=window,{addEventListener:r,clearInterval:i,removeEventListener:s,setInterval:c}=window;return e.addEventListener=function(e,n,i){t.set(e,[...t.get(e)||[],n]),r.call(o,e,n,i)},e.removeEventListener=function(e,n,r){const i=t.get(e);if(i?.length){const e=i.indexOf(n);e>-1&&i.splice(e,1)}s.call(o,e,n,r)},e.setInterval=function(e,t,...r){const i=c.call(o,e,t,...r);return n.push(i),i},e.clearInterval=function(e){const t=n.indexOf(e);t>-1&&n.splice(t,1),i.call(o,e)},{resetWindowFunction:function(){t.size&&(t.forEach(((e,t)=>{e.forEach((e=>s.call(o,t,e)))})),t.clear()),n.length&&n.forEach((e=>{i.call(o,e)}))}}}(this.fakeWindow);this.resetWindowFunction=r,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${p(10)}__`,this.proxyWindow=new Proxy(this.fakeWindow,{defineProperty:(e,o,r)=>t.has(o)?Reflect.defineProperty(n,o,r):Reflect.defineProperty(e,o,r),deleteProperty:(e,t)=>!e.hasOwnProperty(t)||(this.sameRawWindowKeySet.has(t)&&this.sameRawWindowKeySet.delete(t),this.inRawWindowKeySet.has(t)&&Reflect.deleteProperty(n,t),Reflect.deleteProperty(e,t)),get:(t,o)=>{if(i.has(o)||o===Symbol.unscopables)return n[o];if(ce[o])return this.fakeWindow[o];if(re.includes(o))return this.proxyWindow;if("document"===o)return e.registerRunningApp(),this.rawDocument;if("eval"===o)return e.registerRunningApp(),eval;if(ie.includes(o)&&this.app instanceof we&&this.app.iframe&&this.app.scopeLocation)return this.app.iframe.contentWindow?.[o];if("hasOwnProperty"===o)return e=>this.fakeWindow.hasOwnProperty(e)||n.hasOwnProperty(e);if("top"===o||"parent"===o)return n===n.parent?this.proxyWindow:Reflect.get(n,o);if("getComputedStyle"===o)return(e,t)=>e instanceof Element?n.getComputedStyle(e,t):n.getComputedStyle(document.body,t);if(Reflect.has(t,o)||oe.includes(o))return Reflect.get(t,o);const r=Reflect.get(n,o);return fe(n,r)},getOwnPropertyDescriptor:(e,o)=>{if(e.hasOwnProperty(o))return Object.getOwnPropertyDescriptor(e,o);if(n.hasOwnProperty(o)){t.add(o);const e=Object.getOwnPropertyDescriptor(n,o);return e&&!e.configurable&&(e.configurable=!0),e}},has:(e,t)=>i.has(t)||t in e||t in n,ownKeys:e=>Array.from(new Set(Reflect.ownKeys(n).concat(Reflect.ownKeys(e)))),set:(e,t,o)=>{if(this.active){if(ie.includes(t)&&this.app instanceof we&&this.app.iframe&&this.app.scopeLocation)return Reflect.set(this.app.iframe.contentWindow,t,o);if("location"===t)Reflect.set(n,t,o);else if(e.hasOwnProperty(t)||!n.hasOwnProperty(t)||oe.includes(t))Reflect.set(e,t,o),this.sameRawWindowKeySet.add(t);else{const r=Object.getOwnPropertyDescriptor(n,t),{configurable:i,enumerable:s,writable:c}=r;c&&(Object.defineProperty(e,t,{configurable:i,enumerable:s,value:o,writable:c}),this.sameRawWindowKeySet.add(t))}!ne.includes(t)||Reflect.has(n,t)||oe.includes(t)||(Reflect.set(n,t,o),this.inRawWindowKeySet.add(t))}return!0}}),e.showSourceCode&&(n[this.windowSymbolKey]=this.proxyWindow)}activeated(e){if(!this.active){this.active=!0,this.rawDocument=le(document,this.app),this.fakeWindow.__BK_WEWEB_DATA__=e??{};const{resetDocumentAndBodyEvent:t}=function(){const{addEventListener:e,removeEventListener:t}=window.document,{addEventListener:n,removeEventListener:r}=window.document.body,i=new Map;return document.addEventListener=function(t,n,r){const s=o();if(s?.keepAlive){const e=i.get(t)||[];i.set(t,[...e,n])}e.call(s?.container instanceof ShadowRoot?s.container:this,t,n,r)},document.body.addEventListener=document.addEventListener,document.removeEventListener=function(e,n,r){const s=o();if(s?.keepAlive){const t=i.get(e)||[];t.length&&t.some((e=>e===n))&&t.splice(t.indexOf(n),1)}t.call(s?.container instanceof ShadowRoot?s.container:this,e,n,r)},document.body.removeEventListener=document.removeEventListener,{resetDocumentAndBodyEvent:function(){const s=o();s?.keepAlive&&i.values()&&Array.from(i.entries()).forEach((([e,t])=>{t?.forEach((t=>{document.removeEventListener.call(document,e,t)}))})),document.addEventListener=e,document.body.addEventListener=n,document.removeEventListener=t,document.body.removeEventListener=r,i.clear()}}}();this.resetDocumentAndBodyEvent=t}}deactivated(){this.active&&(this.active=!1,this.resetWindowFunction(),this.inRawWindowKeySet.forEach((e=>Reflect.deleteProperty(window,e))),this.inRawWindowKeySet.clear(),this.resetDocumentAndBodyEvent?.())}}const ye="about:blank";class we{state=t.UNSET;container;data;iframe=null;initSource;isModuleApp=!1;isPreLoad=!1;keepAlive;mode=e.WewebMode.APP;name;sandBox;scopeCss;scopeJs;scopeLocation;showSourceCode;source;url;constructor(t){this.name=t.id!==t.url?t.id:p(5),this.mode=t.mode??e.WewebMode.APP,this.container=t.container??void 0,this.showSourceCode=t.showSourceCode??!1,this.url=t.url,this.data=t.data||{},this.scopeJs=t.scopeJs??!0,this.scopeCss=t.scopeCss??!0,this.scopeLocation=t.scopeLocation??!1,this.isPreLoad=t.isPreLoad??!1,this.keepAlive=t.keepAlive??!1,this.initSource=t.initSource??[],this.scopeJs&&(this.sandBox=new me(this)),this.container instanceof HTMLElement&&this.container.setAttribute(s,this.name)}activated(e,n){this.isPreLoad=!1,this.state=t.ACTIVATED;const o=this;if(e&&this.container){e instanceof Element&&e.setAttribute(s,this.name);const t=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{e.__BK_WEWEB_APP_KEY__=this.appCacheKey,Object.defineProperties(e,{ownerDocument:{get:()=>o.sandBox?.rawDocument}}),t.appendChild(e)})),e.innerHTML="",e.appendChild(t),this.container=e,this.initShadowRootContainer(),this.sandBox?.activeated(this.data),n?.(this)}}createIframe(){return new Promise((e=>{const t=document.createElement("iframe"),n=new URL(l(this.url)),o=navigator.userAgent.indexOf("Chrome")>-1;if(t.setAttribute("src",`${o?ye:location.origin}${n.pathname||"/"}${n.search}${n.hash}`),t.style.cssText="display: none;",document.body.appendChild(t),o)setTimeout((()=>e(t)),0);else{const n=setInterval((()=>{t.contentWindow&&t.contentWindow.location.href!==ye&&(t.contentWindow.stop(),t.contentDocument.body.parentElement.innerHTML="<head></head><body></body>",clearInterval(n),e(t))}),0)}}))}deactivated(){this.state=t.DEACTIVATED,this.sandBox?.deactivated()}initShadowRootContainer(){this.container instanceof ShadowRoot&&Object.defineProperties(this.container,{getBoundingClientRect:{get(){return this.container.host.getBoundingClientRect}}})}mount(e,n){this.isPreLoad=!1,this.container=e??this.container,this.initShadowRootContainer(),this.state=t.MOUNTING;const o=this;if(this.container){this.container instanceof Element&&this.container.setAttribute(s,this.name);const e=this.source.html.cloneNode(!0),r=document.createDocumentFragment();Array.from(e.childNodes).forEach((e=>{e.__BK_WEWEB_APP_KEY__=this.appCacheKey,Object.defineProperties(e,{ownerDocument:{get:()=>o.sandBox?.rawDocument}}),r.appendChild(e)})),this.container.innerHTML="",this.container.appendChild(r),this.sandBox?.activeated(this.data),P(this).finally((()=>{this.state=t.MOUNTED,n?.(this)}))}}onError(){this.state=t.ERROR}onMount(){this.isPreLoad||(this.state=t.LOADED,this.mount())}registerRunningApp(){r(this),Promise.resolve().then((()=>r(null)))}async start(){if(!this.source||[t.ERROR,t.UNSET].includes(this.status)){if(this.state=t.LOADING,this.scopeLocation||this.isModuleApp){const e=await this.createIframe();this.iframe=e}this.source=new Ae(this.url),await this.source.importEntery(this)}}unmount(e=!1){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&Se.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}get appCacheKey(){return this.url}get status(){return this.state}set status(e){this.state=e}}async function Ee(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=Se.getCacheScript(e);n.set(e,new L({async:!1,code:t?.code||"",defer:!1,fromHtml:!1,initial:!0,isModule:!1,url:e}))}else if(t.pathname.match(/\.css$/)){const t=Se.getCacheStyle(e);o.set(e,new A({code:t?.code||"",fromHtml:!0,initial:!0,prefetch:!1,preload:!1,url:e}))}}catch{console.error(`【bk-weweb】: ${e} is invalid URL`)}})),{collectScript:n,collectStyle:o}}const _e=["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"];class Ae{url;html=null;rawHtml;scripts;styles;constructor(e){this.url=e,this.scripts=new Map,this.styles=new Map}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 r,i=e.getAttribute("href");if("stylesheet"===o&&i){i=d(i,this.url),r=document.createComment(`【bk-weweb】style with href=${i}`);let o=this.getStyle(i);return o||(o=new A({code:"",fromHtml:!n,prefetch:!!e.getAttribute("prefetch"),preload:!!e.getAttribute("preload"),url:i}),this.styles.set(i,o)),!n&&t.replaceChild(r,e),{replace:r,style:o}}return o&&["apple-touch-icon","icon","prefetch","preload","prerender"].includes(o)?(r=document.createComment(`【bk-weweb】style with rel=${o}${i?` & href=${i}`:""}`),!n&&t.removeChild(e),{replace:r}):(i&&e.setAttribute("href",d(i,this.url)),{replace:e})}collectScript(e,t,n=!1){if(e.hasAttribute("ignore")||"module"!==e.type&&f(e.getAttribute("src"))||e.hasAttribute("type")&&!_e.includes(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 r=e.getAttribute("src");if(r){r=d(r,this.url);let i=this.getScript(r);return i||(i=new L({async:e.hasAttribute("async"),code:"",defer:e.defer||"module"===e.type,fromHtml:!n,isModule:"module"===e.type,url:r}),this.scripts.set(r,i)),o=document.createComment(`【bk-weweb】script with src='${r}'`),!n&&t.replaceChild(o,e),{replace:o,script:i}}if(e.textContent){const r=u(),i=new L({async:!1,code:e.textContent,defer:"module"===e.type,fromHtml:!n,initial:!1,isModule:"module"===e.type,url:r});return this.scripts.set(r,i),o=document.createComment("【bk-weweb】script with texcontent"),!n&&t.replaceChild(o,e),{replace:o,script:i}}return{replace:e}}collectScriptAndStyle(e){const t=Array.from(e.querySelectorAll("link"));t?.forEach((e=>{this.collectLink(e,e.parentElement)}));const n=Array.from(e.querySelectorAll("style"));n?.forEach((e=>{e.hasAttribute("exclude")||e.hasAttribute("ignore")||(this.styles.set(u(),new A({code:e.textContent||"",fromHtml:!0,url:""})),e.remove())}));const o=Array.from(e.querySelectorAll("script"));o?.forEach((e=>{this.collectScript(e,e.parentElement)}));const r=Array.from(e.querySelectorAll("meta"));r?.forEach((e=>{e.parentElement.removeChild(e)}));const i=Array.from(e.querySelectorAll("img"));i?.forEach((e=>{e.hasAttribute("src")&&e.setAttribute("src",d(e.getAttribute("src"),this.url))}))}getScript(e){return this.scripts.get(e)}getStyle(e){return this.styles.get(e)||Array.from(this.styles.values()).find((t=>t.code===e))}async importEntery(e){if(e.initSource?.length){const{collectScript:t,collectStyle:n}=await Ee(e.initSource);t&&(this.scripts=t),n&&(this.styles=n)}e instanceof we?await this.importHtmlEntry(e):e instanceof g&&await this.importInstanceEntry()}async importHtmlEntry(e){let t=Se.getCacheHtml(this.url);if(!t&&(t=await _(l(this.url),{cache:"no-cache"}),!t))return console.error("load app entry error, pleace check"),Promise.reject();this.rawHtml=t;const n=document.createElement("div");n.__BK_WEWEB_APP_KEY__&&delete n.__BK_WEWEB_APP_KEY__,n.innerHTML=t.replace(/<\/?head>/gim,"").replace(/<\/?body>/i,""),this.collectScriptAndStyle(n),await S(e,n),this.html=n}async importInstanceEntry(){let e=Se.getCacheScript(this.url)?.code;if(e||(e=await _(this.url,{cache:"no-cache"})),!e)return console.error("load app entry error, pleace check"),Promise.reject();this.scripts.set(this.url,new L({async:!1,code:e,defer:!1,fromHtml:!0,isModule:!!this.url.match(/\.ts$/),url:this.url}))}setScript(e,t){this.scripts.set(e,t instanceof L?t:new L(t))}setStyle(e,t){this.styles.set(e,t)}}const Se=new class{baseSource;cache;constructor(){this.cache=new Map,this.baseSource=new Ae(location.href)}deleteApp(e){this.cache.delete(e)}getApp(e){if(!e)return;const t=this.cache.get(e);return t||Array.from(this.cache.values()).find((t=>t.name===e))}getBaseAppStyle(e){return this.baseSource.getStyle(e)}getCacheHtml(e){const t=Array.from(this.cache.values()).find((t=>t.url===e));return t&&t.source?.rawHtml||""}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}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}setApp(e){this.cache.set(e.appCacheKey,e)}setBaseAppScript(e,t){this.baseSource.setScript(e,t)}setBaseAppStyle(e,t){this.baseSource.setStyle(e,t)}get hasActiveApp(){return Array.from(this.cache.values()).some((e=>e.status!==t.UNMOUNT))}};function ge(e){if(e instanceof HTMLLinkElement){const t=e.getAttribute("rel");let n=e.getAttribute("href");if("stylesheet"===t&&n){n=d(n,location.origin);const t=document.createElement("style");return function(e,t,n){const o=document.createElement("style");O(o),_(e).then((r=>{t.code=r,Se.setBaseAppStyle(e,t),o.textContent=r,w(n)})).catch((e=>{console.error(e),E(n)}))}(n,new A({code:"",fromHtml:!1,url:n}),e),t}}if(e instanceof HTMLScriptElement){let t=e.getAttribute("src");if(t&&"module"!==e.type){t=d(t,location.origin);const n=new L({async:e.hasAttribute("async"),code:"",defer:e.defer||"module"===e.type,fromHtml:!1,isModule:!1});Se.setBaseAppScript(t,n);const o=function(e,t,n){const o=document.createElement("script");return O(o),_(e).then((r=>{t.setCode(r);try{o.textContent=r,e.startsWith("inline-")||n.setAttribute("origin-src",e)}catch(t){console.error(t,e)}w(n)})).catch((e=>{console.error(e),E(n)})),o}(t,n,e);return o||e}}return e}function be(e){return e instanceof HTMLLinkElement||e instanceof HTMLScriptElement}function Ce(e,t,n){if(be(t)){const o=ge(t);return n.call(e,o)}return n.call(e,t)}function ve(){const e=HTMLBodyElement.prototype.appendChild,t=HTMLHeadElement.prototype.appendChild,n=HTMLHeadElement.prototype.appendChild;HTMLBodyElement.prototype.appendChild=function(t){return Ce(this,t,e)},HTMLHeadElement.prototype.appendChild=function(e){return Ce(this,e,t)},HTMLHeadElement.prototype.insertBefore=function(e,t){return function(e,t,n,o){if(be(t)){const r=ge(t);return o.call(e,r,n)}return o.call(e,t,n)}(this,e,t,n)},window.addEventListener("load",(()=>{document.head.querySelectorAll("style").forEach((e=>{e.textContent&&Se.setBaseAppStyle(u(),new A({code:e.textContent,fromHtml:!1,url:""}))}))}))}window.__getAppOrInstance__=function(e){return e?Se.getApp(e):Se};const Le="bk-weweb";class Te{fetchSource;webcomponentTag=Le;constructor(){window.customElements.get(Le)||window.customElements.define(Le,te)}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,te)}start(e){e?.collectBaseSource&&ve(),"function"==typeof e?.fetchSource&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||Le,this.setWebComponentTag()}}const Pe=new Te;e.WeWeb=Te,e.activated=F,e.deactivated=q,e.default=Pe,e.load=G,e.loadApp=z,e.loadInstance=Z,e.mount=function(e,t,n){const o=Se.getApp(e);o&&a((()=>{J(),o.mount(t,n)}))},e.preLoadApp=function(e){h((()=>z({...e,isPreLoad:!0})))},e.preLoadInstance=function(e){h((()=>Z({...e,isPreLoad:!0})))},e.preLoadSource=function(e){h((()=>{!async function(e){const{collectScript:t,collectStyle:n}=await Ee(e),o=[];t&&Array.from(t.entries()).forEach((e=>{o.push(e[1].getCode()),Se.setBaseAppScript(...e)})),n&&Array.from(n.entries()).forEach((e=>{o.push(e[1].getCode()),Se.setBaseAppStyle(...e)})),await Promise.all(o).catch((e=>{throw e}))}(e)}))},e.unload=function(e){Se.deleteApp(e)},e.unmount=Q,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|