修改可发布的xpi的插件包

This commit is contained in:
yehongyu
2026-07-20 15:55:03 +08:00
parent b784a7e2fb
commit 3d5ffa1cb6
14 changed files with 1066 additions and 96 deletions
+40
View File
@@ -0,0 +1,40 @@
import fs from "node:fs/promises";
/**
* zotero-plugin-toolkit's `_importESModule` still prefers ChromeUtils.import
* when that property exists. On Zotero 9 / Firefox 128+, `import` remains as a
* stub that throws "ChromeUtils.import() has been removed".
*/
export function fixToolkitImportESModule() {
return {
name: "chatpapers-fix-toolkit-importesmodule",
setup(build) {
build.onLoad(
{ filter: /zotero-plugin-toolkit[\\/]dist[\\/]index\.js$/ },
async (args) => {
const text = await fs.readFile(args.path, "utf8");
const next = text.replace(
/function _importESModule\(path\) \{[\s\S]*?\n\}/,
`function _importESModule(path) {
\tif (typeof ChromeUtils.importESModule === "function") {
\t\ttry {
\t\t\treturn ChromeUtils.importESModule(path, { global: "contextual" });
\t\t} catch (e) {
\t\t\treturn ChromeUtils.importESModule(path);
\t\t}
\t}
\tif (path.endsWith(".sys.mjs")) path = path.replace(/\\.sys\\.mjs$/, ".jsm");
\treturn ChromeUtils.import(path);
}`,
);
if (next === text) {
console.warn(
"[chatpapers] toolkit _importESModule patch did not match; check toolkit version",
);
}
return { contents: next, loader: "js" };
},
);
},
};
}