-
Notifications
You must be signed in to change notification settings - Fork 0
/
exturl_content.js
63 lines (58 loc) · 1.54 KB
/
exturl_content.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
const popupToCopy = content => {
const modal = new tingle.modal({
closeMethods: ["button"],
onOpen: () =>
// let close = modal.close / let close modal = modal.close
// ambiguity? and why Haskell has referential transparency?
document.querySelector(".exturl-copy-trigger")
.addEventListener("exturlCopyCompleted", () => modal.close())
})
modal.setContent(
`
<div><textarea id="exturl-copy-source">${content}</textarea></div>
<div>
<button class="tingle-btn tingle-btn--primary exturl-copy-trigger"
data-clipboard-target="#exturl-copy-source">
COPY!
</button>
</div>
`
)
modal.open()
}
browser.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
if (request["kind"] == "POPUP_TO_COPY") {
popupToCopy(request["content"])
}
if (request["kind"] == "COPY_TO_CLIPBOARD") {
copyToClipboard(request["content"])
}
}
)
const copyIt = () => {
const target = document.getElementById("exturl-copy-source")
target.focus()
target.select()
document.execCommand('copy')
}
document.body.addEventListener("click",
e => {
if (e.target.classList.contains("exturl-copy-trigger")) {
copyIt()
e.target.dispatchEvent(new CustomEvent("exturlCopyCompleted", { }))
}
});
const copyToClipboard = content => {
const element = document.createElement("textarea")
Object.assign(element.style, {
width: "2em",
height: "2em"
})
document.body.appendChild(element)
element.value = content
// element.focus()
element.select()
document.execCommand("copy")
document.body.removeChild(element)
}