@blueking/bk-weweb 0.0.2-5.beta.2 → 0.0.2-5.beta.4

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.
@@ -1188,13 +1188,9 @@ async function execAppScripts(app) {
1188
1188
  }
1189
1189
  const appScriptList = Array.from(app.source.scripts.values()).filter(script => script.fromHtml && !script.initial);
1190
1190
  const commomList = appScriptList.filter(script => (!script.async && !script.defer) || script.isModule);
1191
- let i = 0;
1192
- while (i < commomList.length && commomList[i]) {
1193
- await commomList[i].excuteCode(app);
1194
- i++;
1195
- }
1196
1191
  // 保证同步脚本 和 module类型 最先执行
1197
- // await Promise.all(commomList.map(script => script.excuteCode(app)));
1192
+ await Promise.all(commomList.map(script => script.getCode(app)));
1193
+ await Promise.all(commomList.map(script => script.excuteCode(app)));
1198
1194
  // 最后执行 defer 和 async 脚本
1199
1195
  const deferScriptList = [];
1200
1196
  const asyncScriptList = [];
@@ -1823,7 +1819,12 @@ const createProxyDocument = (rawDocument, app) => {
1823
1819
  return rawDocument.querySelector.call(this, selectors);
1824
1820
  }
1825
1821
  // 返回当前应用程序容器中匹配选择器的元素,如果没有匹配的元素则返回 null
1826
- return app?.container?.querySelector(selectors) ?? null;
1822
+ try {
1823
+ return app?.container?.querySelector(selectors) ?? null;
1824
+ }
1825
+ catch {
1826
+ return null;
1827
+ }
1827
1828
  }
1828
1829
  /**
1829
1830
  * 重写了 Document 类的 querySelectorAll 方法
@@ -1849,7 +1850,7 @@ const createProxyDocument = (rawDocument, app) => {
1849
1850
  }
1850
1851
  function getElementsByTagName(key) {
1851
1852
  if (SPECIAL_ELEMENT_TAG.includes(key) || (!app?.showSourceCode && key.toLocaleLowerCase() === 'script')) {
1852
- return rawDocument.getElementsByName(key);
1853
+ return rawDocument.getElementsByTagName(key);
1853
1854
  }
1854
1855
  return querySelectorAllNew(key);
1855
1856
  }
@@ -2607,9 +2608,10 @@ class EntrySource {
2607
2608
  const nonceStr = randomUrl();
2608
2609
  const scriptInstance = new Script({
2609
2610
  async: false,
2610
- code: script.textContent,
2611
+ code: script.textContent.replace(/var\s+([^=,]+)=/gm, `window.$1 = `),
2611
2612
  defer: script.type === 'module',
2612
2613
  fromHtml: !needReplaceELement,
2614
+ initial: true,
2613
2615
  isModule: script.type === 'module',
2614
2616
  url: nonceStr,
2615
2617
  });
@@ -2623,36 +2625,64 @@ class EntrySource {
2623
2625
  }
2624
2626
  return { replace: script };
2625
2627
  }
2626
- collectScriptAndStyle(parent, app) {
2627
- const children = Array.from(parent.children);
2628
- children.length &&
2629
- children.forEach(child => {
2630
- this.collectScriptAndStyle(child, app);
2631
- });
2632
- children?.forEach(dom => {
2633
- if (dom instanceof HTMLLinkElement) {
2634
- this.collectLink(dom, parent);
2635
- }
2636
- else if (dom instanceof HTMLStyleElement) {
2637
- if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2638
- this.styles.set(randomUrl(), new Style({
2639
- code: dom.textContent || '',
2640
- fromHtml: true,
2641
- url: '',
2642
- }));
2643
- dom.remove();
2644
- }
2645
- }
2646
- else if (dom instanceof HTMLScriptElement) {
2647
- this.collectScript(dom, parent);
2648
- }
2649
- else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2650
- parent.removeChild(dom);
2628
+ collectScriptAndStyle(parent) {
2629
+ const links = Array.from(parent.querySelectorAll('link'));
2630
+ links?.forEach(link => {
2631
+ this.collectLink(link, link.parentElement);
2632
+ });
2633
+ const styles = Array.from(parent.querySelectorAll('style'));
2634
+ styles?.forEach(style => {
2635
+ if (!style.hasAttribute('exclude') && !style.hasAttribute('ignore')) {
2636
+ this.styles.set(randomUrl(), new Style({
2637
+ code: style.textContent || '',
2638
+ fromHtml: true,
2639
+ url: '',
2640
+ }));
2641
+ style.remove();
2651
2642
  }
2652
- else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2653
- dom.setAttribute('src', fillUpPath(dom.getAttribute('src'), this.url));
2643
+ });
2644
+ const scripts = Array.from(parent.querySelectorAll('script'));
2645
+ scripts?.forEach(script => {
2646
+ this.collectScript(script, script.parentElement);
2647
+ });
2648
+ const metas = Array.from(parent.querySelectorAll('meta'));
2649
+ metas?.forEach(meta => {
2650
+ meta.parentElement.removeChild(meta);
2651
+ });
2652
+ const imgs = Array.from(parent.querySelectorAll('img'));
2653
+ imgs?.forEach(img => {
2654
+ if (img.hasAttribute('src')) {
2655
+ img.setAttribute('src', fillUpPath(img.getAttribute('src'), this.url));
2654
2656
  }
2655
2657
  });
2658
+ // const children = Array.from(parent.children);
2659
+ // children?.forEach(dom => {
2660
+ // if (dom instanceof HTMLLinkElement) {
2661
+ // this.collectLink(dom, parent);
2662
+ // } else if (dom instanceof HTMLStyleElement) {
2663
+ // if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2664
+ // this.styles.set(
2665
+ // randomUrl(),
2666
+ // new Style({
2667
+ // code: dom.textContent || '',
2668
+ // fromHtml: true,
2669
+ // url: '',
2670
+ // }),
2671
+ // );
2672
+ // dom.remove();
2673
+ // }
2674
+ // } else if (dom instanceof HTMLScriptElement) {
2675
+ // this.collectScript(dom, parent);
2676
+ // } else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2677
+ // parent.removeChild(dom);
2678
+ // } else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2679
+ // dom.setAttribute('src', fillUpPath(dom.getAttribute('src')!, this.url));
2680
+ // }
2681
+ // });
2682
+ // children.length &&
2683
+ // children.forEach(child => {
2684
+ // this.collectScriptAndStyle(child as HTMLElement, app);
2685
+ // });
2656
2686
  }
2657
2687
  getScript(url) {
2658
2688
  return this.scripts.get(url);
@@ -2690,7 +2720,7 @@ class EntrySource {
2690
2720
  if (wrapElement.__BK_WEWEB_APP_KEY__)
2691
2721
  delete wrapElement.__BK_WEWEB_APP_KEY__;
2692
2722
  wrapElement.innerHTML = htmlStr.replace(/<\/?head>/gim, '').replace(/<\/?body>/i, '');
2693
- this.collectScriptAndStyle(wrapElement, app);
2723
+ this.collectScriptAndStyle(wrapElement);
2694
2724
  await excuteAppStyles(app, wrapElement);
2695
2725
  this.html = wrapElement;
2696
2726
  }