-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipbordCopy.js
58 lines (47 loc) · 1.36 KB
/
clipbordCopy.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
var base64 = require('./base64.js')
function clipboardCopy (text) {
// A <span> contains the text to copy
var span = document.createElement('span')
span.textContent = text
span.style.whiteSpace = 'pre' // Preserve consecutive spaces and newlines
// An <iframe> isolates the <span> from the page's styles
var iframe = document.createElement('iframe')
iframe.sandbox = 'allow-same-origin'
document.body.appendChild(iframe)
var win = iframe.contentWindow
win.document.body.appendChild(span)
var selection = win.getSelection()
// Firefox fails to get a selection from <iframe> window, so fallback
if (!selection) {
win = window
selection = win.getSelection()
document.body.appendChild(span)
}
var range = win.document.createRange()
selection.removeAllRanges()
range.selectNode(span)
selection.addRange(range)
var success = false
try {
success = win.document.execCommand('copy')
} catch (err) {}
selection.removeAllRanges()
span.remove()
iframe.remove()
return success
}
module.exports = function(e){
var cp_code=e.getAttribute("data-copy");
var decode = base64.decode(cp_code)
var isSuc = clipboardCopy(decode)
if(decode){
var p = e.firstElementChild
console.log(p.innerHTML)
p.innerHTML = "Copied!"
setTimeout(function(){
p.innerHTML = "Copy to clipbord!"
},1500)
}
else{
}
}