/
background.js
83 lines (75 loc) · 2.58 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
function copyToClipboard(text) {
console.log("called copyToClipboard", text);
const input = document.createElement('textarea');
input.style.position = 'fixed';
input.value = text;
document.body.appendChild(input);
input.select();
document.execCommand('Copy');
document.body.removeChild(input);
};
function getSelection() {
return window.getSelection().toString();
}
function copyTextWithURL(tab, copy_mode) {
// console.log("called copyTextWithURL");
let url = tab.url;
let tabId = tab.id;
chrome.scripting.executeScript(
{
target: { tabId: tabId },
func: getSelection
},
(res) => {
let selection = res[0].result;
if (selection.length > 0) {
console.log("selected_text", selection);
let text = "---"
if (copy_mode === "plain") {
text = selection + "\n" + url;
} else if (copy_mode === "markdown") {
text = "[" + selection + "](" + url + ")";
}
chrome.scripting.executeScript(
{
target: { tabId: tabId },
func: copyToClipboard,
args: [text],
},
(res) => {
let options = {
type: "basic",
iconUrl: "./images/icon_128.png",
title: "Copied as " + copy_mode + " text.",
message: "\"" + selection + "\"",
}
chrome.notifications.create(`copy_with_text_notification-${Date.now()}`, options);
});
} else {
alert("TODO no selected text");
}
});
}
chrome.commands.onCommand.addListener(function (command) {
// console.log("onCommand:", command);
let copy_mode = null;
if (command === "copy-text-with-url-as-plain") {
copy_mode = "plain"
} else if (command === "copy-text-with-url-as-markdown") {
copy_mode = "markdown"
} else {
alert("Unknown command: " + command);
}
// console.log("copy mode", copy_mode);
if (copy_mode !== undefined) {
// console.log("call tabs.query to activeTab");
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function (tabs) {
console.log("call copyTextWithURL");
copyTextWithURL(tabs[0], copy_mode);
});
}
});