@playcraft/devkit 1.0.22 → 1.0.24
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.
|
@@ -64,9 +64,13 @@ const linkRegex=/<link\s+[^>]*?\/?>/gi;while((match=linkRegex.exec(headHtml))!==
|
|
|
64
64
|
* @param {string} headContent - 从原始 HTML 提取的 head 内容
|
|
65
65
|
* @returns {Promise<string>} 完整的自解压 HTML
|
|
66
66
|
*/async generateDecompressHtml(compressedBase64,fflateLibCode,headContent=""){// 可读的解压代码(保持格式以便维护)
|
|
67
|
-
|
|
67
|
+
// hasHeadContent: 标记 extractHeadContent 是否从原始 HTML 提取了外部资源引用
|
|
68
|
+
// - true: 原始 HTML 有 <head>,外部资源已提取到模板 head,避免解压时重复
|
|
69
|
+
// - false: 原始 HTML 是片段(如 Adikteev),外部资源在压缩数据中,需要完整恢复
|
|
70
|
+
const hasHeadContent=headContent.length>0;const decompressCodeReadable=`
|
|
68
71
|
(function() {
|
|
69
72
|
var compressedData = '${compressedBase64}';
|
|
73
|
+
var __hasHeadContent = ${hasHeadContent};
|
|
70
74
|
|
|
71
75
|
try {
|
|
72
76
|
// Base64 解码为 Uint8Array
|
|
@@ -85,20 +89,52 @@ const decompressCodeReadable=`
|
|
|
85
89
|
// 替换 body
|
|
86
90
|
document.body.replaceWith(doc.body);
|
|
87
91
|
|
|
88
|
-
// 恢复 head
|
|
89
|
-
//
|
|
92
|
+
// 恢复 head 中的资源引用和内联脚本
|
|
93
|
+
// 如果原始 HTML 有 <head>(__hasHeadContent=true),外部资源已由 extractHeadContent
|
|
94
|
+
// 提取并放在模板 head 中,这里只需追加未被模板覆盖的内联脚本和样式
|
|
95
|
+
// 如果原始 HTML 是片段(__hasHeadContent=false),如 Adikteev 格式,
|
|
96
|
+
// 所有资源引用都在压缩数据中,需要完整恢复——包括外部脚本和样式表
|
|
97
|
+
//
|
|
98
|
+
// 重要:动态注入的外部脚本在页面解析完成后才异步加载执行,
|
|
99
|
+
// 此时 DOMContentLoaded 已触发,脚本内的事件监听器将永远无法执行。
|
|
100
|
+
// 因此在外部脚本加载完成后手动 dispatch DOMContentLoaded,确保游戏等代码正常初始化。
|
|
101
|
+
var __extScriptCount = 0;
|
|
102
|
+
var __extScriptLoaded = 0;
|
|
90
103
|
Array.from(doc.head.children).forEach(function(child) {
|
|
91
|
-
if (child.tagName === 'SCRIPT'
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
script.type = child.type;
|
|
104
|
+
if (child.tagName === 'SCRIPT') {
|
|
105
|
+
if (!child.src && child.textContent) {
|
|
106
|
+
// 内联 script(有内容但无 src)——始终需要恢复
|
|
107
|
+
var script = document.createElement('script');
|
|
108
|
+
script.textContent = child.textContent;
|
|
109
|
+
if (child.type) script.type = child.type;
|
|
110
|
+
document.head.appendChild(script);
|
|
111
|
+
} else if (child.src && !__hasHeadContent) {
|
|
112
|
+
// 外部 script(有 src 属性,如 <script src="creative.js">)
|
|
113
|
+
// 仅在原始 HTML 无 <head> 时恢复,避免与模板中已提取的重复
|
|
114
|
+
__extScriptCount++;
|
|
115
|
+
var extScript = document.createElement('script');
|
|
116
|
+
extScript.src = child.src;
|
|
117
|
+
if (child.type) extScript.type = child.type;
|
|
118
|
+
if (child.async) extScript.async = true;
|
|
119
|
+
if (child.defer) extScript.defer = true;
|
|
120
|
+
extScript.onload = function() {
|
|
121
|
+
__extScriptLoaded++;
|
|
122
|
+
if (__extScriptLoaded >= __extScriptCount) {
|
|
123
|
+
document.dispatchEvent(new Event('DOMContentLoaded'));
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
document.head.appendChild(extScript);
|
|
97
127
|
}
|
|
98
|
-
document.head.appendChild(script);
|
|
99
128
|
} else if (child.tagName === 'STYLE') {
|
|
100
|
-
//
|
|
129
|
+
// 内联 style——始终需要恢复
|
|
101
130
|
document.head.appendChild(child.cloneNode(true));
|
|
131
|
+
} else if (child.tagName === 'LINK' && /stylesheet/i.test(child.rel) && !__hasHeadContent) {
|
|
132
|
+
// 外部样式表(如 <link rel="stylesheet" href="style.css">)
|
|
133
|
+
// 仅在原始 HTML 无 <head> 时恢复,避免与模板中已提取的重复
|
|
134
|
+
var link = document.createElement('link');
|
|
135
|
+
link.rel = 'stylesheet';
|
|
136
|
+
link.href = child.href;
|
|
137
|
+
document.head.appendChild(link);
|
|
102
138
|
}
|
|
103
139
|
});
|
|
104
140
|
} catch (error) {
|