// ==UserScript== // @name 类型明确的打印隐藏脚本 // @namespace http://tampermonkey.net/ // @version 0.9 // @description 优化混合类型规则数组的处理逻辑 目前只用于涂色集,简化图片打印自动旋转逻辑,类名添加专属前缀,旋转图片铺满打印区域 // @author Your Name // @match *://tuseji.com/* // @match *://hongdou1fan.com/* // @grant none // ==/UserScript== (function () { 'use strict'; function injectStyle() { // 注入打印样式(移除margin和max-width,让旋转图片铺满) if (!document.getElementById('userscript-no-print-style')) { const style = document.createElement('style'); style.id = 'userscript-no-print-style'; style.textContent = ` @media print { .userscript-no-print { display: none !important; } /* 打印旋转样式:移除margin和max-width,铺满打印区域 */ .userscript-print-rotate-90 { transform: rotate(90deg) !important; transform-origin: center !important; width: 100% !important; height: auto !important; } } `; document.head.appendChild(style); } } // 规则定义:保持不变 const noPrintRules = { "https://tuseji.com/": [ '#postMain span.tags,#postMain .postMeta,.related-posts,#footer,#header,#category,.floating-pagi', () => [...document.querySelectorAll('#postMain .entry p')].filter(p => !p.querySelector("img")) ], "https://hongdou1fan.com/": [ '#postMain span.tags,#postMain .postMeta,.related-posts,#footer,#header,#category,.floating-pagi', () => [...document.querySelectorAll('#postMain .entry p')].filter(p => !p.querySelector("img")) ], }; /** * 安全地为元素添加带前缀的no-print类 */ function addNoPrintClass(elements) { if (!elements) return; const elementArray = Array.isArray(elements) ? elements : elements instanceof NodeList ? Array.from(elements) : [elements]; elementArray.forEach(el => { if (el?.nodeType === Node.ELEMENT_NODE && !el.classList.contains('userscript-no-print')) { el.classList.add('userscript-no-print'); } }); } /** * 处理选择器字符串 */ function handleSelector(selector) { if (typeof selector !== 'string' || !selector.trim()) return; addNoPrintClass(document.querySelectorAll(selector)); } /** * 处理自定义函数 */ function handleCustomHandler(handler) { if (typeof handler !== 'function') return; try { addNoPrintClass(handler()); } catch (error) { console.error('自定义处理器执行出错:', error); } } /** * 简化的图片旋转处理逻辑 */ function handleImageRotation() { const allImgs = document.querySelectorAll('img:not(.userscript-print-rotate-90)'); if (!allImgs.length) return; Array.from(allImgs).forEach(img => { const isInNoPrint = img.closest('.userscript-no-print'); if (!isInNoPrint) { img.classList.add('userscript-print-rotate-90'); } }); } function run() { const currentUrl = window.location.href; // 1. 先注入样式(全局执行) injectStyle(); // 2. 处理no-print规则 for (const [urlPattern, rule] of Object.entries(noPrintRules)) { if (currentUrl.includes(urlPattern) && Array.isArray(rule)) { const [selectors, customHandler] = rule; handleSelector(selectors); handleCustomHandler(customHandler); break; } } // 3. 把旋转逻辑移到for循环之外(全局执行) handleImageRotation(); } // 页面加载完成后执行 if (document.readyState === 'complete') { run(); } else { window.addEventListener('load', run); } })();