-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
executable file
·146 lines (133 loc) · 4.08 KB
/
background.js
File metadata and controls
executable file
·146 lines (133 loc) · 4.08 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const FORMAT_MAX_COUNT = 9;
const DEFAULT_OPTIONS = {
"defaultFormat": 1,
"title1": 'HTML',
"format1": "<a href=\"{{url.s(\"\\\"\",\""\")}}\">{{text.s(\"<\",\"<\")}}</a>",
"html1": 1,
"title2": "Markdown",
"format2": "[{{text.s(\"\\\\[\",\"\\\\[\").s(\"\\\\]\",\"\\\\]\")}}]({{url.s(\"\\\\(\",\"%28\").s(\"\\\\)\",\"%29\")}})",
"html2": 0,
"title3": "Text",
"format3": "{{text}}\\n{{url}}",
"html3": 0,
"title4": "reST",
"format4": "`{{text}} <{{url}}>`_",
"html4": 0,
"title5": "LaTeX",
"format5": "\\\\href\\{{{url}}\\}\\{{{text}}\\}",
"html5": 0,
"title6": "",
"format6": "",
"html6": 0,
"title7": "",
"format7": "",
"html7": 0,
"title8": "",
"format8": "",
"html8": 0,
"title9": "",
"format9": "",
"html9": 0,
"createSubmenus": true
};
const getFormatCount = options => {
let i;
for (i = 1; i <= 9; ++i) {
const optTitle = options['title' + i];
const optFormat = options['format' + i];
if (optTitle === '' || optFormat === '') {
break;
}
}
return i - 1;
};
const getDefaultOptions = () => {
const options = structuredClone(DEFAULT_OPTIONS);
const count = getFormatCount(options);
return { ...options, count, maxCount: FORMAT_MAX_COUNT };
};
const getOptions = async () => {
const options = await chrome.storage.sync.get(DEFAULT_OPTIONS);
const count = getFormatCount(options);
return { ...options, count, maxCount: FORMAT_MAX_COUNT };
};
const createContextMenus = async options => {
await chrome.contextMenus.removeAll();
if (options.createSubmenus) {
let promises = [];
for (let i = 0; i < options.count; i++) {
const format = options['title' + (i + 1)];
promises[i] = chrome.contextMenus.create({
id: "format-link-format" + (i + 1),
title: "as " + format,
contexts: ["all"]
});
}
await Promise.all(promises);
} else {
const defaultFormat = options['title' + options['defaultFormat']];
await chrome.contextMenus.create({
id: "format-link-format-default",
title: "Format Link as " + defaultFormat,
contexts: ["all"]
});
}
};
// NOTE: We use callback here since the return value of sendMessage called in
// popup.js becomes undefined if we use async/await.
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'getOptions') {
getOptions().then(options => {
sendResponse({ options });
});
} else if (request.message === 'getDefaultOptions') {
const options = getDefaultOptions();
sendResponse({ options });
} else if (request.message === 'updateDefaultFormat') {
chrome.storage.sync.set({ defaultFormat: request.formatID }).then(() => {
getOptions().then(options => {
createContextMenus(options).then(() => {
sendResponse({});
})
})
})
} else if (request.message === 'createContextMenus') {
createContextMenus(request.options).then(() => {
sendResponse({});
});
}
return true;
});
chrome.runtime.onInstalled.addListener(async () => {
const options = await getOptions();
await createContextMenus(options);
});
const menuItemIdPrefix = 'format-link-format';
const menuItemIdDefault = 'format-link-format-default';
const copyLink = async (menuItemId, linkUrl) => {
const options = await getOptions();
const formatID = menuItemId === menuItemIdDefault ?
options.defaultFormat : menuItemId.substr(menuItemIdPrefix.length);
const format = options['format' + formatID];
const asHTML = options['html' + formatID];
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const response = await chrome.tabs.sendMessage(tabs[0].id, {
message: "copyLink",
format,
asHTML,
platformOs: chrome.runtime.PlatformOs,
linkUrl,
});
};
chrome.contextMenus.onClicked.addListener((item, tab) => {
const menuItemId = item.menuItemId;
if (menuItemId.startsWith(menuItemIdPrefix)) {
copyLink(menuItemId, item.linkUrl);
}
});
chrome.commands.onCommand.addListener(command => {
if (command.startsWith(menuItemIdPrefix)) {
copyLink(command);
}
});