菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

JavaScript实现CSDN/CNBlogs去除无用内容的方法

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:js教程 CSDN

要在JavaScript中打印CSDN或博客网站上的内容并去除无用内容,可以使用以下方法: 1. 使用`fetch`或`XMLHttpRequest`从网站获取HTML内容。 2. 使用DOM解析器(如`DOMParser`)将HTML字符串转换为DOM对象。 3. 遍历DOM对象,找到需要的内容并将其添加到结果字符串中。 4. 使用正则表达式或其他方法去除无用内容。 5. 打印结果字符串。 以下是一个简单的示例代码: ```javascript // 获取HTML内容 fetch('https://www.csdn.net/') .then(response => response.text()) .then(html => { // 创建DOM解析器 const parser = new DOMParser(); // 将HTML字符串转换为DOM对象 const doc = parser.parseFromString(html, 'text/html');

javascript打印CSDN/cnblogs去除无用内容方法,搜索参考网上代码,很多要么页面打印不全、要么文字被切。
1. 进入页面,按F12,在console输入以下代码:
cnblogs博客园:

(function(){'usestrict';$("#comment_form,#header,#leftmenu,#big_banner,#footer,#blog_post_info_block,#sideBar").remove();$("#content").css('margin','0');$("body").css('margin','0').css('padding','2px');$("#mainContent").css('width','100%');window.print();})();

CSDN:

(function(){'usestrict';$(".toolbar-inside,.blog_container_aside,.csdn-side-toolbar,.more-toolbox,.recommend-boxinsert-baidu-box,.blog-footer-bottom").remove();$("main").css('display','content');$("main").css('float','left');$("main").width(1080);$("#mainBox").width("100%");window.print();})();

2. 其他类似,如有改版,页面右键检查要去除的部分,查看源码,按选择器去除相关部分:
.xxx 类选择器,以class=xxx
#xxx ID选择器,以id=xxx
3. CSDN打印时切文字,因其width设置与打印显示不一致,按下设置:

$("main").width(1080);


相关专题

相关文章

    无相关信息
评论
建站知识
建站知识
使用技巧
调试安装
运营推广