-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
141 lines (109 loc) · 3.25 KB
/
index.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
body {
background-color: var(--figma-color-bg);
color: var(--figma-color-text);
font-family: monospace;
}
#app {
white-space: pre;
}
</style>
<div id="app"></div>
<script>
function makeError () {
return new DOMException('The request is not allowed', 'NotAllowedError')
}
async function copyClipboardApi (text) {
// Use the Async Clipboard API when available. Requires a secure browsing
// context (i.e. HTTPS)
if (!navigator.clipboard) {
throw makeError()
}
return navigator.clipboard.writeText(text)
}
async function copyExecCommand (text) {
// Put the text to copy into a <span>
const span = document.createElement('span')
span.textContent = text
// Preserve consecutive spaces and newlines
span.style.whiteSpace = 'pre'
span.style.webkitUserSelect = 'auto'
span.style.userSelect = 'all'
// Add the <span> to the page
document.body.appendChild(span)
// Make a selection object representing the range of text selected by the user
const selection = window.getSelection()
const range = window.document.createRange()
selection.removeAllRanges()
range.selectNode(span)
selection.addRange(range)
// Copy text to the clipboard
let success = false
try {
success = window.document.execCommand('copy')
} finally {
// Cleanup
selection.removeAllRanges()
window.document.body.removeChild(span)
}
if (!success) throw makeError()
}
async function clipboardCopy (text) {
try {
await copyClipboardApi(text)
} catch (err) {
// ...Otherwise, use document.execCommand() fallback
try {
await copyExecCommand(text)
} catch (err2) {
throw (err2 || err || makeError())
}
}
}
// my code
const appElem = document.querySelector("#app")
appElem.addEventListener("click", () => {
// Select everything and try to copy again
let success = false
const selection = window.getSelection()
const range = document.createRange()
range.selectNodeContents(appElem)
selection.removeAllRanges()
selection.addRange(range)
success = document.execCommand("copy")
if (success) {
parent.postMessage({ pluginMessage: { type: 'copy-success' } }, '*')
}
})
onmessage = async (event) => {
if (event && event.data && event.data.pluginMessage) {
const json = event.data.pluginMessage
appElem.innerText = json
const suc = await copyToClipBoard(json)
if (suc) {
parent.postMessage({ pluginMessage: { type: 'copy-success' } }, '*')
}
else {
parent.postMessage({ pluginMessage: { type: 'copy-fail' } }, '*')
}
}
}
async function copyToClipBoard(text) {
let sucCopy = false
try {
await clipboardCopy(text)
sucCopy = true
}
catch(e) {}
return sucCopy
}
</script>
</body>
</html>