forked from moritz-h/urls-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
61 lines (56 loc) · 2.18 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
52
53
54
55
56
57
58
59
60
61
browser.contextMenus.create({
id: "url-list-copy-urls",
title: "Copy URLs (all tabs)",
contexts: ["tab"],
});
browser.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === "url-list-copy-urls") {
browser.tabs.query({currentWindow: true}).then((tabs) => {
// Use active tab for copy to clipboard, so it is possible to open
// the context menu on inactive tabs. Save clicked tab as fallback.
let activeTabId = tab.id;
let urls = '';
for (let tab of tabs) {
urls += tab.url + "\r\n";
if (tab.active) {
activeTabId = tab.id;
}
}
// example source: https://github.com/mdn/webextensions-examples/tree/master/context-menu-copy-link-with-types
// The example will show how data can be copied, but since background
// pages cannot directly write to the clipboard, we will run a content
// script that copies the actual content.
// clipboard-helper.js defines function copyToClipboard.
const code = "copyToClipboard(" + JSON.stringify(urls) + ");";
browser.tabs.executeScript({
code: "typeof copyToClipboard === 'function';",
}).then(function (results) {
// The content script's last expression will be true if the function
// has been defined. If this is not the case, then we need to run
// clipboard-helper.js to define function copyToClipboard.
if (!results || results[0] !== true) {
return browser.tabs.executeScript(activeTabId, {
file: "clipboard-helper.js",
});
}
}).then(function () {
return browser.tabs.executeScript(activeTabId, {
code,
});
}).catch(function (error) {
// This could happen if the extension is not allowed to run code in
// the page, for example if the tab is a privileged page.
console.error("Failed to copy text: " + error);
notifyError();
});
});
}
});
function notifyError() {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("icons/error.svg"),
"title": "Error!",
"message": "Cannot write to clipboard on settings tab!"
});
}