去除CSDN代码登录后复制及全文关注后查看的限制

解决未登录时CSDN不能复制代码的问题

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

ผู้เขียน
zhzhch335
จำนวนติดตั้งประจำวัน
0
จำนวนติดตั้งทั้งหมด
1,880
คะแนน
2 1 0
เวอร์ชัน
1.7
สร้างเมื่อ
22-03-2022
อัปเดตเมื่อ
23-06-2022
Size
2.57 กิโลไบต์
สัญญาอนุญาต
ไม่มี
ปรับใช้กับ

效果:


之前


之后










作为程序猿,应该会经常去csdn参(chao)考(xi)代码,今天在复制一篇文章的代码的时候,突然发现需要登录才能复制,但是我用github授权登录的时候居然失败了!

突发奇想,写个脚本解除这个限制吧!

稍微看一下文档结构,不难发现禁止选中复制就是css的user-select属性设置成了none而已,只要改回text即可:






[JavaScript]
1
2
3
4
5
// 将所有代码区域变为可选
document.querySelectorAll("code").forEach(function(item) {
    item.style = item.style + ";user-select: text !important;";
    return item;
})



顺便也把右侧的登录复制按钮变成复制全部了:

[JavaScript]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
// 将所有登录复制按钮变成全选
document.querySelectorAll(".hljs-button").forEach(function(item) {
    item.dataset.title = "复制全部";
    return item;
})
// 重写登录复制方法
window.hljs.signin = e => {
    var preNode = e.path.filter(item => item.tagName == "PRE")[0];
    // 选中一段文字
    let selection = window.getSelection();
    let range = document.createRange();
    range.selectNode(preNode);
    selection.removeAllRanges();
    selection.addRange(range);
    // 执行复制命令
    document.execCommand('copy', false, null);
    e.target.dataset.title = "复制成功";
    setTimeout(() => {
        e.target.dataset.title = "复制全部";
    },1000);
}