41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
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" };
|
|
},
|
|
);
|
|
},
|
|
};
|
|
}
|