-
Notifications
You must be signed in to change notification settings - Fork 8
/
CSDN-allowCopy.user.js
138 lines (129 loc) · 4.81 KB
/
CSDN-allowCopy.user.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
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
// ==UserScript==
// @name CSDN 允许复制、去广告
// @namespace https://github.com/the-eric-kwok/my_userscripts
// @version 0.9
// @description 将“登录以复制”按钮更改为复制功能,去除 banner 广告
// @author EricKwok
// @supportURL https://github.com/the-eric-kwok/my_userscripts/issues
// @match https://*.csdn.net/*
// @icon https://g.csdnimg.cn/static/logo/favicon32.ico
// @grant none
// @run-at document-start
// @license GPLv3
// ==/UserScript==
(function () {
'use strict';
function addListener(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
}
else if (element.attachEvent) {
element.attachEvent('on' + eventName, handler);
}
else {
element['on' + eventName] = handler;
}
}
function allowSelect(elem) {
// 删除 CSS 中禁止复制的属性
elem.style.setProperty("-webkit-touch-callout", "initial");
elem.style.setProperty("-webkit-user-select", "initial");
elem.style.setProperty("-khtml-user-select", "initial");
elem.style.setProperty("-moz-user-select", "initial");
elem.style.setProperty("-ms-user-select", "initial");
elem.style.setProperty("user-select", "initial");
}
function copyMe(elem) {
console.log(elem);
elem.path[0].setAttribute("data-title", "复制成功✅")
window.setTimeout(() => elem.path[0].setAttribute("data-title", "复制"), 1000);
let code = elem.path[1].innerText;
function _legacyCopy() {
console.log("正在使用传统方法复制");
let tmpInput = document.createElement('input');
elem.insertAdjacentHTML("afterend", tmpInput)
tmpInput.value = code;
tmpInput.focus();
tmpInput.select();
if (document.execCommand('copy')) {
document.execCommand('copy');
}
tmpInput.blur();
console.log('复制成功');
tmpInput.remove();
}
if (navigator.clipboard && window.isSecureContext) {
console.log("正在使用 navigator clipboard api 进行复制操作");
navigator.clipboard.writeText(code)
.then(() => {
console.log('复制成功');
})
.catch(err => {
console.log("navigator clipboard api 复制时出错,将使用传统方法进行复制")
_legacyCopy();
})
} else {
_legacyCopy();
}
}
function makeCopy() {
for (var item of document.querySelectorAll("pre")) {
allowSelect(item);
for (var child of item.children) {
allowSelect(child);
if (child.className.includes("signin")) {
child.setAttribute("data-title", "复制");
child.setAttribute("onclick", "");
addListener(child, 'click', function (child) {
copyMe(child);
});
}
for (var child2 of child.children) {
allowSelect(child2);
if (child2.className.includes("signin")) {
child2.setAttribute("data-title", "复制");
child.setAttribute("onclick", "");
addListener(child2, 'click', function (child) {
copyMe(child);
});
}
}
}
}
}
/**
*
* @param {string} CssSelector CSS selector to identify the advert element
*/
function removeAd(CssSelector) {
for (var elem of document.querySelectorAll(CssSelector)) {
elem.remove();
}
}
var adCssSelectors = [
".toolbar-advert",
".csdn-common-logo-advert",
".passport-login-container",
".top-bar",
".csdn-common-logo-advert",
".adsbygoogle"
];
if (window.location.href.includes("blog.csdn.net")) {
window.onload = makeCopy;
// 去除复制时末尾追加的版权信息(实际上通过阻止copy事件传播来实现)
window.addEventListener("copy", function (event) {
event.stopImmediatePropagation();
}, true);
window.setInterval(function () {
for (var sel of adCssSelectors) {
removeAd(sel);
}
}, 100);
} else if (window.location.href.includes("download.csdn.net")) {
window.setInterval(function () {
for (var sel of adCssSelectors) {
removeAd(sel);
}
}, 100);
}
})();