-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.js
61 lines (56 loc) · 1.53 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
chrome.runtime.onMessage.addListener(onMsg)
var PORT = 3396
var ws = new WebSocket('ws://localhost:' + PORT)
createListener(ws)
var timeout;
function onMsg(msg) {
if (msg.event === 'copy') {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
if (~[2,3].indexOf(ws.readyState)) {
timeout = setTimeout(() => {
ws = new WebSocket('ws://localhost:' + PORT)
createListener(ws)
ws.onopen = () => {
ws.send(msg.data)
}
},300)
} else {
ws.send(msg.data)
}
}
}
function createListener(ws) {
ws.onmessage = msg => {
if (msg.data instanceof Blob)
reader = new FileReader()
reader.onload = () => copyTextToClipboard(reader.result)
reader.readAsText(event.data)
}
}
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}