-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyscript.js
More file actions
60 lines (44 loc) · 1.4 KB
/
myscript.js
File metadata and controls
60 lines (44 loc) · 1.4 KB
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
function oldwayCopy(textToCopy) {
console.log('use old way')
const input = document.createElement('textarea');
document.body.appendChild(input);
input.value = textToCopy;
input.focus();
input.select();
const result = document.execCommand('copy');
if (result === 'unsuccessful') {
console.error('无法复制此文本:');
}
else {
alert('复制成功!')
}
}
var copyText = function () {
var documentClone = document.cloneNode(true);
var article = new Readability(documentClone).parse();
console.log(article.textContent)
var titleText = article.title.trim()
var contentText = article.textContent.trim()
var urlText = window.location.href
var textToCopy = '\t\t\t标题: ' + titleText +
'\n转帖原地址: ' + urlText +
'\n' + contentText
//https网页使用clipboard api
if (location.protocol == 'https:') {
console.log('use clipboard api')
navigator.clipboard.writeText(textToCopy)
.then(() => {
console.log('文本已经成功复制到剪切板');
alert('复制成功!')
})
.catch(err => {
// 如果用户没有授权,使用oldway尝试
oldwayCopy(textToCopy)
});
}
//http网页使用execCommand
else {
oldwayCopy(textToCopy)
}
}
copyText();