// ==UserScript== // @name 裁判文书网打印 // @namespace 邓律师Lex-deng@foxmail.com // @version 1.0.0 // @description 去除多余空白,正文14pt/行距1.5,案由、案号、发布日期、浏览次数不动、隐藏“点击了解更多”按钮 // @match https://wenshu.court.gov.cn/website/wenshu/*/index.html* // @run-at document-idle // @grant none // @license MIT // ==/UserScript== (function () { 'use strict'; /* ---------- 1. 注入打印样式(仅做结构裁剪,绝不给容器设字号) ---------- */ function injectPrintStyle() { if (document.getElementById('ws-case-fix')) return; const s = document.createElement('style'); s.id = 'ws-case-fix'; s.textContent = ` @media print{ html,body{margin:0;padding:0;background:#fff!important;} body>*:not(.main){display:none!important;} .main{margin:0!important;padding:0!important;background:#fff!important;} .main>*{display:none!important;} .main>.detailBg{display:block!important;margin:0 auto!important;padding-top:0!important;} /* 只保留第二个 item_table(含案由区 + .del_center 正文) */ .detailBg .detailBox>.item_table{display:none!important;} .detailBg .detailBox>.item_table:nth-of-type(2){display:block!important;} /* 去掉目录/装饰 */ .detailBg .del_left,.detailBg .mulu_box,.detailBg .mulu_top,.detailBg .mulu_center{display:none!important;} /* 去黄边 */ .detailBg,.detailBg .detailBox,.detailBg .detailBox>.item_table:nth-of-type(2){ background:#fff!important;box-shadow:none!important;border:none!important; } /* 正文容器:自适应居中,190mm;【不设font-size,避免任何继承】 */ .detailBg .detailBox .del_center{ width:auto!important;max-width:190mm!important;margin:0 auto!important;overflow:visible!important; } .detailBg .detailBox .del_center .PDF_box{width:auto!important;} /* 案由区完全不动:不写任何字号规则 */ /* 【唯一新增:仅打印时藏"点击了解更多",用你提供的86px特征】 */ diy\\:lawyee div[style*="86px"]{display:none!important;} } `; document.head.appendChild(s); } /* ---------- 2. 判断是否为“正文 div”(原版逻辑,一字未改) ---------- */ function isBodyDiv(d) { if (!d || d.tagName !== 'DIV') return false; if (d.querySelector('table, img')) return false; const txt = d.textContent || ''; if (/案号|案由|发布日期|浏览次数|审理法院|当事人|裁判日期/.test(txt)) return false; const fs = (d.getAttribute('style') || '') + (d.style ? d.style.cssText : ''); if (!/font-size\s*:/i.test(fs)) return false; return true; } /* ---------- 3. 打印前:仅改正文 div 为 14pt/1.5(原版逻辑,一字未改) ---------- */ const _saved = []; function prepareBeforePrint() { const center = document.querySelector('.detailBg .detailBox .del_center'); if (!center) return; _saved.length = 0; center.querySelectorAll('div').forEach(function (d) { if (isBodyDiv(d)) { _saved.push({ el: d, fs: d.style.getPropertyValue('font-size'), lh: d.style.getPropertyValue('line-height'), ti: d.style.getPropertyValue('text-indent') }); d.style.setProperty('font-size', '14pt', 'important'); d.style.setProperty('line-height', '1.5', 'important'); d.style.setProperty('text-indent', '2em', 'important'); } }); } function restoreAfterPrint() { _saved.forEach(function (o) { if (o.fs) o.el.style.setProperty('font-size', o.fs); else o.el.style.removeProperty('font-size'); if (o.lh) o.el.style.setProperty('line-height', o.lh); else o.el.style.removeProperty('line-height'); if (o.ti) o.el.style.setProperty('text-indent', o.ti); else o.el.style.removeProperty('text-indent'); }); _saved.length = 0; } /* ---------- 4. 导出 PDF 按钮(原版,一字未改) ---------- */ function createExportButton() { if (document.getElementById('ws-export-btn')) return; const detail = document.querySelector('.detailBg'); if (!detail) return; const b = document.createElement('button'); b.id = 'ws-export-btn'; b.textContent = '导出 PDF'; b.style.cssText = 'position:fixed;right:16px;bottom:16px;z-index:99999;padding:6px 12px;border-radius:4px;border:1px solid #888;background:#fff;cursor:pointer;font-size:12px;box-shadow:0 1px 4px rgba(0,0,0,.15)'; b.onclick = function () { window.print(); }; document.body.appendChild(b); } /* ---------- 5. 初始化(原版,仅保留原有绑定) ---------- */ function init() { injectPrintStyle(); createExportButton(); window.addEventListener('beforeprint', prepareBeforePrint); window.addEventListener('afterprint', restoreAfterPrint); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();