CSDN文章去除广告保存文章油猴脚本

油猴脚本:CSDN 文章保存 进入csdn文章页面,会显示三个按钮,通过浏览器自带的打印功能实现另存为PDF,展开文章、代码 使得能够完整保存,不用关注博主即可阅读全文、界面优化、保存文章(没有登录就无目录,登录了就有目录

CSDN文章去除广告保存文章油猴脚本

将下面代码添加到油猴启用即可。

 

// ==UserScript==
// @name         CSDN 文章优化保存
// @version     2024-05-12
// @description csdn文章优化保存
// @author      hualy13
// @match       *://blog.csdn.net/*
// @icon        https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// ==/UserScript==
(function() {
    'use strict';
    //不用关注博主即可阅读全文
    window.addEventListener('load', function() {
        // 移除 article_content 元素的内联样式
        var articleContent = document.getElementById("article_content");
        if (articleContent) {
            articleContent.removeAttribute("style");
        }

        // 移除特定的元素
        var followTextParent = document.querySelector('.follow-text')?.closest('[data-flag="follow"]');
        if (followTextParent) {
            followTextParent.remove();
        }

        var hideArticleBox = document.querySelector('.hide-article-box');
        if (hideArticleBox) {
            hideArticleBox.remove();
        }
    });
    //模拟点击
    window.addEventListener('load', function() {
    var selectors = ['.sidecolumn-hide', '.hide-preCode-bt','sidecolumn-show'];
    selectors.forEach(function(selector) {
        var element = document.querySelector(selector);
        if (element) {
            element.click();
        }
    });
});
    // 定义要删除的ID数组和类名数组
    var idsToRemove = ['csdn-toolbar', 'toolBarBox', 'recommendNps', 'toolbarBox','treeSkill'];
    var classesToRemove = ['blog_container_aside', 'recommend-box', 'comment-box', 'blog-footer-bottom', 'csdn-side-toolbar','passport-login-container'];

    // 删除指定ID和类名的元素
    function removeElements() {
        // var hideElement = document.querySelector('.sidecolumn-hide');
        // if (hideElement) {
        //     hideElement.click();
        // }
        removeElementsById(idsToRemove);
        removeElementsByClass(classesToRemove);
    }

    // 删除指定ID的元素
    function removeElementsById(ids) {
        ids.forEach(function(id) {
            var element = document.getElementById(id);
            if (element) element.parentNode.removeChild(element);
        });
    }

    // 删除指定类名的元素
    function removeElementsByClass(classes) {
        classes.forEach(function(className) {
            var elements = document.getElementsByClassName(className);
            while(elements.length > 0) {
                elements[0].parentNode.removeChild(elements[0]);
            }
        });
    }

    // 监听打印事件并在打印时隐藏按钮
    function setupPrintListener(buttons) {
        window.matchMedia('print').addListener(function(mql) {
            if (mql.matches) {
                // 打印开始,隐藏按钮
                buttons.forEach(function(button) {
                    button.style.display = 'none';
                });
            } else {
                // 打印结束,显示按钮
                buttons.forEach(function(button) {
                    button.style.display = '';
                });
            }
        });
    }

    // 创建按钮并添加点击事件
    var buttons = [];
    var buttonNames = ['优化', '保存', '优化保存'];

    var buttonsContainer = document.createElement('div');
    buttonsContainer.style.display = 'flex';
    buttonsContainer.style.flexDirection = 'column';
    buttonsContainer.style.alignItems = 'flex-end';
    buttonsContainer.style.position = 'fixed';
    buttonsContainer.style.top = '100px';
    buttonsContainer.style.right = '10px';
    buttonsContainer.style.zIndex = '1000';

    buttonNames.forEach(function(name) {
        var button = document.createElement('button');
        button.textContent = name;
        button.style.margin = '5px 0';
        button.style.padding = '10px 20px';

        // 分配点击事件
        button.onclick = function() {
            if (name === '优化保存') {
                removeElements(); // 删除元素
                window.print(); // 触发打印
            }
            else if (name === '保存') {
                window.print(); // 触发打印
            }
            else if (name === '优化') {
                removeElements(); // 删除元素
            }
        };

        buttons.push(button); // 添加按钮到数组
        buttonsContainer.appendChild(button); // 添加按钮到容器
    });

    document.body.appendChild(buttonsContainer); // 将按钮容器添加到页面

    // 设置打印事件监听器
    setupPrintListener(buttons);
})();

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
技术教程

浏览器同时让四个搜索引擎一起帮你搜索

2024-4-11 13:34:51

技术教程

Python刷微信步数

2024-5-17 15:20:02

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索