-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.js
More file actions
221 lines (184 loc) · 6.24 KB
/
Copy pathplatform.js
File metadata and controls
221 lines (184 loc) · 6.24 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* TabJump browser API compatibility helpers.
*
* Firefox exposes Promise-based WebExtension APIs through `browser`.
* Chrome/Chromium may expose only callback-based APIs through `chrome`,
* so this file provides the small Promise-style surface TabJump uses.
*/
(function () {
if (globalThis.TabJumpPlatform)
return;
function makeErrorFromLastError(lastError) {
return lastError ? new Error(lastError.message || String(lastError)) : null;
}
function promisifyChromeMethod(scope, methodName) {
if (!scope || typeof scope[methodName] !== "function")
return undefined;
return (...args) => new Promise((resolve, reject) => {
try {
const result = scope[methodName](...args, value => {
const error = makeErrorFromLastError(globalThis.chrome?.runtime?.lastError);
if (error) {
reject(error);
return;
}
resolve(value);
});
if (result && typeof result.then === "function")
result.then(resolve, reject);
} catch (error) {
reject(error);
}
});
}
function wrapChromeMessageEvent(event) {
return {
addListener(listener) {
event.addListener((message, sender, sendResponse) => {
try {
const result = listener(message, sender, sendResponse);
if (result && typeof result.then === "function") {
result.then(
value => sendResponse(value),
error => {
console.error(error);
sendResponse(undefined);
}
);
return true;
}
if (result !== undefined)
sendResponse(result);
} catch (error) {
console.error(error);
sendResponse(undefined);
}
return false;
});
}
};
}
function createChromeBrowserShim(chromeApi) {
const runtime = {
getManifest: chromeApi.runtime.getManifest.bind(chromeApi.runtime),
getURL: chromeApi.runtime.getURL.bind(chromeApi.runtime),
getPlatformInfo: promisifyChromeMethod(chromeApi.runtime, "getPlatformInfo"),
sendMessage: promisifyChromeMethod(chromeApi.runtime, "sendMessage"),
openOptionsPage: promisifyChromeMethod(chromeApi.runtime, "openOptionsPage"),
onInstalled: chromeApi.runtime.onInstalled,
onStartup: chromeApi.runtime.onStartup,
onMessage: wrapChromeMessageEvent(chromeApi.runtime.onMessage),
onMessageExternal: wrapChromeMessageEvent(chromeApi.runtime.onMessageExternal)
};
const storage = {
local: {
get: promisifyChromeMethod(chromeApi.storage.local, "get"),
set: promisifyChromeMethod(chromeApi.storage.local, "set")
}
};
const tabs = {
get: promisifyChromeMethod(chromeApi.tabs, "get"),
query: promisifyChromeMethod(chromeApi.tabs, "query"),
update: promisifyChromeMethod(chromeApi.tabs, "update"),
create: promisifyChromeMethod(chromeApi.tabs, "create"),
onRemoved: chromeApi.tabs.onRemoved,
onUpdated: chromeApi.tabs.onUpdated,
onAttached: chromeApi.tabs.onAttached
};
const windows = {
update: promisifyChromeMethod(chromeApi.windows, "update"),
onRemoved: chromeApi.windows.onRemoved
};
const commands = {
getAll: promisifyChromeMethod(chromeApi.commands, "getAll"),
onCommand: chromeApi.commands.onCommand
};
const notifications = chromeApi.notifications
? {
create: promisifyChromeMethod(chromeApi.notifications, "create"),
update: promisifyChromeMethod(chromeApi.notifications, "update"),
clear: promisifyChromeMethod(chromeApi.notifications, "clear"),
getPermissionLevel: promisifyChromeMethod(chromeApi.notifications, "getPermissionLevel")
}
: null;
const actionApi = chromeApi.action || chromeApi.browserAction;
const action = actionApi
? {
setBadgeBackgroundColor: promisifyChromeMethod(actionApi, "setBadgeBackgroundColor"),
setBadgeText: promisifyChromeMethod(actionApi, "setBadgeText")
}
: null;
const contextMenuApi = chromeApi.contextMenus || chromeApi.menus;
const menus = contextMenuApi
? {
removeAll: promisifyChromeMethod(contextMenuApi, "removeAll"),
create: contextMenuApi.create.bind(contextMenuApi),
onClicked: contextMenuApi.onClicked
}
: null;
return {
runtime,
storage,
tabs,
windows,
commands,
notifications,
action,
browserAction: action,
contextMenus: menus,
menus
};
}
if (!globalThis.browser && globalThis.chrome)
globalThis.browser = createChromeBrowserShim(globalThis.chrome);
const api = globalThis.browser || globalThis.chrome;
if (api) {
if (!api.browserAction && api.action)
api.browserAction = api.action;
if (!api.action && api.browserAction)
api.action = api.browserAction;
if (!api.menus && api.contextMenus)
api.menus = api.contextMenus;
if (!api.contextMenus && api.menus)
api.contextMenus = api.menus;
}
globalThis.TabJumpPlatform = {
api,
getManifest() {
return api?.runtime?.getManifest ? api.runtime.getManifest() : {};
},
isFirefoxRuntime() {
return Boolean(api?.runtime?.getBrowserInfo);
},
isFirefoxBuild() {
return Boolean(this.getManifest().browser_specific_settings?.gecko);
},
isManifestV3() {
return this.getManifest().manifest_version === 3;
},
getBrowserLabel() {
return this.isFirefoxRuntime() ? "Firefox" : "Chrome/Chromium";
},
getOpenPopupCommandName() {
return this.isManifestV3() ? "_execute_action" : "_execute_browser_action";
},
getActionContextName() {
return this.isManifestV3() ? "action" : "browser_action";
},
getActionMenuContexts() {
return [this.getActionContextName()];
},
supportsTabContextMenus() {
return this.isFirefoxRuntime();
},
supportsTstBadges() {
return this.isFirefoxRuntime();
},
supportsDefaultSlotShortcuts() {
return this.isFirefoxRuntime();
},
supportsDesktopNotifications() {
return Boolean(api?.notifications);
}
};
}());