// ==UserScript==
// @name Bing 搜索建议点击修复
// @namespace https://tampermonkey.net/
// @version 1.0.0
// @description 修复 Bing 搜索框下方关联搜索建议(自动建议)无法点击的问题
// @author you
// @match *://www.bing.com/*
// @match *://cn.bing.com/*
// @match *://global.bing.com/*
// @match *://bing.com/*
// @run-at document-start
// @grant none
// ==/UserScript==
(function () {
'use strict';
/* ---------- 选择器 ---------- */
const SEL_LIST = '#sa_ul'; // 建议面板外层
const SEL_ITEM = SEL_LIST + ' li.sa_sg'; // 单条建议
const SEL_DEL = '.sa_rm'; // 历史记录右侧的“删除”按钮
/* ---------- 1. 注入样式:保证建议项可被鼠标命中 ---------- */
const css = `
#sa_ul, #sa_hs_block, #sa_sug_block { pointer-events: auto !important; }
#sa_ul li.sa_sg,
#sa_ul li.sa_sg * { pointer-events: auto !important; }
#sa_ul li.sa_sg { cursor: pointer !important; }
`;
const styleEl = document.createElement('style');
styleEl.textContent = css;
(document.head || document.documentElement).appendChild(styleEl);
/* ---------- 2. 工具函数 ---------- */
const getItem = (el) =>
el instanceof Element ? el.closest(SEL_ITEM) : null;
// 从
的 url / query 属性解析出目标地址
function resolveHref(li) {
const raw = li.getAttribute('url');
if (raw) {
try { return new URL(raw, location.href).href; } catch (_) {}
}
const q = li.getAttribute('query');
if (q) {
return location.origin + '/search?q=' + encodeURIComponent(q);
}
return null;
}
let navigating = false;
function go(href, e) {
if (!href || navigating) return;
navigating = true;
if (e && (e.ctrlKey || e.metaKey || e.button === 1)) {
// Ctrl / Cmd / 中键 → 新标签页
window.open(href, '_blank', 'noopener');
navigating = false;
} else {
location.assign(href);
}
}
/* ---------- 3. mousedown:记录按下的建议项 ---------- */
let pending = null;
window.addEventListener('mousedown', (e) => {
const li = getItem(e.target);
if (!li) { pending = null; return; }
if (e.target.closest(SEL_DEL)) { pending = null; return; } // “删除”按钮不处理
if (e.button !== 0 && e.button !== 1) { pending = null; return; }
pending = { li, href: resolveHref(li), t: Date.now() };
}, true);
/* ---------- 4. mouseup:真正执行跳转 ---------- */
// 直接在 mouseup 里跳转,而不是等 click。
// 因为 Bing 常在 mousedown 时把建议列表隐藏/重建,
// 后续的 click 事件根本落不到 上,这也是“点了没反应”的常见原因。
window.addEventListener('mouseup', (e) => {
const p = pending;
pending = null;
if (!p || !p.href) return;
if (e.button !== 0 && e.button !== 1) return;
if (Date.now() - p.t > 2000) return; // 按太久,视为误触
e.preventDefault();
e.stopPropagation();
go(p.href, e);
}, true);
/* ---------- 5. click:兜底(触屏 / 程序化点击) ---------- */
window.addEventListener('click', (e) => {
const li = getItem(e.target);
if (!li) return;
if (e.target.closest(SEL_DEL)) return;
const href = resolveHref(li);
if (!href) return;
e.preventDefault();
e.stopPropagation();
go(href, e);
}, true);
})();