-
Notifications
You must be signed in to change notification settings - Fork 8
/
background.js
61 lines (57 loc) · 2.22 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
async function saveDefaultFormat(formatID) {
await browser.storage.sync.set({defaultFormat: formatID});
}
(async function() {
browser.commands.onCommand.addListener(async (command) => {
try {
const prefix = 'copy-link-in-format';
if (command.startsWith(prefix)) {
let formatID = command.substr(prefix.length);
const options = await gettingOptions();
const format = options['format' + formatID];
const asHTML = options['html' + formatID];
await copyLinkToClipboard(format, asHTML, options);
}
} catch (err) {
console.error("FormatLink extension failed to copy URL to clipboard.", err);
}
});
try {
const options = await gettingOptions();
await createContextMenus(options);
browser.contextMenus.onClicked.addListener(async (info, tab) => {
const prefix = "format-link-format";
if (info.menuItemId.startsWith(prefix)) {
try {
const options = await gettingOptions();
let formatID = info.menuItemId.substr(prefix.length);
if (formatID === "-default") {
formatID = options.defaultFormat;
}
const format = options['format' + formatID];
const asHTML = options['html' + formatID];
await copyLinkToClipboard(format, asHTML, options, info.linkUrl, info.linkText);
} catch (err) {
console.error("FormatLink extension failed to copy URL to clipboard.", err);
}
}
});
async function handleMessage(request, sender, sendResponse) {
if (request.messageID === 'update-default-format') {
const formatID = request.formatID;
await saveDefaultFormat(formatID);
const options = await gettingOptions();
const defaultFormat = options['title' + request.formatID];
await browser.contextMenus.update(
"format-link-format-default",
{ title: "Format Link as " + defaultFormat });
sendResponse({response: 'default format updated'});
} else {
sendResponse({response: 'invalid messageID'});
}
}
browser.runtime.onMessage.addListener(handleMessage);
} catch (err) {
console.error("failed to create context menu for FormatLink extension", err);
};
})();