-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
56 lines (47 loc) · 1.3 KB
/
Copy pathbackground.js
File metadata and controls
56 lines (47 loc) · 1.3 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
const START_MESSAGE = "download-images-in-element:start";
const MENU_ID = "download-images-in-element";
async function startPicker(tab) {
if (!tab || typeof tab.id !== "number") {
return;
}
try {
//Both scripts are idempotent, so reinjection is harmless.
await browser.tabs.executeScript(tab.id, {
file: "lib/zip-store.js",
runAt: "document_idle",
});
await browser.tabs.executeScript(tab.id, {
file: "content/picker.js",
runAt: "document_idle",
});
await browser.tabs.sendMessage(tab.id, {
type: START_MESSAGE,
});
} catch (error) {
console.error("Could not start element picker:", error);
flashBadge("!");
}
}
//Toolbar/add-ons-menu button.
browser.browserAction.onClicked.addListener(startPicker);
//Firefox desktop only: Android currently has no menus API.
if (browser.contextMenus) {
browser.runtime.onInstalled.addListener(() => {
browser.contextMenus.create({
id: MENU_ID,
title: "Download Images in Element",
contexts: ["all"],
});
});
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === MENU_ID) {
void startPicker(tab);
}
});
}
function flashBadge(text) {
browser.browserAction.setBadgeText({ text }).catch(() => {});
setTimeout(() => {
browser.browserAction.setBadgeText({ text: "" }).catch(() => {});
}, 2500);
}