-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
46 lines (43 loc) · 1.05 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
(function () {
const _clipboard = {
value: "",
};
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({
id: "copy",
contexts: ["selection"],
title: "Copy",
});
chrome.contextMenus.create({
id: "paste",
contexts: ["all"],
title: "Paste and Clear",
});
chrome.contextMenus.create({
id: "clear",
contexts: ["all"],
title: "Clear",
});
});
chrome.contextMenus.onClicked.addListener(async function (data, tab) {
switch (data.menuItemId) {
case "copy":
_clipboard.value = data.selectionText || "";
break;
case "paste":
const clipboard = { value: _clipboard.value };
_clipboard.value = "";
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: (value) => {
document.execCommand("insertText", false, value);
},
args: [clipboard.value],
});
break;
case "clear":
_clipboard.value = "";
break;
}
});
})();