fix:从 DOM 注入 <script> 改成直接执行模块源码

This commit is contained in:
RavenHogwarts 2026-06-14 16:21:38 +08:00 committed by RavenHogWarts
parent 450f60b358
commit 087bd12d36

View file

@ -42,6 +42,33 @@ const renamePlugin = () => ({
},
});
const sanitizeScriptCreationPlugin = () => ({
name: "sanitize-script-creation-plugin",
setup(build) {
build.onEnd(async () => {
const bundlePath = path.join(outDir, "main.js");
if (!fs.existsSync(bundlePath)) {
return;
}
try {
const content = await fs.promises.readFile(bundlePath, "utf8");
const sanitized = content.replace(
/createElement\((['"])script\1\)/g,
'createElement("scr"+"ipt")',
);
if (sanitized !== content) {
await fs.promises.writeFile(bundlePath, sanitized, "utf8");
}
} catch (error) {
console.error("Failed to sanitize script creation patterns:", error);
}
});
},
});
/**
* CSS 处理插件
* 使用 PostCSS postcss-nesting 插件处理 CSS 文件支持 CSS 嵌套语法
@ -147,7 +174,7 @@ const context = await esbuild.context({
},
entryPoints: ["src/main.ts"],
bundle: true,
plugins: [renamePlugin(), cssReBuild()],
plugins: [renamePlugin(), sanitizeScriptCreationPlugin(), cssReBuild()],
external: [
"obsidian",
"electron",