-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
46 lines (41 loc) · 1.87 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
var getting_description = browser.storage.sync.get('description');
getting_description.then((res) => {
browser.contextMenus.create({
id: "copy-selection-link-clipboard",
title: res.description,
contexts: ["selection"],
});
});
browser.contextMenus.onClicked.addListener((info, tab) => {
var getting_template = browser.storage.sync.get('template');
getting_template.then((res) => {
buildLink( res.template )
});
function buildLink( template ) {
browser.tabs.query({currentWindow: true, active: true}).then((tabs) => {
var hyperlink = template.replace("$LINKTEXT", info.selectionText ).replace("$URL", tabs[0].url ).replace("$TITLE", tabs[0].title );
// clipboard-helper.js defines function copyToClipboard.
const code = "copyToClipboard(" + JSON.stringify( hyperlink ) + ");";
browser.tabs.executeScript({
code: "typeof copyToClipboard === 'function';",
}).then((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(tab.id, {
file: "clipboard-helper.js",
});
}
}).then(() => {
return browser.tabs.executeScript(tab.id, {
code,
});
}).catch((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);
});
});
}
});