-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpluginBridge.ts
107 lines (102 loc) · 3.47 KB
/
pluginBridge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import type ToolkitGlobal from "../managers/toolkitGlobal.js";
import { BasicTool } from "../basic.js";
/**
* Plugin bridge. Install plugin from zotero://plugin
*
* @deprecated Since this is a temporary solution for debugging, it is not recommended to use.
* The zotero-plugin-scaffold no longer need this.
*
* @example
* Install plugin from url, with minimal Zotero version requirement.
* ```text
* zotero://plugin/?action=install&url=https%3A%2F%2Fgithub.heygears.com%2FMuiseDestiny%2Fzotero-style%2Freleases%2Fdownload%2F3.0.5%2Fzotero-style.xpi&minVersion=6.999
* ```
*/
export class PluginBridge {
public static readonly version: number = 1;
public get version(): number {
return PluginBridge.version;
}
constructor() {
this.initializePluginBridge();
}
public static setModule(instance: ToolkitGlobal) {
if (
!instance.pluginBridge?.version ||
instance.pluginBridge.version < PluginBridge.version
) {
instance.pluginBridge = new PluginBridge();
}
}
private initializePluginBridge() {
// @ts-expect-error has import
const { AddonManager } = ChromeUtils.import(
"resource://gre/modules/AddonManager.jsm",
);
const Zotero = BasicTool.getZotero();
const pluginBridgeExtension = {
noContent: true,
doAction: async (uri: { spec: string }) => {
try {
const uriString = uri.spec.split("//").pop();
if (!uriString) {
return;
}
const params: { [key: string]: any } = {};
uriString
.split("?")
.pop()
?.split("&")
.forEach((p: string) => {
params[p.split("=")[0]] = decodeURIComponent(p.split("=")[1]);
});
if (params.action === "install" && params.url) {
if (
(params.minVersion &&
Services.vc.compare(Zotero.version, params.minVersion) < 0) ||
(params.maxVersion &&
Services.vc.compare(Zotero.version, params.maxVersion) > 0)
) {
throw new Error(
`Plugin is not compatible with Zotero version ${Zotero.version}.` +
`The plugin requires Zotero version between ${params.minVersion} and ${params.maxVersion}.`,
);
}
const addon = await AddonManager.getInstallForURL(params.url);
if (addon && addon.state === AddonManager.STATE_AVAILABLE) {
addon.install();
hint("Plugin installed successfully.", true);
} else {
throw new Error(`Plugin ${params.url} is not available.`);
}
}
} catch (e: any) {
Zotero.logError(e);
hint(e.message, false);
}
},
newChannel(uri: any) {
this.doAction(uri);
},
};
// @ts-expect-error wrappedJSObject
Services.io.getProtocolHandler("zotero").wrappedJSObject._extensions[
"zotero://plugin"
] = pluginBridgeExtension;
}
}
function hint(content: string, success: boolean) {
const progressWindow = new Zotero.ProgressWindow({ closeOnClick: true });
progressWindow.changeHeadline("Plugin Toolkit");
// @ts-expect-error custom
progressWindow.progress = new progressWindow.ItemProgress(
success
? "chrome://zotero/skin/tick.png"
: "chrome://zotero/skin/cross.png",
content,
);
// @ts-expect-error custom
progressWindow.progress.setProgress(100);
progressWindow.show();
progressWindow.startCloseTimer(5000);
}