-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbackground.js
51 lines (45 loc) · 1.34 KB
/
background.js
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
function initContextMenu() {
browser.storage.sync.get().then(settings => {
let showEntry = ('showTabContextMenuCopyUrls' in settings) ? settings.showTabContextMenuCopyUrls : true;
if (showEntry) {
browser.contextMenus.create({
id: "url-list-copy-urls",
title: "Copy URLs (all tabs)",
contexts: ["tab"],
});
browser.contextMenus.onClicked.addListener(onContextMenuClick);
}
}, error => {
console.log(`Error: ${error}`);
});
}
function clearContextMenu() {
browser.contextMenus.onClicked.removeListener(onContextMenuClick);
browser.contextMenus.remove('url-list-copy-urls');
}
function onContextMenuClick(info, tab) {
if (info.menuItemId === "url-list-copy-urls") {
browser.tabs.query({currentWindow: true}).then((tabs) => {
let urls = tabs.map(tab => tab.url).join('\r\n');
navigator.clipboard.writeText(urls).then(() => {
// success
}, () => {
notifyError();
});
});
}
}
function notifyError() {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.runtime.getURL("icons/error.svg"),
"title": "Error!",
"message": "Writing to clipboard is not possible!"
});
}
function settingsChanged(message) {
clearContextMenu();
initContextMenu();
}
browser.runtime.onMessage.addListener(settingsChanged);
initContextMenu();