谷歌插件-tampermonkey油猴脚本笔记

📅 发布时间:2026/7/5 13:02:11 👁️ 浏览次数:
谷歌插件-tampermonkey油猴脚本笔记
文章目录脚本极简测试脚本为什么脚本未生效?主要是为了屏蔽百度热搜右侧的搜索栏。脚本// UserScript// name 百度搜索结果页 - 彻底屏蔽右侧栏 (含新版AI/搜索标签)// namespace http://tampermonkey.net/// version 2026.03.Fix// description 基于最新页面结构分析强制隐藏 #searchTag, .guide-info-new, .select-search-ai-guide 及传统右侧栏。修复动态加载和样式覆盖问题。// author YourAssistant// match https://www.baidu.com/s?*// match https://www.baidu.com/?*// grant GM_addStyle// run-at document-start// /UserScript(function(){use strict;console.log( [TEST] 脚本已成功加载如果你看到这条日志说明油猴工作正常。 );// 1. 强力 CSS 注入使用 !important 覆盖所有内联样式和动态样式// 重点添加了从 htmlhea.txt 中解析出的新选择器constcssRules/* --- [新增] 针对 htmlhea.txt 中识别出的新元素 --- */ #searchTag.tag-fixed, /* 固定定位的搜索标签/侧边栏 */ #searchTag, /* 兼容不带 tag-fixed 的情况 */ .guide-info-new, /* 新版引导信息框 */ .select-search-ai-guide, /* AI 搜索引导框 */ .s-tipbox, /* 提示框 */ /* --- [保留] 传统右侧栏及广告 --- */ #content_right, /* 经典右侧栏 ID */ #rs_new_container, /* 新版相关搜索容器 */ .c-container[data-tooltopsearch], .c-container[data-toolrelated_search], .ec_ad_results, .rs-container, .result-op, .hint-unsafe, /* --- [布局修复] 确保左侧内容占满全屏 --- */ #content_left { width: 100% !important; float: none !important; margin-right: 0 !important; } /* 防止页面出现空白占位 */ body { overflow-x: hidden !important; };// 注入 CSSif(typeofGM_addStyle!undefined){GM_addStyle(cssRules);}else{conststyledocument.createElement(style);style.textContentcssRules;(document.head||document.documentElement).appendChild(style);}// 2. JS 清理机制作为双重保险处理 CSS 无法覆盖的动态节点或 Shadow DOM 情况constcleanUpRightBar(){// 合并新旧选择器constselectors[#searchTag,.guide-info-new,.select-search-ai-guide,.s-tipbox,#content_right,#rs_new_container,.c-container[data-tooltopsearch],.c-container[data-toolrelated_search],.ec_ad_results,.rs-container];letremovedCount0;selectors.forEach(sel{try{constelementsdocument.querySelectorAll(sel);elements.forEach(el{// 检查元素是否可见且未被处理if(el.offsetParent!nullel.style.display!none){el.style.setProperty(display,none,important);// 也可以直接移除但 display:none 更稳妥防止页面重排抖动// el.remove();removedCount;}});}catch(e){// 忽略无效选择器错误}});// 强制调整左侧宽度防止右侧留白constcontentLeftdocument.getElementById(content_left);if(contentLeft){contentLeft.style.setProperty(width,100%,important);contentLeft.style.setProperty(float,none,important);contentLeft.style.setProperty(margin-right,0,important);}// 尝试调整主容器 wrapperconstwrapperdocument.querySelector(#wrapper_wrapper, #wrapper);if(wrapper){// 某些新版布局可能需要重置 wrapper 的 flex 属性wrapper.style.justifyContentflex-start;}};// 立即执行cleanUpRightBar();// 定时轮询 (每 800ms)应对慢速加载的广告或推荐consttimersetInterval(cleanUpRightBar,800);// 3. MutationObserver: 监听 DOM 变化一旦有新节点插入立即清理constobservernewMutationObserver((mutations){lethasAddedNodesfalse;for(constmutationofmutations){if(mutation.addedNodes.length0){hasAddedNodestrue;break;}}if(hasAddedNodes){cleanUpRightBar();}});// 启动观察器conststartObserving(){if(document.body){observer.observe(document.body,{childList:true,subtree:true});}else{// 如果 body 尚未加载等待 body 出现constbodyObservernewMutationObserver((){if(document.body){observer.observe(document.body,{childList:true,subtree:true});bodyObserver.disconnect();}});bodyObserver.observe(document.documentElement,{childList:true});}};if(document.readyStateloading){document.addEventListener(DOMContentLoaded,startObserving);}else{startObserving();}console.log(✅ [优化版] 百度右侧栏屏蔽已激活包含 #searchTag, .guide-info-new 等新特征库);})();极简测试脚本刷新界面还是有热搜可以用极简脚本测试下一步一步来。// UserScript// name TEST-百度脚本是否存活// namespace http://tampermonkey.net/// version 0.1// description 仅用于测试脚本是否能运行。如果能运行会弹窗并打印日志。// author Test// match *://*.baidu.com/*// grant GM_info// run-at document-start// /UserScript(function(){use strict;console.log( [TEST] 脚本已成功加载如果你看到这条日志说明油猴工作正常。 );// 使用 setTimeout 确保页面加载一点后再弹窗避免阻塞setTimeout((){alert(✅ 成功\n\n油猴脚本正在运行。\n\n如果之前的大脚本没反应可能是代码中有语法错误或选择器导致浏览器卡死。\n\n现在的任务是修复大脚本的语法。);},1000);})();为什么脚本未生效?1、先用极简脚本试下是否运行成功。正常应该alert出内容如果alert不出来很可能是允许用户运行脚本 开关没开启打开后再试下。2、如果极简脚本可以打印出来但是全量脚本无效。(1)看下控制台是否有报错。(2)也可能是class没找准f12拿到整个html给ai重新生成下脚本。