Google Search Highlighter

Highlights searched terms in results in yellow, and underlines exact matches

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Search Highlighter
// @namespace    gglSearchHighlight_kk
// @description  Highlights searched terms in results in yellow, and underlines exact matches
// @version      1.0
// @author       Kai Krause <[email protected]>
// @include      http*://www.google.*/*
// @include      http*://www.google.co*.*/*
// @run-at       document-start
// ==/UserScript==

let embedCSS = setInterval(() => {
	if (document.head) {
		let css = document.createElement("style");
		css.innerText = "em { background-color: #FFFF7F; color: black !important }";
		document.head.appendChild(css);
		clearInterval(embedCSS);
	}
}, 4);

setInterval(() => {
	let search = document.title;
	search = search.replace(/(imagesize|site|related|cache|inurl|filetype|-|OR).*?(?=\s|$)/g, "|split|");
	search = search.replace(/"/g, "|split|");
	search = search.split("|split|");

	search.forEach((term) => {
		term = term.trimStart().trim().toLowerCase();
		term = term.replace(/(\s\*\s|\*)/g, ".*?");
		if (!term) return;

		document.querySelectorAll("em").forEach((el) => {
			let str = el.textContent.trimStart().trim().toLowerCase();

			let isMatch = false;

			if (str === term) {
				isMatch = true;
			}
			else if (term.includes("*")) {
				let re = new RegExp(term);
				if (re.test(str)) isMatch = true;
			}

			if (isMatch) el.innerHTML = "<u>" + el.textContent + "</u>";
		});
	});
}, 50);