Skip to content

Commit 3c200ae

Browse files
committed
fix: importESModule error in worker
1 parent 5acecb9 commit 3c200ae

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/basic.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,20 @@ export class BasicTool {
5858
_plugin: undefined,
5959
},
6060
};
61-
if (
62-
typeof globalThis.ChromeUtils?.importESModule !== "undefined" ||
63-
typeof globalThis.ChromeUtils?.import !== "undefined"
64-
) {
65-
const { ConsoleAPI } = _importESModule(
66-
"resource://gre/modules/Console.sys.mjs",
67-
);
68-
this._console = new ConsoleAPI({
69-
consoleID: `${this._basicOptions.api.pluginID}-${Date.now()}`,
70-
});
71-
}
61+
try {
62+
if (
63+
typeof globalThis.ChromeUtils?.importESModule !== "undefined" ||
64+
typeof globalThis.ChromeUtils?.import !== "undefined"
65+
) {
66+
const { ConsoleAPI } = _importESModule(
67+
"resource://gre/modules/Console.sys.mjs",
68+
);
69+
this._console = new ConsoleAPI({
70+
consoleID: `${this._basicOptions.api.pluginID}-${Date.now()}`,
71+
});
72+
}
73+
} catch {}
74+
7275
this.updateOptions(data);
7376
}
7477

@@ -521,10 +524,10 @@ export function makeHelperTool(cls: any, options: BasicTool | BasicOptions) {
521524

522525
// Make compatible import between fx128 (import jsm) and fx140 (importESModule mjs)
523526
export function _importESModule(path: string): any {
524-
const platformVersion = Zotero.platformMajorVersion;
525-
526-
if (platformVersion >= 140) {
527-
return ChromeUtils.importESModule(path);
527+
// Since the `Zotero` or `Services` might not be available,
528+
// we directly check `ChromeUtils` for import.
529+
if (typeof ChromeUtils.import === "undefined") {
530+
return ChromeUtils.importESModule(path, { global: "contextual" });
528531
}
529532

530533
// If matching *.sys.mjs, convert to *.jsm

0 commit comments

Comments
 (0)