如何用Tampermonkey将content-inner类的max-height固定为4000px
用Tampermonkey固定content-inner元素的max-height为4000px
基础脚本(适用于页面加载时已存在的元素)
直接遍历所有目标元素,强制替换max-height值:
// ==UserScript== // @name 固定content-inner的max-height // @namespace http://tampermonkey.net/ // @version 0.1 // @description 将class为content-inner的div元素max-height固定为4000px // @author You // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // 选中所有class为content-inner的元素 const contentInners = document.querySelectorAll('.content-inner'); contentInners.forEach(el => { // 强制设置max-height为固定值 el.style.maxHeight = '4000px'; }); })();
进阶脚本(支持动态加载的元素)
如果页面存在滚动加载、AJAX渲染等动态添加元素的场景,用DOM监听自动处理新增元素:
// ==UserScript== // @name 固定content-inner的max-height(支持动态加载) // @namespace http://tampermonkey.net/ // @version 0.2 // @description 将class为content-inner的div元素max-height固定为4000px,适配动态新增元素 // @author You // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // 单个元素的处理逻辑 function setFixedHeight(el) { if (el.classList.contains('content-inner')) { el.style.maxHeight = '4000px'; } } // 初始化处理页面已有的元素 document.querySelectorAll('.content-inner').forEach(setFixedHeight); // 监听DOM变化,处理新增元素 const observer = new MutationObserver(mutations => { mutations.forEach(mutation => { mutation.addedNodes.forEach(node => { if (node.nodeType === 1) { setFixedHeight(node); // 处理新增容器内部的目标元素 node.querySelectorAll('.content-inner').forEach(setFixedHeight); } }); }); }); // 启动监听 observer.observe(document.body, { childList: true, subtree: true }); })();
注意事项
- 修改脚本中的
@match规则,替换为你需要生效的网站域名(比如https://example.com/*),避免脚本在所有网站运行 - 安装脚本后刷新目标页面即可生效
内容的提问来源于stack exchange,提问作者Scripter
相关产品推荐
相关产品推荐

