// ==UserScript== // @name 代码块自动换行展开 (原生菜单) // @namespace http://tampermonkey.net/ // @version 0.0.1 // @description 自动将网页中的代码块换行展开。直接在油猴菜单中进行模式切换与黑白名单管理,无缝即时生效。 // @author 妮娜可 // @license MIT // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @run-at document-idle // @downloadURL https://update.greasyfork.icu/scripts/569565/%E4%BB%A3%E7%A0%81%E5%9D%97%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C%E5%B1%95%E5%BC%80%20%28%E5%8E%9F%E7%94%9F%E8%8F%9C%E5%8D%95%29.user.js // @updateURL https://update.greasyfork.icu/scripts/569565/%E4%BB%A3%E7%A0%81%E5%9D%97%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C%E5%B1%95%E5%BC%80%20%28%E5%8E%9F%E7%94%9F%E8%8F%9C%E5%8D%95%29.meta.js // ==/UserScript== (function() { 'use strict'; const DEFAULT_SETTINGS = { expandMode: 0, toggleMode: 0, siteList:[] }; let settings = GM_getValue('code_wrap_settings', DEFAULT_SETTINGS); let menuIds =[]; function applyStyles() { const currentHost = window.location.hostname; const inList = settings.siteList.includes(currentHost); const isEnabled = settings.toggleMode === 0 ? !inList : inList; let styleEl = document.getElementById('cw-dynamic-style'); if (!isEnabled) { if (styleEl) styleEl.remove(); return; } if (!styleEl) { styleEl = document.createElement('style'); styleEl.id = 'cw-dynamic-style'; document.head.appendChild(styleEl); } const selectors = 'pre, pre code, code, .highlight, .blob-code, .markdown-body pre, .syntaxhighlighter'; const baseWrapRules = ` white-space: pre-wrap !important; word-wrap: break-word !important; overflow-wrap: break-word !important; overflow-x: hidden !important; `; let customRules = ''; if (settings.expandMode === 0) { customRules = `font-size: inherit !important;`; } else if (settings.expandMode === 1) { customRules = `font-size: 0.85em !important; line-height: 1.3 !important;`; } else if (settings.expandMode === 2) { customRules = `font-size: 0.9em !important; line-height: 1.4 !important; max-width: 100% !important;`; } styleEl.textContent = `${selectors} { ${baseWrapRules} ${customRules} }`; } function saveAndApply() { GM_setValue('code_wrap_settings', settings); applyStyles(); renderMenu(); } function renderMenu() { menuIds.forEach(id => GM_unregisterMenuCommand(id)); menuIds =[]; const host = window.location.hostname; const inList = settings.siteList.includes(host); const modes = ['扩展区域', '缩小字体', '平衡模式']; modes.forEach((name, index) => { const icon = settings.expandMode === index ? '🔘' : '⚪'; const id = GM_registerMenuCommand(`${icon} 模式: ${name}`, () => { settings.expandMode = index; saveAndApply(); }); menuIds.push(id); }); const ruleName = settings.toggleMode === 0 ? '黑名单模式 (默认全部开启)' : '白名单模式 (默认全部关闭)'; const idRule = GM_registerMenuCommand(`🔄 当前规则: ${ruleName} -> 点击切换 `, () => { settings.toggleMode = settings.toggleMode === 0 ? 1 : 0; saveAndApply(); }); menuIds.push(idRule); let siteActionText = ''; if (settings.toggleMode === 0) { siteActionText = inList ? `✅ 恢复当前网站展开 (移出黑名单)` : `🚫 排除当前网站 (加入黑名单)`; } else { siteActionText = inList ? `🚫 取消当前网站展开 (移出白名单)` : `✅ 允许当前网站展开 (加入白名单)`; } const idSite = GM_registerMenuCommand(siteActionText, () => { if (inList) { settings.siteList = settings.siteList.filter(s => s !== host); } else { settings.siteList.push(host); } saveAndApply(); }); menuIds.push(idSite); } applyStyles(); renderMenu(); })();