去除csdn登錄才能復制代碼的限制 油猴腳本
https://greasyfork.org/zh-CN/scripts/441883
// ==UserScript==
// @name? ? ? ? ?去除CSDN代碼登錄后復制及全文關注后查看的限制
// @namespace? ? https://github.com/zhzhch335/myTampermonkey
// @version? ? ? 1.6
// @description? 解決未登錄時CSDN不能復制代碼的問題
// @author? ? ? ?zhzhch335
// @match? ? ? ? http*://blog.csdn.net/*/article/details/*
// @match? ? ? ? https://*.blog.csdn.net/article/details/*
// @icon? ? ? ? ?https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// @grant? ? ? ? none
// ==/UserScript==
(function () {
? ? 'use strict';
? ? // Your code here...
? ? // 將所有代碼區(qū)域變?yōu)榭蛇x
? ? document.querySelectorAll("code").forEach(function (item) {
? ? ? ? item.style = item.style + ";user-select: text !important;";
? ? ? ? return item;
? ? })
? ? // 將所有登錄復制按鈕變成全選
? ? document.querySelectorAll(".hljs-button").forEach(function (item) {
? ? ? ? item.dataset.title = "復制全部";
? ? ? ? return item;
? ? })
? ? try {
? ? ? ? // 重寫登錄復制方法
? ? ? ? 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);
? ? ? ? ? ? // 執(zhí)行復制命令
? ? ? ? ? ? document.execCommand('copy', false, null);
? ? ? ? ? ? e.target.dataset.title = "復制成功";
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? e.target.dataset.title = "復制全部";
? ? ? ? ? ? }, 1000);
? ? ? ? }
? ? ? ? // 重寫另一個登錄方法(需要去除行號和版權聲明)
? ? ? ? window.mdcp.signin = e => {
? ? ? ? ? ? // 避免拖動選擇代碼時直接觸發(fā)了復制全部
? ? ? ? ? ? if (!e.target.className.includes("hljs-button")) return;
? ? ? ? ? ? var preNode = e.path.filter(item => item.tagName == "CODE")[0];
? ? ? ? ? ? // 選中一段文字
? ? ? ? ? ? let selection = window.getSelection();
? ? ? ? ? ? let range = document.createRange();
? ? ? ? ? ? range.selectNode(preNode);
? ? ? ? ? ? selection.removeAllRanges();
? ? ? ? ? ? selection.addRange(range);
? ? ? ? ? ? // 執(zhí)行復制命令
? ? ? ? ? ? document.execCommand('copy', false, null);
? ? ? ? ? ? e.target.dataset.title = "復制成功";
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? e.target.dataset.title = "復制全部";
? ? ? ? ? ? }, 1000);
? ? ? ? }
? ? }
? ? catch { }
? ? // 解除關注才能查看全文的限制
? ? $('#article_content').removeAttr("style");
? ? $('.hide-article-box').remove();
})();