-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-polyfill.js
More file actions
45 lines (45 loc) · 1.34 KB
/
browser-polyfill.js
File metadata and controls
45 lines (45 loc) · 1.34 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
// Minimal polyfill: wraps chrome.* callback APIs as promise-based browser.* APIs
// If browser.* already exists (Firefox), this is a no-op.
if (typeof globalThis.browser === 'undefined') {
globalThis.browser = {
storage: {
local: {
get(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(keys, (result) => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve(result);
});
});
},
set(items) {
return new Promise((resolve, reject) => {
chrome.storage.local.set(items, () => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve();
});
});
}
}
},
browserAction: {
onClicked: chrome.browserAction
? chrome.browserAction.onClicked
: { addListener() {} }
},
sidebarAction: null,
runtime: chrome.runtime,
windows: chrome.windows
? {
create(opts) {
return new Promise((resolve, reject) => {
chrome.windows.create(opts, (win) => {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError);
else resolve(win);
});
});
}
}
: null
};
}