网页转pdf

这段脚本展示了如何通过JavaScript删除网页中的广告和非必要元素,以优化保存技术文章为PDF的过程。针对不同平台(如优快云、掘金、简书和思否)的脚本分别移除了相应网站的特定组件,然后调用`window.print()`来生成干净的PDF。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

保存技术文章到pdf,方便自己查看。下面的脚本去除了页面广告、和其他不必要的页面上的东西。 使用体验还有优化空间,不过先就这样了,后续有时间再优化一下。

保存的时候需要把chrome用调试台打开。然后刷新一下页面,手动展开文章,然后对应平台上的代码copy到console中去,然后enter就能够保存pdf


//csdn脚本
(function () {
  $("#side").remove();
  $("#comment_title, #comment_list, #comment_bar, #comment_form, .announce, #ad_cen, #ad_bot").remove();
  $(".nav_top_2011, #header, #navigator").remove();
  $(".p4course_target, .comment-box, .recommend-box, #csdn-toolbar, #tool-box").remove();
  $("aside").remove();
  $(".m_toolbar_left").remove();
  $(".tool-box").remove();
  $(".csdn-shop-window").remove();
  $(".spec_space").remove();
  $("#first_recommend_list").remove();
  $("#comment").remove();
  $("#operate").remove();
  $("#recommend").remove();
  $(".recommend-tit-mod").remove();
  $("main").css('display', 'content');
  $("main").css('float', 'left');
  window.print();
  $("tool-box").remove();
})();


//掘金脚本

(function () {
  function removeClass(str) {
    var paras = document.getElementsByClassName(str);
    for (i = 0; i < paras.length; i++) {
      if (paras[i] != null) {
        paras[i].parentNode.removeChild(paras[i]);
      }
    }
  }
  function removeId(id) {
    var paras = document.getElementById(id);
    paras.parentNode.removeChild(paras);
  }
  removeId('comment-box');
  removeClass('main-area recommended-area shadow');
  removeClass('article-banner');
   removeClass('main-header-box');
   removeClass('suspension-panel suspension-panel');

  window.print();

})();


//简书脚本

(function () {

  function removeClass(str) {
    var paras = document.getElementsByClassName(str);
    for (i = 0; i < paras.length; i++) {
      if (paras[i] != null) {
        paras[i].parentNode.removeChild(paras[i]);
      }
    }
  }
  function removeId(id) {
    var paras = document.getElementById(id);
    paras.parentNode.removeChild(paras);
  }

  removeId('comment-main')
  removeId('recommended-notes')
  removeId('footer')
  removeClass('note-graceful-button');
  removeClass('call-app-btn');
  removeClass('transleft-NxFM3_0');
  window.print();

})();


//思否脚本

(function () {

  function removeClass(str) {
    var paras = document.getElementsByClassName(str);
    for (i = 0; i < paras.length; i++) {
      if (paras[i] != null) {
        paras[i].parentNode.removeChild(paras[i]);
      }
    }
  }
  function removeId(id) {
    var paras = document.getElementById(id);
    paras.parentNode.removeChild(paras);
  }

  removeId('comment-area')
  removeId('footer')
  removeClass('bg-white text-center border border-dark border-width-2 p-5 my-4 rounded');
  removeClass('mt-4 card');

  window.print();

})();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值