// ==UserScript== // @name 搜索引擎净化 // @namespace https://bbs.tampermonkey.net.cn/ // @version 2.1.3 // @description 整合多个搜索引擎的美化与净化功能,包含广告过滤、链接优化、布局调整等 // @author Combined Scripts // @match *://www.baidu.com/* // @match *://www.google.com/* // @match *://www.google.com.*/* // @match *://*.bing.com/* // @match *://*.bing.net/* // @match *://www.sogou.com/* // @match *://www.so.com/* // @grant GM_setValue // @grant GM_getValue // @grant GM_addStyle // @grant GM_xmlhttpRequest // @grant unsafeWindow // @connect api.staticj.top // @require https://code.jquery.com/jquery-3.6.0.min.js // ==/UserScript== (function() { 'use strict'; // 工具函数:移除元素 function removeElements(selector) { try { var elements = document.querySelectorAll(selector); elements.forEach(function(el) { el.remove(); }); } catch (e) { console.error('移除元素失败:', selector, e); } } // URL参数清理功能 function initUrlCleaner() { // 监听URL变化事件 var originalPushState = history.pushState; var originalReplaceState = history.replaceState; history.pushState = function () { originalPushState.apply(this, arguments); window.dispatchEvent(new Event('locationchange')); }; history.replaceState = function () { originalReplaceState.apply(this, arguments); window.dispatchEvent(new Event('locationchange')); }; window.addEventListener('popstate', function() { window.dispatchEvent(new Event('locationchange')); }); // 清理URL参数实现 function cleanUrlParams() { try { var url = new URL(window.location.href); var changed = false; // 需要移除的查询参数集合 var paramsToRemove = [ // 百度参数 'rsp', 'prefixsug', 'fr', 'bsst', 'f', 'inputT', 'usm', 'rsv_page', 'rqlang', 'rsv_t', 'oq', 'rsv_pq', 'rsv_spt', 'ie', 'rsv_enter', 'rsv_sug', 'rsv_sug1', 'rsv_sug7', 'rsv_sug2', 'rsv_sug3', 'rsv_iqid', 'rsv_bp', 'rsv_btype', 'rsv_idx', 'rsv_dl', 'issp', 'cshid', 'tn', 'rsv_sug4', 'rtt', 'bsst', 'rsv_sid', 'rsv_tn', '_ss', 'rsv_jmp', 'rsv_bl', 'rsv_sug5', 'rsv_sug6', 'rsv_sug8', 'rsv_sug9', 'bar', 'rsv_n', 'bs', 'cl', // 谷歌参数 'tbas', 'ved', 'uact', 'ei', 'ie', 'oq', 'sclient', 'cshid', 'dpr', 'iflsig', 'aqs', 'gs_lcp', 'source', 'sourceid', 'sxsrf', 'pccc', 'sa', 'biw', 'bih', 'hl', 'newwindow', 'stick', 'gws_rd', 'client', 'gs_rn', 'tbo', 'dcr', 'safe', 'ssui', 'psi', // 必应参数 'tsc', 'sp', 'FORM', 'form', 'pq', 'sc', 'qs', 'sk', 'cvid', 'lq', 'ghsh', 'ghacc', 'ghpl', 'ghc', 'ubireng', 'FPIG', 'PC', 'setmkt', 'setlang', 'mkt', 'qpvt', 'ensearch', 'first', 'adppc', // 搜狗参数 '_asf', '_ast', 'w', 'p', 'from', 's_from', 'sessiontime', 'ie', 'sut', 'sst0', 'lkt', 'sugsuv', 'sugtime', 'suguuid', 'sourceid', 'sug', 'sugs', 'ri', 'oq', 'dp', 't', 's_t', 'cid', 'sessionid', // 360 搜索 'src', 'ssid', 'cp', 'nlpv', 'ie', 'fr', 'pn', 'psid', 'ls', 'hs', 'ahs', 'a', 'shb' ]; // 移除参数 paramsToRemove.forEach(function(param) { if (url.searchParams.has(param)) { url.searchParams.delete(param); changed = true; } }); // 特殊参数处理 if (url.searchParams.get('start') === '0') { url.searchParams.delete('start'); changed = true; } // 更新URL if (changed) { window.history.replaceState(null, null, url.toString()); } } catch (e) { console.error('URL参数清理失败:', e); } } // 初始化并监听 cleanUrlParams(); window.addEventListener('locationchange', cleanUrlParams); } // 搜索引擎检测 function detectSearchEngine() { var host = window.location.hostname; if (host.includes('baidu.com')) return 'baidu'; if (host === 'www.google.com') return 'google'; if (host === 'www.google.com.') return 'google'; if (host.includes('bing.com') || host.includes('bing.net')) return 'bing'; if (host.includes('sogou.com')) return 'sogou'; if (host === 'www.so.com') return '360'; return null; // 未匹配到任何搜索引擎 } // 百度净化 function optimizeBaidu() { GM_addStyle(` /* 移除首页元素 */ html body a.operate-wrapper, html body .operate-image, html body div#s_wrap.s-isindex-wrap, html body div.aigc-skin-bg, html body .side-entry.aging-entry, html body #s_qrcode_feed, html body #s_upfunc_menus, html body .s-upfunc-menus, html body #bottom_space, html body #s_new_search_guide, #bottom_layer.s-bottom-layer.s-isindex-wrap { display: none !important; } /* 布局优化 */ #head_wrapper { top: 15% !important; } div#head { max-height: 100vh !important; min-height: unset !important; } #content_left { width: 1000px !important; max-width: 1000px !important; } #content_right { display: none !important; } /* 广告隐藏 */ [data-adid], .c-container[data-click*="ad"], span.ad-label { display: none !important; } `); // 左侧加宽和右侧移除 function adjustContentLayout() { var leftContent = document.getElementById('content_left'); if (leftContent) { leftContent.style.width = '1000px'; leftContent.style.maxWidth = '1000px'; } var rightContent = document.getElementById('content_right'); if (rightContent) { rightContent.style.display = 'none'; } } // 广告过滤增强 function filterAds() { removeElements('.ec_wise_ad, #content_right, #content_right > br, #content_right > div:not([id])'); removeElements('#content_left > div[class*="recommend"]'); removeElements('.res_top_banner, #content_left div[class*="_rs"]'); // 优化后的判断逻辑 document.querySelectorAll('#content_left > div.result-op').forEach(function(container) { var mu = container.getAttribute('mu') || ''; var srcid = container.getAttribute('srcid') || ''; // 白名单:保留百度百科、百度翻译、百度地图、Steam官网等官方卡片 var isWhitelisted = mu.includes('baike.baidu.com') || // 百度百科 mu.includes('fanyi.baidu.com') || // 百度翻译 mu.includes('map.baidu.com') || // 百度地图 mu.includes('image.baidu.com') || // 百度图片 mu.includes('nourl.ubs.baidu.com') || // 百度汉语/百度翻译 mu.includes('wenku.baidu.com') || // 百度文库 mu.includes('store.steampowered.com') || // Steam官网 srcid.includes('baike') || srcid.includes('official') || // 官网标识 mu.includes('official-site'); // 官网标识 // 只有不在白名单的才移除 if (!isWhitelisted) { container.remove(); } }); // 搜索结果广告识别与移除 document.querySelectorAll('#content_left > div').forEach(function(container) { var span = container.querySelector('.f13 > span'); var tuiguangSpan = container.querySelector('span.tuiguang'); var brandSpan = container.querySelector('span.brand'); var dataTuiguang = container.querySelector('span[data-tuiguang]'); if ((span && span.textContent && span.textContent.includes('广告')) || container.querySelector('a[data-landurl]') || (tuiguangSpan && tuiguangSpan.textContent && tuiguangSpan.textContent.includes('广告')) || (brandSpan && brandSpan.textContent && brandSpan.textContent.includes('广告')) || dataTuiguang) { container.remove(); } }); } // 链接重定向处理 function handleRedirects() { document.querySelectorAll('a[href*="baidu.com/link"]:not([ac-redirect-processed])').forEach(function(link) { try { link.setAttribute('ac-redirect-processed', '1'); var originalHref = link.href; link.removeAttribute('onclick'); link.removeAttribute('onmousedown'); link.addEventListener('click', function (e) { e.preventDefault(); e.stopPropagation(); GM_xmlhttpRequest({ method: "GET", url: originalHref, headers: { "Accept": "*/*", "Referer": originalHref }, timeout: 5000, onload: function (response) { var directUrl = response.finalUrl; if (!directUrl) { var matches = /URL='([^']+)'/.exec(response.responseText); directUrl = matches ? matches[1] : null; } if (directUrl && !directUrl.includes('baidu.com/link')) { window.open(directUrl, '_blank'); } else { window.open(originalHref, '_blank'); } }, onerror: function () { window.open(originalHref, '_blank'); } }); }, { once: true }); } catch (e) { console.error('百度重定向处理失败:', e); } }); } // 动态内容监听 var observer = new MutationObserver(function() { adjustContentLayout(); filterAds(); handleRedirects(); }); // 开始监听整个文档的变化 observer.observe(document, { childList: true, subtree: true }); // 定时检查 setInterval(function() { adjustContentLayout(); filterAds(); handleRedirects(); }, 1000); // 初始执行 setTimeout(function() { adjustContentLayout(); }, 0); filterAds(); handleRedirects(); } // 谷歌净化 function optimizeGoogle() { // 样式优化 GM_addStyle(` /* 基础样式优化 */ .g { margin-bottom: 16px !important; } .yuRUbf { padding: 8px 0 !important; } `); // 搜索结果在新标签页打开 function processSearchResults() { document.querySelectorAll("a:not([data-processed])").forEach(function(link) { if (link.href && !link.href.includes("google.com/")) { link.dataset.processed = "true"; link.target = "_blank"; link.rel = "noopener noreferrer"; } }); } // 移除Google Lens拖放区 function removeLensDropZone() { var divElement = document.querySelector('div > c-wiz > div.focusSentinel'); if (divElement) { var parent = divElement.parentElement; if (parent) parent = parent.parentElement; if (parent && parent.parentNode) { parent.parentNode.removeChild(parent); } } } // 广告过滤 function filterAds() { removeElements('.commercial-unit, #tads, #bottomads, div[aria-label="广告"], div[aria-label="Ads"]'); } // 链接重定向优化 function handleRedirects() { document.querySelectorAll('a[onmousedown]:not([ac-redirect-processed]), ' + 'a[data-jsarwt]:not([ac-redirect-processed])').forEach(function(link) { try { link.setAttribute('ac-redirect-processed', '1'); link.removeAttribute('onmousedown'); link.removeAttribute('data-jsarwt'); link.removeAttribute('ping'); link.setAttribute('target', '_blank'); } catch (e) { console.error('谷歌重定向处理失败:', e); } }); } // 动态内容监听 var observer = new MutationObserver(function() { processSearchResults(); removeLensDropZone(); filterAds(); handleRedirects(); }); observer.observe(document.body, { childList: true, subtree: true }); // 定时检查 setInterval(function() { processSearchResults(); removeLensDropZone(); filterAds(); handleRedirects(); }, 1000); // 初始执行 processSearchResults(); removeLensDropZone(); filterAds(); handleRedirects(); } // Bing净化 function optimizeBing() { GM_addStyle(` /* 基础冗余元素隐藏 */ #footer, #est_switch, #headline, .hp_trivia_inner_new, .trivia, #id_rh, #id_qrcode, #sa_zis_PN, #sa_pn_block, .sb_form_placeholder, .below_sbox, .vs, #sb_feedback, #id_rfob, #id_rfoc, #id_mobile, #id_qrcode_popup_positioner, #gs_main > div.gs_section.gs_header > div.gs_temp > div.gs_pre_cont, #HBContent > div > div.hb_sect_container:has(div.hb_section_nohover), #vs_cont > div.mc_caro > div.hp_trivia_outer, #vs_cont > div.mc_caro_newmuse.five_col, #vert_iotd, #vert_images, #vert_otd, #vsrewds, #vs_default, #vs_cont > div.mc_caro.five_col_new, #b_context > li.b_ad, #b_results > li.b_ad, #adstop_gradiant_separator, div.b_hPanel:has(#bingApp_area), #id_mobpoppos, #b_footer, #main > ul:has(#data-from), #main > footer, #b_content > div.aca_contact, div#bnp_container { display: none !important; } /* 布局优化 */ .vs_cont .moduleCont .module { padding: 0 !important; } #b_results > li.b_algo { margin-top: 0 !important; } /* 搜索框样式 */ .sbox_cn { top: 40% !important; } #sb_form_q::placeholder { opacity: 0; } `); // 处理搜索框交互样式 function setupSearchBox() { var searchbox = document.querySelector('#sb_form_q'); var searchLabel = document.querySelector('#sb_form'); if (!searchbox || !searchLabel) return; // 初始背景透明 searchLabel.style.backgroundColor = '#FFFFFF00'; // 背景色设置函数 var setBg = function(color) { return function() { if (document.activeElement && document.activeElement.id === 'sb_form_q') return; searchLabel.style.backgroundColor = color; }; }; // 绑定事件 searchbox.onblur = setBg('#FFFFFF00'); searchbox.onfocus = function() { searchLabel.style.backgroundColor = '#FFFFFF'; }; searchbox.onclick = searchbox.onfocus; searchLabel.onclick = searchbox.onfocus; searchLabel.onmouseover = setBg('#FFFFFF00'); searchLabel.onmouseleave = setBg('#FFFFFF00'); } // 处理今日热点列表 function handleHotList() { var saUl = document.getElementById('sa_ul'); if (saUl) { var children = saUl.children; for (var i = 0; i < children.length; i++) { if (children[i].className === 'sa_hd') { for (var j = i; j < children.length; j++) { children[j].style.display = 'none'; } break; } } } } // 广告过滤增强 function filterAds() { removeElements('li.b_ad, .pa_sb, .adsMvC, a[h$=",Ads"], a[href*="/aclick?ld="], .ad_sc'); // 图片广告识别 document.querySelectorAll('.b_algo').forEach(function(algo) { var img = algo.querySelector('.rms_img'); if (img && (img.src.includes('/th?id=OADD2.') || img.src.includes('=AdsPlus'))) { algo.remove(); } }); } // 链接重定向优化 function handleRedirects() { document.querySelectorAll('a[href*="/click?"]:not([ac-redirect-processed]), ' + 'a[href*="go.microsoft.com"]:not([ac-redirect-processed])').forEach(function(link) { try { link.setAttribute('ac-redirect-processed', '1'); var url = new URL(link.href); var targetUrl = url.searchParams.get('u') || url.searchParams.get('r'); if (targetUrl) { link.href = decodeURIComponent(targetUrl); link.setAttribute('target', '_blank'); link.removeAttribute('onclick'); link.removeAttribute('onmousedown'); } } catch (e) { console.error('必应重定向处理失败:', e); } }); } // 隐藏APP弹窗 function handleAppPopup() { var observer = new MutationObserver(function() { var closeBtn = document.querySelector('div#sacs_close'); if (closeBtn) closeBtn.click(); }); observer.observe(document.body, { childList: true, subtree: true }); } // 动态内容监听 var observer = new MutationObserver(function() { filterAds(); handleRedirects(); }); observer.observe(document, { childList: true, subtree: true }); // 定时检查 setInterval(function() { filterAds(); handleRedirects(); }, 1000); // 初始执行 document.addEventListener('DOMContentLoaded', function() { setupSearchBox(); handleHotList(); handleAppPopup(); filterAds(); handleRedirects(); }); } // 搜狗净化 function optimizeSogou() { GM_addStyle(` div#right.right { display: none; } div.hintBox { display: none; } div#s_footer.cr { display: none; } a#cniil_wza { display: none; } [id^="sogou_vr_"][id*="yuanbao_banner"] > div, [id*="yuanbao_banner"] > div, [id*="yuanbao-banner"] > div, [id^="sogou_vr_"][id*="ima_banner"] > div, [id*="ima_banner"] > div, [id*="ima-banner"] > div { display: none !important; } vrwrap.middle-better-hintBox { display: none; } [id^="sogou_vr_"].vrwrap.middle-better-hintBox { display: none !important; } /* 移除内部含有 video-recommend 链接的 reactResult 卡片 */ .reactResult:has(a[href*="/video-recommend?ids="]) { display: none !important; } #main, #main.main { width: 1000px; margin: 0 auto; } #main > div.search-info, #main > #stable_uphint, #main > div.js-page-results { width: 1000px; margin-top: 0; margin-bottom: 0; } div.header { display: flex !important; justify-content: center !important; } ul.searchnav { padding-left: 0px !important; width: 1000px; } div.header-box { width: 1000px; } form#searchForm { padding: 0px !important; } #main > div.js-page-results > div.results, [id^="sogou_vr_"].reactResult, [id^="sogou_vr_"].vrwrap{ width: 1000px; } .vrwrap, div.rb { margin-bottom: 10px; border-radius: 10px !important; box-shadow: 1px 1px 10px #ccc; padding: 20px; min-width: 1000px; } #main > div.autopage-bar.widthAuto.autopage-over, #main > div.autopage-bar.widthAuto.autopage-over > div { width: 1000px; } `); // 广告过滤 function filterAds() { removeElements('#so_kw-ad, #m-spread-left, #m-spread-bottom'); // 广告标记内容移除 document.querySelectorAll('#righttop_box li').forEach(function(li) { var span = li.querySelector('span'); if (span && span.textContent && span.textContent.includes('广告')) { li.remove(); } }); } // 动态内容监听 var observer = new MutationObserver(filterAds); observer.observe(document, { childList: true, subtree: true }); // 定时检查 setInterval(filterAds, 1000); // 初始执行 filterAds(); } // 360净化 function optimize360() { var style = ` html body #ai_tools, html body .aitools, html body #card_container, html body #side, html body #side_wrap, html body #j-chat-so, html body #so_ob, html body #so_kw-ad.e-buss, html body #lm-rightbottom, #ai-input-wrapper > div.ai-input-panel { display: none !important; } `; // 仅在首页添加搜索框位置样式 if (window.location.href === 'https://www.so.com/') { style += ` .page-wrap.skin-page-wrap { position: relative; width: 100%; } #main { position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%); } `; } GM_addStyle(style); // 广告过滤 function filterAds() { removeElements('.res-mediav, .e_result, .c-title-tag, DIV.res-mediav-right, ' + 'DIV.inner_left, #so-activity-entry, DIV.tg-wrap'); } // 动态内容监听 var observer = new MutationObserver(filterAds); observer.observe(document, { childList: true, subtree: true }); // 定时检查 setInterval(filterAds, 1000); // 初始执行 filterAds(); } // 主执行函数 function main() { // 初始化通用URL清理功能 initUrlCleaner(); // 根据搜索引擎执行对应优化 var engine = detectSearchEngine(); switch (engine) { case 'baidu': optimizeBaidu(); console.log('应用百度净化'); break; case 'google': optimizeGoogle(); console.log('应用谷歌净化'); break; case 'bing': optimizeBing(); console.log('应用Bing净化'); break; case 'sogou': optimizeSogou(); console.log('应用搜狗净化'); break; case '360': optimize360(); console.log('应用360净化'); break; default: console.log('当前页面不支持优化'); } } // 执行主函数 main(); })();