吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1013|回复: 24
收起左侧

[其他原创] 【JavaScript】csdn文章优化保存

  [复制链接]
hualy 发表于 2024-5-12 21:48
本帖最后由 hualy 于 2024-5-13 11:55 编辑

关联拓展教程:基于csdn文章优化保存的拓展教程【https://www.52pojie.cn/thread-1923542-1-1.html】

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

1

1
  优化:删除不必要元素、代码展开  保存:打印当前页面  优化保存:优化页面后保存原页面

2

2

3

3

4

4

优化页面

5

5

6

6

原代码
[JavaScript] 纯文本查看 复制代码
// ==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);
})();

ai优化后的主代码(减少了几行):
[JavaScript] 纯文本查看 复制代码
(function() {
    'use strict';
    
    // 移除 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();
    }

    // 模拟点击
    function simulateClick() {
        var selectors = ['.sidecolumn-hide', '.hide-preCode-bt'];
        selectors.forEach(function(selector) {
            var elements = document.querySelectorAll(selector);
            elements.forEach(function(element) {
                if (element) {
                    element.click();
                }
            });
        });
    }

    // 删除指定ID和类名的元素
    function removeElements() {
        simulateClick();
        removeElementsById(['csdn-toolbar', 'toolBarBox', 'recommendNps', 'toolbarBox','treeSkill']);
        removeElementsByClass(['blog_container_aside', 'recommend-box', 'comment-box', 'blog-footer-bottom', 'csdn-side-toolbar','passport-login-container']);
    }

    // 删除指定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) {
            buttons.forEach(function(button) {
                button.style.display = mql.matches ? 'none' : '';
            });
        });
    }

    // 创建按钮并添加点击事件
    var buttonActions = [
        { name: '优化', action: removeElements },
        { name: '保存', action: window.print },
        { name: '优化保存', action: function() { removeElements(); window.print(); } }
    ];

    var buttonsContainer = document.createElement('div');
    buttonsContainer.style.cssText = 'display: flex; flex-direction: column; align-items: flex-end; position: fixed; top: 100px; right: 10px; z-index: 1000;';

    var buttons = buttonActions.map(function(action) {
        var button = document.createElement('button');
        button.textContent = action.name;
        button.style.cssText = 'margin: 5px 0; padding: 10px 20px;';

        button.onclick = action.action;

        buttonsContainer.appendChild(button);
        return button;
    });

    document.body.appendChild(buttonsContainer);

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

遇到的小问题:1、打印不全

7

7

8

8

2、目录显示问题【没有登录账号不显示目录,有的文章不设置目录】

6

6

10

10



求好评呐

免费评分

参与人数 8威望 +1 吾爱币 +27 热心值 +7 收起 理由
overflow + 1 谢谢@Thanks!
苏紫方璇 + 1 + 20 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
notproblem + 1 + 1 谢谢@Thanks!
vethenc + 1 + 1 谢谢@Thanks!细节满分!
foreveriuu + 1 + 1 谢谢@Thanks!
renpeng009 + 1 + 1 我很赞同!
q1009 + 1 + 1 谢谢@Thanks!
FitContent + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

renpeng009 发表于 2024-5-12 22:39
直接复制粘贴会出错,改成以下格式

// ==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==

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
hualy + 1 + 1 我很赞同!

查看全部评分

Scan 发表于 2024-5-12 21:53
不错,楼主开发的确实很实用,还考虑到了打印
资料终结者 发表于 2024-5-12 22:01
archon1 发表于 2024-5-12 22:47
资料终结者 发表于 2024-5-12 22:01
有没有能给他导出成markdown的

建议使用SingleFile扩展,能完整的保留CSS,所以保存后的页面内容完全是原始样式。
Wisdom_xiaogui 发表于 2024-5-12 22:55
强啊,这样可以节省很多没必要的操作,直接看完csdn文章!
vethenc 发表于 2024-5-12 22:56
感谢分享!非常实用
 楼主| hualy 发表于 2024-5-12 23:02
renpeng009 发表于 2024-5-12 22:39
直接复制粘贴会出错,改成以下格式

// ==UserScript==

感谢提醒,已经改回来啦
meder 发表于 2024-5-12 23:06
大佬,用的是哪个AI
 楼主| hualy 发表于 2024-5-12 23:11
meder 发表于 2024-5-12 23:06
大佬,用的是哪个AI

chatgpt、Kimi
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-24 02:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表