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