/
copy.js
55 lines (47 loc) · 1.76 KB
/
copy.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
(function () {
let md = `[${(document.title || document.location.hostname)}](${document.location.href})`
let org = `[[${document.location.href}][${(document.title || document.location.hostname)}]]`
// -----------------------------------------------------------
// 🛠️ 链接类型设置
// org - org mode link, eg. [[https://ovirgo.com/ship/][Ship]]
// md - markdown link, eg. [Ship](https://ovirgo.com/ship/)
let type = md
// -----------------------------------------------------------
let btn = document.createElement("div")
btn.id = 'btn'
btn.innerText = "🥳 copy"
btn.style = "background: #e6a23c; box-shadow: 1px 1px 3px #333; width: 120px; height: 40px; text-align: center; line-height: 40px; border-radius: 4px; color: #333; cursor: pointer; font-weight: 700; font-family: segoe script, courier new;font-size: 16px;"
btn.addEventListener("click", copy)
let ipt = document.createElement("input")
ipt.value = type
ipt.id = "ipt"
ipt.style = "opacity: 0;"
ipt.select()
let cont = document.createElement("div")
cont.id = 'cont'
cont.style = "position: fixed; right: 32px; top: 16px; width: 100px;z-index: 10000;"
cont.appendChild(btn)
cont.appendChild(ipt)
document.body.appendChild(cont)
})();
function copy() {
const btn = document.querySelector('#btn')
const ipt = document.querySelector('#ipt')
const cont = document.querySelector('#cont')
ipt.select();
if (!!document.execCommand('copy')) {
document.execCommand('copy')
console.log('copy success')
btn.innerText = '🎉 success'
btn.style.background = '#67c23a'
btn.style.color = '#fff'
} else {
console.log('copy failed')
btn.innerText = '❌ faild'
btn.style.background = '#f56c6c'
btn.style.color = '#fff'
}
setTimeout(() => {
document.body.removeChild(cont)
}, 1500)
}