CSDN 允许复制、去广告

将“登录(不可用)以复制”按钮更改为复制功能,去除 banner 广告

目前為 2021-11-22 提交的版本,檢視 最新版本

// ==UserScript==
// @name         CSDN 允许复制、去广告
// @namespace    https://github.com/the-eric-kwok/my_userscripts
// @version      0.5
// @description  将“登录(不可用)以复制”按钮更改为复制功能,去除 banner 广告
// @author       EricKwok
// @match        https://*.csdn.net/*
// @icon         https://g.csdnimg.cn/static/logo/favicon32.ico
// @grant        none
// @run-at       document-end
// @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 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")) {
            for (var child of item.children) {
                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) {
                    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"
    ];

    if (window.location.href.includes("blog.csdn.net")) {
        window.onload = function () {
            makeCopy();
            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);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址