// ==UserScript== // @name 简洁版阮一峰周报 // @namespace https://github.com/AliubYiero/TamperMonkeyScripts // @version 1.1.0 // @description 显示简洁版阮一峰周报, 点击标题可以展开内容 // @author Yiero // @license GPL-3 // @match https://www.ruanyifeng.com/blog/* // @require https://scriptcat.org/lib/513/2.0.0/ElementGetter.js // @grant GM_addStyle // @grant GM_getValue // ==/UserScript== /* ==UserConfig== 配置项: imgHeight: title: 限制帖子内部图片高度 (单位px) (百分比缩放, 默认0表示auto) description: 限制图片大小, 单位像素px (默认0表示auto) type: number default: 0 defaultDisplayMode: title: 帖子默认显示状态 description: 帖子默认显示状态 ( 'hide' 默认隐藏, 'show' 默认显示 ) type: select default: 'hide' values: [ 'hide', 'show' ] ==/UserConfig== */ (async () => { const imgHeight = GM_getValue('配置项.imgHeight', 0); const defaultDisplayMode = GM_getValue('配置项.defaultDisplayMode', 'hide'); GM_addStyle(` p.content:not(:has(a)).hide {display: none;} h2.title.hide {color: #a3a3a3;} h2.title.hide::after { content: 'hide'; font-size: 50%; border-radius: 10px; margin: 0 0 10px 10px; background-color: #a3a3a36b; padding: 5px; color: #fff; } p.content > img { height: ${imgHeight === 0 ? 'auto' : `${imgHeight}px`} } `) await elmGetter.get('#main-content > h2'); let index = 0; document.querySelectorAll('#main-content > h2, #main-content > p').forEach(element => { defaultDisplayMode === 'hide' && element.classList.add('hide') if (element.tagName === 'H2') { index++; element.classList.add('title') } else { element.classList.add('content') } element.dataset.index = index }) document.querySelector('#main-content').addEventListener('click', (e) => { e.preventDefault() if (!e.target.classList.contains('title')) { return; } document.querySelectorAll(`[data-index="${e.target.dataset.index}"]`).forEach(ele => { ele.classList.toggle('hide') }) }) })()