// ==UserScript== // @name Bing Latte · 搜索增强 // @namespace bing-latte-suite // @version 3.0.0 // @description 必应搜索增强三合一:Catppuccin Latte 浅色主题、结果直链还原(跳过 bing.com/ck 跳转)、URL 参数清理。无定时器、无轮询。 // @tag bing theme 必应 // @author deran // @license MIT // @match *://*.bing.com/search* // @grant GM_addStyle // @run-at document-start // ==/UserScript== (() => { 'use strict'; /* ================================================================== 0. 配置 ================================================================== */ /* Catppuccin Latte */ const latte = { base: '#eff1f5', // 画布 mantle: '#e6e9ef', // 下沉表面:卡片、搜索框 crust: '#dce0e8', // 再下一层:折叠标题、次要按钮、描边 surface0: '#ccd0da', // 悬停 surface1: '#bcc0cc', // 按下 / 选中 overlay1: '#8c8fa1', // 最弱的辅助文字 subtext0: '#6c6f85', // 摘要、脚注 subtext1: '#5c5f77', text: '#4c4f69', // 标题、正文 blue: '#1e66f5', // 链接 }; /* 搜索结果页只保留这些查询参数,其余全部丢弃 */ const KEEP_PARAMS = ['q', 'first', 'mkt', 'setlang', 'cc']; /* ================================================================== 1. URL 参数清理 在 DOM 就绪前执行,避免页面根据脏 URL 已发起一遍无谓请求。 replaceState 不触发导航,也不需要 postMessage 通知宿主。 ================================================================== */ function cleanUrl() { const url = new URL(location.href); const params = new URLSearchParams(); for (const key of KEEP_PARAMS) { for (const value of url.searchParams.getAll(key)) { params.append(key, value); } } const query = params.toString(); const cleaned = url.origin + url.pathname + (query ? '?' + query : '') + url.hash; if (cleaned !== location.href) { history.replaceState(history.state, '', cleaned); } } cleanUrl(); /* 翻页是整页导航,脚本会在新页面重新执行;但必应有些交互用 pushState 原地换参,这里接管两个 history 方法补一次。事件驱动,不轮询。 */ for (const method of ['pushState', 'replaceState']) { const original = history[method]; history[method] = function (...args) { const result = original.apply(this, args); cleanUrl(); return result; }; } window.addEventListener('popstate', cleanUrl); /* ================================================================== 2. 结果直链还原 必应把每条结果包成 /ck/a?...&u=a1。 u 参数就是 base64url(真实地址),前缀 a1 是版本标记, 纯前端可解,不需要任何网络请求。 ================================================================== */ /* a1 前缀之后是 base64url,补齐 padding 后 atob */ function decodeBingLink(u) { try { let b64 = u.startsWith('a1') ? u.slice(2) : u; b64 = b64.replace(/-/g, '+').replace(/_/g, '/'); while (b64.length % 4) b64 += '='; return atob(b64); } catch { return null; } } function unwrapLink(anchor) { let url; try { url = new URL(anchor.href, location.href); } catch { return; } /* 只碰自己域名下的 /ck/ 跳转,别的站外链接一律不动 */ if (url.hostname !== location.hostname) return; if (!url.pathname.startsWith('/ck/')) return; const u = url.searchParams.get('u'); if (!u) return; const target = decodeBingLink(u); if (!target) return; /* 两种情况都要还原: - 站外结果:绝对地址 https://... - 站内导航(图片/视频/地图/资讯):以 / 开头的相对路径 还原后 href 不再含 /ck/,天然幂等,不需要额外的已处理标记。 */ if (/^https?:\/\//i.test(target)) { anchor.href = target; } else if (target.startsWith('/')) { anchor.href = target; } } function unwrapAll(root) { /* 结果标题 + 站点图标 + 「查看详细信息」等,凡是 /ck/ 包一层都还原 */ for (const anchor of root.querySelectorAll('a[href*="/ck/"]')) { unwrapLink(anchor); } } /* ================================================================== 3. 主题 CSS ================================================================== */ /* 搜索页的颜色几乎都由 html 上的这些变量驱动,值全是必应混淆过的名字。 清单按实测被样式表引用筛选,改一处等于整类组件一起换色。 必应会在页面中部用