-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_script.js
More file actions
70 lines (61 loc) · 1.97 KB
/
content_script.js
File metadata and controls
70 lines (61 loc) · 1.97 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
function copyToClipboard(text) {
var elem = document.createElement("textarea");
elem.style.position = "fixed";
elem.style.opacity = "0";
elem.style.zIndex = "-1";
elem.style.width = "1px";
elem.style.height = "1px";
elem.style.overflow = "hidden";
elem.style.whiteSpace = "pre";
elem.textContent = text;
document.body.appendChild(elem);
elem.select();
elem.focus();
document.execCommand("copy");
document.body.removeChild(elem);
}
function readFromClipboard() {
var elem = document.createElement("textarea");
elem.style.position = "fixed";
elem.style.opacity = "0";
elem.style.zIndex = "-1";
elem.style.width = "1px";
elem.style.height = "1px";
elem.style.overflow = "hidden";
elem.style.whiteSpace = "pre";
document.body.appendChild(elem);
elem.select();
elem.focus();
if (document.execCommand("paste")) return elem.value;
return "";
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log("Received message:", request);
if (request.message === "copy-and-paste") {
try {
const text = readFromClipboard();
console.log("Clipboard text:", text);
const decodedText = decodeURIComponent(text);
console.log("Decoded text:", decodedText);
copyToClipboard(decodedText);
console.log("Copied decoded text to clipboard");
sendResponse({ status: "success", text: decodedText });
} catch (err) {
sendResponse({ status: "error", error: err });
}
}
});
const iconRedPaths = {
16: "images/icon16red.png",
24: "images/icon24red.png",
32: "images/icon32red.png",
48: "images/icon48red.png",
128: "images/icon128red.png",
};
const iconGreenPaths = {
16: "images/icon16green.png",
24: "images/icon24green.png",
32: "images/icon32green.png",
48: "images/icon48green.png",
128: "images/icon128green.png",
};