@blueking/bk-weweb 0.0.2-5.beta.2 → 0.0.2-5.beta.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/collect-source.js +2 -6
- package/dist/collect-source.js.map +1 -1
- package/dist/index.esm.js +2 -6
- 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/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=document.createElement("style");i.setAttribute("id","SCOPED_CSS_STYLE_ID"),document.body.appendChild(i),i.sheet.disabled=!0;const s=i,c=new Map;Object.getOwnPropertyNames(window).forEach((e=>{!c.has(e)&&e.match(/^[A-Z]/)&&"function"==typeof window[e]&&window[e].toString().includes("[native code]")&&c.set(e,!0)}));const a="id";var l;!function(e){e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.STYLE_RULE=1]="STYLE_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(l||(l={}));const d=e=>Promise.resolve().then(e);function u(e){return e.startsWith("//")?`${location.protocol}${e}`:e.startsWith("http")?e:`${location.protocol}//${e}`}function h(e,t){if(!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e))return e;const{origin:n,pathname:o}=new URL(u(t));return new URL(e,`${n}${o}`.replace(/\.(\w+)$/,"/")).toString()}function p(){return`inline-${m(16)}`}const f=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout((()=>{e({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-t))})}),1)},m=(e,t="abcdefghijklmnopqrstuvwxyz0123456789")=>{let n="";for(let o=0;o<e;o++)n+=t[parseInt((Math.random()*t.length).toString(),10)];return n},y=e=>{if(!e)return!1;if(e.match(/\.js$/))return!1;const{pathname:t}=new URL(u(e));return!t.match(/\.js$/)},w=["currentTarget","srcElement","target"];function E(e,t="custom"){return Object.defineProperties(new CustomEvent(t),w.reduce(((t,n)=>(t[n]={get:()=>e},t)),{}))}function A(e){const t=E(e,"load");"function"!=typeof e.onload?e.dispatchEvent(t):e.onload(t)}function _(e){const t=E(e,"error");"function"!=typeof e.onerror?e.dispatchEvent(t):e.onerror(t)}function S(e,t={}){return Me.fetchSource?Me.fetchSource(e,t):window.fetch(e,t).then((e=>e.text()))}class b{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,n){if(!n.scopeCss||n.container instanceof ShadowRoot){const o=this.resetUrlHost(t.textContent||e.textContent||"",n.url,this.url);if(o&&n.container instanceof ShadowRoot){let e="";o.match(/@font-face\s*\{[^}]+\}/g)?.forEach((t=>{e+=`${t}\n`}));const t=n.sandBox?.rawDocument;if(t&&e){const n=t.createElement("style");n.setAttribute("type","text/css"),n.setAttribute("powered-by","bk-weweb"),n.textContent=e,t?.head?.append(n)}}t.textContent=o}else{const o=Array.from(e.sheet?.cssRules??[]),r=`#${n.name}`,i=this.scopeRule(o,r),s=this.resetUrlHost(i,n.url,this.url);t.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&&ge.getCacheStyle(this.url)){const e=ge.getCacheStyle(this.url);t=e?.code||""}return t||(t=await S(this.url).catch((()=>""))),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot||!e.textContent||!ge.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=u(`${e.join("/")}/`)}return`url("${h(o,t)}")`}))}scopeRule(e,t){let n="";for(const o of e)switch(o.type){case l.STYLE_RULE:n+=this.scopeStyleRule(o,t);break;case l.MEDIA_RULE:n+=this.resetPackRule(o,t,"media");break;case l.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);k(n,e,o);const r=o?document.head:e.container;try{if(this.code)s.textContent=n.textContent||this.code,this.commonScoped(s,n,e),r?.prepend(n),t&&A(t),s.textContent="";else if(t.getAttribute("href"))this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&A(t),this.scoped=!0}));else{const o=new MutationObserver((()=>{t.href&&(o.disconnect(),this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&A(t),this.scoped=!0})))}));o.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&_(t)}return n}scopedStyleCSS(e,t){const n=!(!e.keepAlive||e.container instanceof ShadowRoot);if(k(t,e,n),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;s.textContent=this.code,this.commonScoped(s,t,e),s.textContent=""}else{const n=new MutationObserver((()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,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 g(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 C{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:m(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 we(this))}activated(e,n){if(this.isPreLoad=!1,this.state=t.ACTIVATED,this.container&&e){e instanceof Element&&e.setAttribute(a,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(a,this.name),this.container.innerHTML="";const o=document.createElement("div"),r=`${this.name}-wrapper`;o.setAttribute("id",r),this.source?.styles.size&&g(this,this.container),this.container.appendChild(o),this.sandBox?.activeated(),M(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 be(this.url),await this.source.importEntery(this))}unmount(e){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&ge.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}}let v,L,T;class P{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 C){!function(e){L=void 0,v=L;for(const t in e)B(e,t)||(v?L||(L=t):v=t,T=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;k(o,e,r),i.appendChild(o)}else if(this.executeMemoryScript(e,n),t)return document.createComment("【bk-weweb】dynamic script");if(e instanceof C){const t=e.scopeJs&&e.sandBox?.proxyWindow||window,n=function(e,t){let n,o,r=0;for(const i in e)if(!B(e,i)){if(0===r&&i!==v||1===r&&i!==L)return i;n?(T=i,o=t&&o||i):n=i===T,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 Ae&&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){if(this.isModule){if(this.url?.match(/\.ts$/))e.src=this.url;else{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)}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&&ge.getCacheScript(this.url)&&(t=ge.getCacheScript(this.url)?.code||""),t||(t=await S(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?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}}function B(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 M(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));let r=0;for(;r<o.length&&o[r];)await o[r].excuteCode(e),r++;const i=[],s=[];n.forEach((t=>{(t.defer||t.async)&&(!t.code&&t.defer?i.push(t.excuteCode(e)):s.push(t.excuteCode(e)))})),await Promise.all([...s,...i]).catch((e=>{console.error(e)}))}const{appendChild:x}=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)&&ge.getBaseAppStyle(t.textContent))return document.createComment("【bk-weweb】style is effective in base app");if(!t.hasAttribute("ignore")){const e=new b({code:t.textContent||"",fromHtml:!1,url:""});n.source?.setStyle(p(),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&&x.call(n.container,r.replace),y(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 P({async:!1,code:t.textContent,defer:"module"===t.type,fromHtml:!1,isModule:"module"===t.type});n.source.scripts.set(p(),e);try{e.excuteCode(n)}catch(e){console.error(e)}finally{!e.isModule&&A(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 W(e){return e instanceof HTMLScriptElement||e instanceof HTMLStyleElement||e instanceof HTMLLinkElement}function O(e,t,n){if(t.__BK_WEWEB_APP_KEY__){const o=ge.getApp(t.__BK_WEWEB_APP_KEY__);if(o?.container){const r=R(e,t,o),i=W(t)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),s=i?document.head:o?.container;return k(r,o,i),n.call(s,r)}}return n.call(e,t)}function H(e,t,n,o){if(t.__BK_WEWEB_APP_KEY__){const r=ge.getApp(t.__BK_WEWEB_APP_KEY__);if(r?.container){const i=W(t)&&r.keepAlive&&!(r.container instanceof ShadowRoot),s=i?document.head:r?.container,c=R(e,t,r);return i&&k(c,r,i),n&&!s.contains(n)?x.call(s,c):o.call(s,c,n)}}return o.call(e,t,n)}function k(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:D}=Element.prototype,{append:K,appendChild:N,insertBefore:I,removeChild:U}=HTMLBodyElement.prototype,{appendChild:$,insertBefore:j,removeChild:V}=HTMLHeadElement.prototype,Y=document.head;let J=!1;function F(){r(null),Element.prototype.setAttribute=D,HTMLBodyElement.prototype.appendChild=N,HTMLBodyElement.prototype.append=K,HTMLBodyElement.prototype.removeChild=U,HTMLBodyElement.prototype.insertBefore=I,HTMLHeadElement.prototype.appendChild=$,HTMLHeadElement.prototype.insertBefore=j,HTMLHeadElement.prototype.removeChild=V,J=!1}function G(){J||(J=!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__&&ge.getApp(this.__BK_WEWEB_APP_KEY__||"")?D.call(this,e,h(t,ge.getApp(this.__BK_WEWEB_APP_KEY__).url)):D.call(this,e,t)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&W(e)?$.call(Y,e):O(this,e,N)},HTMLBodyElement.prototype.append=function(...e){e.forEach((e=>{if(e.__KEEP_ALIVE__&&W(e))return $.call(Y,e);O(this,e,N)}))},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(e,t){return H(this,e,t,j)},HTMLBodyElement.prototype.insertBefore=function(e,t){return H(this,e,t,j)},HTMLBodyElement.prototype.removeChild=function(e){const t=ge.getApp(e.__BK_WEWEB_APP_KEY__);return t?.container?.contains(e)?U.call(t.container,e):this.contains(e)?U.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild)}function q(e,n,o){const r=ge.getApp(e);r?.status===t.DEACTIVATED&&r.keepAlive?d((()=>{G(),r.activated(n,o)})):r&&d((()=>{G(),r.mount(n,o)}))}function z(e){const n=ge.getApp(e);n&&[t.ACTIVATED,t.MOUNTED].includes(n.status)&&(n.keepAlive?n.deactivated():n.unmount()),ge.hasActiveApp||F()}async function Z(t){return G(),t.mode===e.WewebMode.INSTANCE?await X(t):await Q(t)}async function Q(e){G();let t=ge.getApp(e.id);return t||(t=new Ae(e),ge.setApp(t)),await t.start(),t}function X(e){return G(),new Promise((n=>{let o=ge.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 C(e),ge.setApp(o),o.start().then((()=>n(o)))}))}function ee(e){const n=ge.getApp(e);n&&n.status!==t.UNMOUNT&&n.unmount(),ge.hasActiveApp||F()}var te,ne;!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"}(te||(te={}));class oe extends HTMLElement{appUrl="";connected=!1;static get observedAttributes(){return[te.url]}getBooleanAttr(e){return this.hasAttribute(e)?"false"!==this.getAttribute(e):void 0}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr(te.setShodowDom)&&this.attachShadow({mode:"open"});const e=ge.getApp(this.appKey);!e||e.url!==this.appUrl||!e.isPreLoad&&e.status!==t.UNMOUNT?await Z(this.appProps):q(this.appKey,this.shadowRoot??this)}attributeChangedCallback(e,t,n){if(e!==te.url||this[e]===n||!this.connected)return;this.appUrl=n;const o=ge.getApp(this.appKey);(this.connected||o)&&this.handleAttributeChanged()}async connectedCallback(){this.getBooleanAttr(te.setShodowDom)&&!this.shadowRoot&&this.attachShadow({delegatesFocus:!1,mode:"open"}),await Z(this.appProps),q(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?z(this.appKey):ee(this.appKey)}get appData(){if(this.hasAttribute(te.data))try{return JSON.parse(this.getAttribute(te.data))}catch{}return{}}get appKey(){return this.getAttribute(te.id)||this.getAttribute(te.url)}get appProps(){return this.getAttribute("mode")===e.WewebMode.INSTANCE?{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(te.keepAlive),mode:e.WewebMode.INSTANCE,scopeCss:this.getBooleanAttr(te.scopeCss)&&!this.getBooleanAttr(te.setShodowDom),scopeJs:this.getBooleanAttr(te.scopeJs),showSourceCode:this.getBooleanAttr(te.showSourceCode),url:this.getAttribute(te.url)}:{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(te.keepAlive),mode:e.WewebMode.APP,scopeCss:!this.getBooleanAttr(te.setShodowDom),scopeJs:!this.getBooleanAttr(te.scopeJs),scopeLocation:this.getBooleanAttr(te.scopeLocation),showSourceCode:this.getBooleanAttr(te.showSourceCode),url:this.getAttribute(te.url)}}}e.WewebMode=void 0,(ne=e.WewebMode||(e.WewebMode={})).APP="app",ne.CONFIG="config",ne.INSTANCE="js";const re=["System","__cjsWrapper","production"!==process.env.NODE_ENV?"__REACT_DEVTOOLS_GLOBAL_HOOK__":""],ie=["__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],se=["window","self","globalThis"],ce=["location","history"],ae={__bk_pop_manager:!0,__bk_zIndex_manager:!0,i18n:!0},le="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,...ae}:ae,de=["body","html","head"],ue=(e,t)=>{const n=new Proxy({},{get(n,o){if("querySelector"===o)return t.container instanceof ShadowRoot?t.container.querySelector.bind(t.container):r.bind(e.body);if("querySelectorAll"===o)return t.container instanceof ShadowRoot?t.container.querySelectorAll.bind(t.container):i.bind(e.body);if("insertAdjacentElement"===o)return e.body.insertAdjacentElement.bind(e.body);const s=Reflect.get(e.body,o);return"function"==typeof s?s.bind(e.body):s}});function o(n,o){const r=e.createElement(n,o);return r.__BK_WEWEB_APP_KEY__=t.appCacheKey,r}function r(o){return o===n?t.container instanceof ShadowRoot?t.container:e.body:de.includes(o)?t?.container instanceof ShadowRoot?t?.container:e.querySelector.call(this,o):t?.container?.querySelector(o)??null}function i(n){return de.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 de.includes(n)||!t?.showSourceCode&&"script"===n.toLocaleLowerCase()?e.getElementsByName(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 he=/^class\b/,pe=/^function\b\s[A-Z].*/,fe=new WeakMap;const me=new WeakMap;function ye(e,t){if(me.has(t))return me.get(t);if("function"==typeof t&&!function(e){if(e.prototype?.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1)return!0;if(fe.has(e))return fe.get(e);const t=pe.test(e.toString())||he.test(e.toString());return fe.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 me.set(t,n),n}return t}class we{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.fakeWindow.__POWERED_BY_BK_WEWEB__=!0,this.fakeWindow.__BK_WEWEB_APP_KEY__=e.appCacheKey,this.rawWindow=n,this.rawDocument=ue(document,e),this.fakeWindow.rawWindow=n,this.fakeWindow.rawDocument=document;const{resetWindowFunction:o}=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=o,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${m(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(c.has(o)||o===Symbol.unscopables)return n[o];if(le[o])return this.fakeWindow[o];if(se.includes(o))return this.proxyWindow;if("document"===o)return e.registerRunningApp(),this.rawDocument;if("eval"===o)return e.registerRunningApp(),eval;if(ce.includes(o)&&this.app instanceof Ae&&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)||ie.includes(o))return Reflect.get(t,o);const r=Reflect.get(n,o);return ye(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)=>c.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(ce.includes(t)&&this.app instanceof Ae&&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)||ie.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))}!re.includes(t)||Reflect.has(n,t)||ie.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=ue(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 Ee="about:blank";class Ae{state=t.UNSET;container;data;iframe=null;initSource;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:m(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 we(this)),this.container instanceof HTMLElement&&this.container.setAttribute(a,this.name)}activated(e,n){if(this.isPreLoad=!1,this.state=t.ACTIVATED,e&&this.container){e instanceof Element&&e.setAttribute(a,this.name);const t=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{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(u(this.url)),o=navigator.userAgent.indexOf("Chrome")>-1;if(t.setAttribute("src",`${o?Ee: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!==Ee&&(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){if(this.isPreLoad=!1,this.container=e??this.container,this.state=t.MOUNTING,this.container){this.container instanceof Element&&this.container.setAttribute(a,this.name);const e=this.source.html.cloneNode(!0),o=document.createDocumentFragment();Array.from(e.childNodes).forEach((e=>{o.appendChild(e)})),this.container.innerHTML="",this.container.appendChild(o),this.sandBox?.activeated(this.data),M(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){const e=await this.createIframe();this.iframe=e}this.source=new be(this.url),await this.source.importEntery(this)}}unmount(e=!1){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&ge.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 _e(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=ge.getCacheScript(e);n.set(e,new P({async:!1,code:t?.code||"",defer:!1,fromHtml:!1,initial:!0,isModule:!1,url:e}))}else if(t.pathname.match(/\.css$/)){const t=ge.getCacheStyle(e);o.set(e,new b({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 Se=["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"];class be{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=h(i,this.url),r=document.createComment(`【bk-weweb】style with href=${i}`);let o=this.getStyle(i);return o||(o=new b({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",h(i,this.url)),{replace:e})}collectScript(e,t,n=!1){if(e.hasAttribute("ignore")||y(e.getAttribute("src"))||e.hasAttribute("type")&&!Se.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=h(r,this.url);let i=this.getScript(r);return i||(i=new P({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=p(),i=new P({async:!1,code:e.textContent,defer:"module"===e.type,fromHtml:!n,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,t){const n=Array.from(e.children);n.length&&n.forEach((e=>{this.collectScriptAndStyle(e,t)})),n?.forEach((t=>{t instanceof HTMLLinkElement?this.collectLink(t,e):t instanceof HTMLStyleElement?t.hasAttribute("exclude")||t.hasAttribute("ignore")||(this.styles.set(p(),new b({code:t.textContent||"",fromHtml:!0,url:""})),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",h(t.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 _e(e.initSource);t&&(this.scripts=t),n&&(this.styles=n)}e instanceof Ae?await this.importHtmlEntry(e):e instanceof C&&await this.importInstanceEntry()}async importHtmlEntry(e){let t=ge.getCacheHtml(this.url);if(!t&&(t=await S(u(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,e),await g(e,n),this.html=n}async importInstanceEntry(){let e=ge.getCacheScript(this.url)?.code;if(e||(e=await S(this.url,{cache:"no-cache"})),!e)return console.error("load app entry error, pleace check"),Promise.reject();this.scripts.set(this.url,new P({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 P?t:new P(t))}setStyle(e,t){this.styles.set(e,t)}}const ge=new class{baseSource;cache;constructor(){this.cache=new Map,this.baseSource=new be(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 Ce(e){if(e instanceof HTMLLinkElement){const t=e.getAttribute("rel");let n=e.getAttribute("href");if("stylesheet"===t&&n){n=h(n,location.origin);const t=document.createElement("style");return function(e,t,n){const o=document.createElement("style");k(o),S(e).then((r=>{t.code=r,ge.setBaseAppStyle(e,t),o.textContent=r,A(n)})).catch((e=>{console.error(e),_(n)}))}(n,new b({code:"",fromHtml:!1,url:n}),e),t}}if(e instanceof HTMLScriptElement){let t=e.getAttribute("src");if(t&&"module"!==e.type){t=h(t,location.origin);const n=new P({async:e.hasAttribute("async"),code:"",defer:e.defer||"module"===e.type,fromHtml:!1,isModule:!1});ge.setBaseAppScript(t,n);const o=function(e,t,n){const o=document.createElement("script");return k(o),S(e).then((r=>{t.setCode(r);try{o.textContent=r,e.startsWith("inline-")||n.setAttribute("origin-src",e)}catch(t){console.error(t,e)}A(n)})).catch((e=>{console.error(e),_(n)})),o}(t,n,e);return o||e}}return e}function ve(e){return e instanceof HTMLLinkElement||e instanceof HTMLScriptElement}function Le(e,t,n){if(ve(t)){const o=Ce(t);return n.call(e,o)}return n.call(e,t)}function Te(){const e=HTMLBodyElement.prototype.appendChild,t=HTMLHeadElement.prototype.appendChild,n=HTMLHeadElement.prototype.appendChild;HTMLBodyElement.prototype.appendChild=function(t){return Le(this,t,e)},HTMLHeadElement.prototype.appendChild=function(e){return Le(this,e,t)},HTMLHeadElement.prototype.insertBefore=function(e,t){return function(e,t,n,o){if(ve(t)){const r=Ce(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&&ge.setBaseAppStyle(p(),new b({code:e.textContent,fromHtml:!1,url:""}))}))}))}window.__getAppOrInstance__=function(e){return e?ge.getApp(e):ge};const Pe="bk-weweb";class Be{fetchSource;webcomponentTag=Pe;constructor(){window.customElements.get(Pe)||window.customElements.define(Pe,oe)}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,oe)}start(e){e?.collectBaseSource&&Te(),"function"==typeof e?.fetchSource&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||Pe,this.setWebComponentTag()}}const Me=new Be;e.WeWeb=Be,e.activated=q,e.deactivated=z,e.default=Me,e.load=Z,e.loadApp=Q,e.loadInstance=X,e.mount=function(e,t,n){const o=ge.getApp(e);o&&d((()=>{G(),o.mount(t,n)}))},e.preLoadApp=function(e){f((()=>Q({...e,isPreLoad:!0})))},e.preLoadInstance=function(e){f((()=>X({...e,isPreLoad:!0})))},e.preLoadSource=function(e){f((()=>{!async function(e){const{collectScript:t,collectStyle:n}=await _e(e),o=[];t&&Array.from(t.entries()).forEach((e=>{o.push(e[1].getCode()),ge.setBaseAppScript(...e)})),n&&Array.from(n.entries()).forEach((e=>{o.push(e[1].getCode()),ge.setBaseAppStyle(...e)})),await Promise.all(o).catch((e=>{throw e}))}(e)}))},e.unload=function(e){ge.deleteApp(e)},e.unmount=ee,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=document.createElement("style");i.setAttribute("id","SCOPED_CSS_STYLE_ID"),document.body.appendChild(i),i.sheet.disabled=!0;const s=i,c=new Map;Object.getOwnPropertyNames(window).forEach((e=>{!c.has(e)&&e.match(/^[A-Z]/)&&"function"==typeof window[e]&&window[e].toString().includes("[native code]")&&c.set(e,!0)}));const a="id";var l;!function(e){e[e.MEDIA_RULE=4]="MEDIA_RULE",e[e.STYLE_RULE=1]="STYLE_RULE",e[e.SUPPORTS_RULE=12]="SUPPORTS_RULE"}(l||(l={}));const d=e=>Promise.resolve().then(e);function u(e){return e.startsWith("//")?`${location.protocol}${e}`:e.startsWith("http")?e:`${location.protocol}//${e}`}function h(e,t){if(!e||/^((((ht|f)tps?)|file):)?\/\//.test(e)||/^(data|blob):/.test(e))return e;const{origin:n,pathname:o}=new URL(u(t));return new URL(e,`${n}${o}`.replace(/\.(\w+)$/,"/")).toString()}function p(){return`inline-${m(16)}`}const f=window.requestIdleCallback||function(e){const t=Date.now();return setTimeout((()=>{e({didTimeout:!1,timeRemaining:()=>Math.max(0,50-(Date.now()-t))})}),1)},m=(e,t="abcdefghijklmnopqrstuvwxyz0123456789")=>{let n="";for(let o=0;o<e;o++)n+=t[parseInt((Math.random()*t.length).toString(),10)];return n},y=e=>{if(!e)return!1;if(e.match(/\.js$/))return!1;const{pathname:t}=new URL(u(e));return!t.match(/\.js$/)},w=["currentTarget","srcElement","target"];function E(e,t="custom"){return Object.defineProperties(new CustomEvent(t),w.reduce(((t,n)=>(t[n]={get:()=>e},t)),{}))}function A(e){const t=E(e,"load");"function"!=typeof e.onload?e.dispatchEvent(t):e.onload(t)}function _(e){const t=E(e,"error");"function"!=typeof e.onerror?e.dispatchEvent(t):e.onerror(t)}function S(e,t={}){return Me.fetchSource?Me.fetchSource(e,t):window.fetch(e,t).then((e=>e.text()))}class b{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,n){if(!n.scopeCss||n.container instanceof ShadowRoot){const o=this.resetUrlHost(t.textContent||e.textContent||"",n.url,this.url);if(o&&n.container instanceof ShadowRoot){let e="";o.match(/@font-face\s*\{[^}]+\}/g)?.forEach((t=>{e+=`${t}\n`}));const t=n.sandBox?.rawDocument;if(t&&e){const n=t.createElement("style");n.setAttribute("type","text/css"),n.setAttribute("powered-by","bk-weweb"),n.textContent=e,t?.head?.append(n)}}t.textContent=o}else{const o=Array.from(e.sheet?.cssRules??[]),r=`#${n.name}`,i=this.scopeRule(o,r),s=this.resetUrlHost(i,n.url,this.url);t.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&&ge.getCacheStyle(this.url)){const e=ge.getCacheStyle(this.url);t=e?.code||""}return t||(t=await S(this.url).catch((()=>""))),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot||!e.textContent||!ge.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=u(`${e.join("/")}/`)}return`url("${h(o,t)}")`}))}scopeRule(e,t){let n="";for(const o of e)switch(o.type){case l.STYLE_RULE:n+=this.scopeStyleRule(o,t);break;case l.MEDIA_RULE:n+=this.resetPackRule(o,t,"media");break;case l.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);k(n,e,o);const r=o?document.head:e.container;try{if(this.code)s.textContent=n.textContent||this.code,this.commonScoped(s,n,e),r?.prepend(n),t&&A(t),s.textContent="";else if(t.getAttribute("href"))this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&A(t),this.scoped=!0}));else{const o=new MutationObserver((()=>{t.href&&(o.disconnect(),this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then((()=>{this.scopedStyleCSS(e,n),t.remove(),r?.prepend(n),t&&A(t),this.scoped=!0})))}));o.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&_(t)}return n}scopedStyleCSS(e,t){const n=!(!e.keepAlive||e.container instanceof ShadowRoot);if(k(t,e,n),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;s.textContent=this.code,this.commonScoped(s,t,e),s.textContent=""}else{const n=new MutationObserver((()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,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 g(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 C{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:m(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 we(this))}activated(e,n){if(this.isPreLoad=!1,this.state=t.ACTIVATED,this.container&&e){e instanceof Element&&e.setAttribute(a,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(a,this.name),this.container.innerHTML="";const o=document.createElement("div"),r=`${this.name}-wrapper`;o.setAttribute("id",r),this.source?.styles.size&&g(this,this.container),this.container.appendChild(o),this.sandBox?.activeated(),M(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 be(this.url),await this.source.importEntery(this))}unmount(e){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&ge.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}}let v,L,T;class P{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 C){!function(e){L=void 0,v=L;for(const t in e)B(e,t)||(v?L||(L=t):v=t,T=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;k(o,e,r),i.appendChild(o)}else if(this.executeMemoryScript(e,n),t)return document.createComment("【bk-weweb】dynamic script");if(e instanceof C){const t=e.scopeJs&&e.sandBox?.proxyWindow||window,n=function(e,t){let n,o,r=0;for(const i in e)if(!B(e,i)){if(0===r&&i!==v||1===r&&i!==L)return i;n?(T=i,o=t&&o||i):n=i===T,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 Ae&&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){if(this.isModule){if(this.url?.match(/\.ts$/))e.src=this.url;else{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)}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&&ge.getCacheScript(this.url)&&(t=ge.getCacheScript(this.url)?.code||""),t||(t=await S(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?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}}function B(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 M(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.getCode(e)))),await Promise.all(o.map((t=>t.excuteCode(e))));const r=[],i=[];n.forEach((t=>{(t.defer||t.async)&&(!t.code&&t.defer?r.push(t.excuteCode(e)):i.push(t.excuteCode(e)))})),await Promise.all([...i,...r]).catch((e=>{console.error(e)}))}const{appendChild:x}=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)&&ge.getBaseAppStyle(t.textContent))return document.createComment("【bk-weweb】style is effective in base app");if(!t.hasAttribute("ignore")){const e=new b({code:t.textContent||"",fromHtml:!1,url:""});n.source?.setStyle(p(),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&&x.call(n.container,r.replace),y(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 P({async:!1,code:t.textContent,defer:"module"===t.type,fromHtml:!1,isModule:"module"===t.type});n.source.scripts.set(p(),e);try{e.excuteCode(n)}catch(e){console.error(e)}finally{!e.isModule&&A(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 W(e){return e instanceof HTMLScriptElement||e instanceof HTMLStyleElement||e instanceof HTMLLinkElement}function O(e,t,n){if(t.__BK_WEWEB_APP_KEY__){const o=ge.getApp(t.__BK_WEWEB_APP_KEY__);if(o?.container){const r=R(e,t,o),i=W(t)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),s=i?document.head:o?.container;return k(r,o,i),n.call(s,r)}}return n.call(e,t)}function H(e,t,n,o){if(t.__BK_WEWEB_APP_KEY__){const r=ge.getApp(t.__BK_WEWEB_APP_KEY__);if(r?.container){const i=W(t)&&r.keepAlive&&!(r.container instanceof ShadowRoot),s=i?document.head:r?.container,c=R(e,t,r);return i&&k(c,r,i),n&&!s.contains(n)?x.call(s,c):o.call(s,c,n)}}return o.call(e,t,n)}function k(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:D}=Element.prototype,{append:K,appendChild:N,insertBefore:I,removeChild:U}=HTMLBodyElement.prototype,{appendChild:$,insertBefore:j,removeChild:V}=HTMLHeadElement.prototype,Y=document.head;let J=!1;function F(){r(null),Element.prototype.setAttribute=D,HTMLBodyElement.prototype.appendChild=N,HTMLBodyElement.prototype.append=K,HTMLBodyElement.prototype.removeChild=U,HTMLBodyElement.prototype.insertBefore=I,HTMLHeadElement.prototype.appendChild=$,HTMLHeadElement.prototype.insertBefore=j,HTMLHeadElement.prototype.removeChild=V,J=!1}function G(){J||(J=!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__&&ge.getApp(this.__BK_WEWEB_APP_KEY__||"")?D.call(this,e,h(t,ge.getApp(this.__BK_WEWEB_APP_KEY__).url)):D.call(this,e,t)},HTMLBodyElement.prototype.appendChild=function(e){return e.__KEEP_ALIVE__&&W(e)?$.call(Y,e):O(this,e,N)},HTMLBodyElement.prototype.append=function(...e){e.forEach((e=>{if(e.__KEEP_ALIVE__&&W(e))return $.call(Y,e);O(this,e,N)}))},HTMLHeadElement.prototype.appendChild=HTMLBodyElement.prototype.appendChild,HTMLHeadElement.prototype.insertBefore=function(e,t){return H(this,e,t,j)},HTMLBodyElement.prototype.insertBefore=function(e,t){return H(this,e,t,j)},HTMLBodyElement.prototype.removeChild=function(e){const t=ge.getApp(e.__BK_WEWEB_APP_KEY__);return t?.container?.contains(e)?U.call(t.container,e):this.contains(e)?U.call(this,e):e},HTMLHeadElement.prototype.removeChild=HTMLBodyElement.prototype.removeChild)}function q(e,n,o){const r=ge.getApp(e);r?.status===t.DEACTIVATED&&r.keepAlive?d((()=>{G(),r.activated(n,o)})):r&&d((()=>{G(),r.mount(n,o)}))}function z(e){const n=ge.getApp(e);n&&[t.ACTIVATED,t.MOUNTED].includes(n.status)&&(n.keepAlive?n.deactivated():n.unmount()),ge.hasActiveApp||F()}async function Z(t){return G(),t.mode===e.WewebMode.INSTANCE?await X(t):await Q(t)}async function Q(e){G();let t=ge.getApp(e.id);return t||(t=new Ae(e),ge.setApp(t)),await t.start(),t}function X(e){return G(),new Promise((n=>{let o=ge.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 C(e),ge.setApp(o),o.start().then((()=>n(o)))}))}function ee(e){const n=ge.getApp(e);n&&n.status!==t.UNMOUNT&&n.unmount(),ge.hasActiveApp||F()}var te,ne;!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"}(te||(te={}));class oe extends HTMLElement{appUrl="";connected=!1;static get observedAttributes(){return[te.url]}getBooleanAttr(e){return this.hasAttribute(e)?"false"!==this.getAttribute(e):void 0}async handleAttributeChanged(){if(!this.appKey)return;this.getBooleanAttr(te.setShodowDom)&&this.attachShadow({mode:"open"});const e=ge.getApp(this.appKey);!e||e.url!==this.appUrl||!e.isPreLoad&&e.status!==t.UNMOUNT?await Z(this.appProps):q(this.appKey,this.shadowRoot??this)}attributeChangedCallback(e,t,n){if(e!==te.url||this[e]===n||!this.connected)return;this.appUrl=n;const o=ge.getApp(this.appKey);(this.connected||o)&&this.handleAttributeChanged()}async connectedCallback(){this.getBooleanAttr(te.setShodowDom)&&!this.shadowRoot&&this.attachShadow({delegatesFocus:!1,mode:"open"}),await Z(this.appProps),q(this.appKey,this.shadowRoot??this),this.connected=!0}disconnectedCallback(){this.connected=!1,this.appProps.keepAlive?z(this.appKey):ee(this.appKey)}get appData(){if(this.hasAttribute(te.data))try{return JSON.parse(this.getAttribute(te.data))}catch{}return{}}get appKey(){return this.getAttribute(te.id)||this.getAttribute(te.url)}get appProps(){return this.getAttribute("mode")===e.WewebMode.INSTANCE?{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(te.keepAlive),mode:e.WewebMode.INSTANCE,scopeCss:this.getBooleanAttr(te.scopeCss)&&!this.getBooleanAttr(te.setShodowDom),scopeJs:this.getBooleanAttr(te.scopeJs),showSourceCode:this.getBooleanAttr(te.showSourceCode),url:this.getAttribute(te.url)}:{container:this.shadowRoot??this,data:this.appData,id:this.appKey,keepAlive:this.getBooleanAttr(te.keepAlive),mode:e.WewebMode.APP,scopeCss:!this.getBooleanAttr(te.setShodowDom),scopeJs:!this.getBooleanAttr(te.scopeJs),scopeLocation:this.getBooleanAttr(te.scopeLocation),showSourceCode:this.getBooleanAttr(te.showSourceCode),url:this.getAttribute(te.url)}}}e.WewebMode=void 0,(ne=e.WewebMode||(e.WewebMode={})).APP="app",ne.CONFIG="config",ne.INSTANCE="js";const re=["System","__cjsWrapper","production"!==process.env.NODE_ENV?"__REACT_DEVTOOLS_GLOBAL_HOOK__":""],ie=["__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],se=["window","self","globalThis"],ce=["location","history"],ae={__bk_pop_manager:!0,__bk_zIndex_manager:!0,i18n:!0},le="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,...ae}:ae,de=["body","html","head"],ue=(e,t)=>{const n=new Proxy({},{get(n,o){if("querySelector"===o)return t.container instanceof ShadowRoot?t.container.querySelector.bind(t.container):r.bind(e.body);if("querySelectorAll"===o)return t.container instanceof ShadowRoot?t.container.querySelectorAll.bind(t.container):i.bind(e.body);if("insertAdjacentElement"===o)return e.body.insertAdjacentElement.bind(e.body);const s=Reflect.get(e.body,o);return"function"==typeof s?s.bind(e.body):s}});function o(n,o){const r=e.createElement(n,o);return r.__BK_WEWEB_APP_KEY__=t.appCacheKey,r}function r(o){return o===n?t.container instanceof ShadowRoot?t.container:e.body:de.includes(o)?t?.container instanceof ShadowRoot?t?.container:e.querySelector.call(this,o):t?.container?.querySelector(o)??null}function i(n){return de.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 de.includes(n)||!t?.showSourceCode&&"script"===n.toLocaleLowerCase()?e.getElementsByName(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 he=/^class\b/,pe=/^function\b\s[A-Z].*/,fe=new WeakMap;const me=new WeakMap;function ye(e,t){if(me.has(t))return me.get(t);if("function"==typeof t&&!function(e){if(e.prototype?.constructor===e&&Object.getOwnPropertyNames(e.prototype).length>1)return!0;if(fe.has(e))return fe.get(e);const t=pe.test(e.toString())||he.test(e.toString());return fe.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 me.set(t,n),n}return t}class we{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.fakeWindow.__POWERED_BY_BK_WEWEB__=!0,this.fakeWindow.__BK_WEWEB_APP_KEY__=e.appCacheKey,this.rawWindow=n,this.rawDocument=ue(document,e),this.fakeWindow.rawWindow=n,this.fakeWindow.rawDocument=document;const{resetWindowFunction:o}=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=o,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${m(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(c.has(o)||o===Symbol.unscopables)return n[o];if(le[o])return this.fakeWindow[o];if(se.includes(o))return this.proxyWindow;if("document"===o)return e.registerRunningApp(),this.rawDocument;if("eval"===o)return e.registerRunningApp(),eval;if(ce.includes(o)&&this.app instanceof Ae&&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)||ie.includes(o))return Reflect.get(t,o);const r=Reflect.get(n,o);return ye(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)=>c.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(ce.includes(t)&&this.app instanceof Ae&&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)||ie.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))}!re.includes(t)||Reflect.has(n,t)||ie.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=ue(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 Ee="about:blank";class Ae{state=t.UNSET;container;data;iframe=null;initSource;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:m(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 we(this)),this.container instanceof HTMLElement&&this.container.setAttribute(a,this.name)}activated(e,n){if(this.isPreLoad=!1,this.state=t.ACTIVATED,e&&this.container){e instanceof Element&&e.setAttribute(a,this.name);const t=document.createDocumentFragment();Array.from(this.container.childNodes).forEach((e=>{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(u(this.url)),o=navigator.userAgent.indexOf("Chrome")>-1;if(t.setAttribute("src",`${o?Ee: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!==Ee&&(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){if(this.isPreLoad=!1,this.container=e??this.container,this.state=t.MOUNTING,this.container){this.container instanceof Element&&this.container.setAttribute(a,this.name);const e=this.source.html.cloneNode(!0),o=document.createDocumentFragment();Array.from(e.childNodes).forEach((e=>{o.appendChild(e)})),this.container.innerHTML="",this.container.appendChild(o),this.sandBox?.activeated(this.data),M(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){const e=await this.createIframe();this.iframe=e}this.source=new be(this.url),await this.source.importEntery(this)}}unmount(e=!1){this.state=t.UNMOUNT,this.sandBox?.deactivated(),e&&ge.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 _e(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=ge.getCacheScript(e);n.set(e,new P({async:!1,code:t?.code||"",defer:!1,fromHtml:!1,initial:!0,isModule:!1,url:e}))}else if(t.pathname.match(/\.css$/)){const t=ge.getCacheStyle(e);o.set(e,new b({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 Se=["text/javascript","text/ecmascript","application/javascript","application/ecmascript","module"];class be{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=h(i,this.url),r=document.createComment(`【bk-weweb】style with href=${i}`);let o=this.getStyle(i);return o||(o=new b({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",h(i,this.url)),{replace:e})}collectScript(e,t,n=!1){if(e.hasAttribute("ignore")||y(e.getAttribute("src"))||e.hasAttribute("type")&&!Se.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=h(r,this.url);let i=this.getScript(r);return i||(i=new P({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=p(),i=new P({async:!1,code:e.textContent,defer:"module"===e.type,fromHtml:!n,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,t){const n=Array.from(e.children);n.length&&n.forEach((e=>{this.collectScriptAndStyle(e,t)})),n?.forEach((t=>{t instanceof HTMLLinkElement?this.collectLink(t,e):t instanceof HTMLStyleElement?t.hasAttribute("exclude")||t.hasAttribute("ignore")||(this.styles.set(p(),new b({code:t.textContent||"",fromHtml:!0,url:""})),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",h(t.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 _e(e.initSource);t&&(this.scripts=t),n&&(this.styles=n)}e instanceof Ae?await this.importHtmlEntry(e):e instanceof C&&await this.importInstanceEntry()}async importHtmlEntry(e){let t=ge.getCacheHtml(this.url);if(!t&&(t=await S(u(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,e),await g(e,n),this.html=n}async importInstanceEntry(){let e=ge.getCacheScript(this.url)?.code;if(e||(e=await S(this.url,{cache:"no-cache"})),!e)return console.error("load app entry error, pleace check"),Promise.reject();this.scripts.set(this.url,new P({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 P?t:new P(t))}setStyle(e,t){this.styles.set(e,t)}}const ge=new class{baseSource;cache;constructor(){this.cache=new Map,this.baseSource=new be(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 Ce(e){if(e instanceof HTMLLinkElement){const t=e.getAttribute("rel");let n=e.getAttribute("href");if("stylesheet"===t&&n){n=h(n,location.origin);const t=document.createElement("style");return function(e,t,n){const o=document.createElement("style");k(o),S(e).then((r=>{t.code=r,ge.setBaseAppStyle(e,t),o.textContent=r,A(n)})).catch((e=>{console.error(e),_(n)}))}(n,new b({code:"",fromHtml:!1,url:n}),e),t}}if(e instanceof HTMLScriptElement){let t=e.getAttribute("src");if(t&&"module"!==e.type){t=h(t,location.origin);const n=new P({async:e.hasAttribute("async"),code:"",defer:e.defer||"module"===e.type,fromHtml:!1,isModule:!1});ge.setBaseAppScript(t,n);const o=function(e,t,n){const o=document.createElement("script");return k(o),S(e).then((r=>{t.setCode(r);try{o.textContent=r,e.startsWith("inline-")||n.setAttribute("origin-src",e)}catch(t){console.error(t,e)}A(n)})).catch((e=>{console.error(e),_(n)})),o}(t,n,e);return o||e}}return e}function ve(e){return e instanceof HTMLLinkElement||e instanceof HTMLScriptElement}function Le(e,t,n){if(ve(t)){const o=Ce(t);return n.call(e,o)}return n.call(e,t)}function Te(){const e=HTMLBodyElement.prototype.appendChild,t=HTMLHeadElement.prototype.appendChild,n=HTMLHeadElement.prototype.appendChild;HTMLBodyElement.prototype.appendChild=function(t){return Le(this,t,e)},HTMLHeadElement.prototype.appendChild=function(e){return Le(this,e,t)},HTMLHeadElement.prototype.insertBefore=function(e,t){return function(e,t,n,o){if(ve(t)){const r=Ce(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&&ge.setBaseAppStyle(p(),new b({code:e.textContent,fromHtml:!1,url:""}))}))}))}window.__getAppOrInstance__=function(e){return e?ge.getApp(e):ge};const Pe="bk-weweb";class Be{fetchSource;webcomponentTag=Pe;constructor(){window.customElements.get(Pe)||window.customElements.define(Pe,oe)}setWebComponentTag(){window.customElements.get(this.webcomponentTag)||window.customElements.define(this.webcomponentTag,oe)}start(e){e?.collectBaseSource&&Te(),"function"==typeof e?.fetchSource&&(this.fetchSource=e.fetchSource),this.webcomponentTag=e?.webcomponentTag||Pe,this.setWebComponentTag()}}const Me=new Be;e.WeWeb=Be,e.activated=q,e.deactivated=z,e.default=Me,e.load=Z,e.loadApp=Q,e.loadInstance=X,e.mount=function(e,t,n){const o=ge.getApp(e);o&&d((()=>{G(),o.mount(t,n)}))},e.preLoadApp=function(e){f((()=>Q({...e,isPreLoad:!0})))},e.preLoadInstance=function(e){f((()=>X({...e,isPreLoad:!0})))},e.preLoadSource=function(e){f((()=>{!async function(e){const{collectScript:t,collectStyle:n}=await _e(e),o=[];t&&Array.from(t.entries()).forEach((e=>{o.push(e[1].getCode()),ge.setBaseAppScript(...e)})),n&&Array.from(n.entries()).forEach((e=>{o.push(e[1].getCode()),ge.setBaseAppStyle(...e)})),await Promise.all(o).catch((e=>{throw e}))}(e)}))},e.unload=function(e){ge.deleteApp(e)},e.unmount=ee,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|