Google Favicon

Display Favicons on Google Search

当前为 2022-08-16 提交的版本,查看 最新版本

// ==UserScript==
// @name         Google Favicon
// @namespace    https://greasyfork.org/en/users/943407-webchantment
// @version      1.2
// @description  Display Favicons on Google Search
// @author       Webchantment
// @include      https://www.google.tld/search?q=*
// @grant        GM_addStyle
// ==/UserScript==

(function() {

	/**SETTINGS**/
	const iconSize = 16; //use 16 or 24

	const padding = 2 + 4 + iconSize; //existing value + space + iconSize
	const organic = document.querySelectorAll("#search > * cite");
	const topAds = document.querySelectorAll("#tads > * span[role='text']");
	const bottomAds = document.querySelectorAll("#tadsb > * span[role='text']");

	organic.forEach(o => { prependFavicon(o, o.innerText, false); });
	topAds.forEach(t => { prependFavicon(t, t.getAttribute("data-dtld"), true); });
	bottomAds.forEach(b => { prependFavicon(b, b.getAttribute("data-dtld"), true); });

	GM_addStyle(`.eFM0qc { padding-left: ${padding}px !important; }`);

	function prependFavicon(element, text, isAd)
	{
		let domain = text;

		if (domain.startsWith("http") || isAd)
		{
			domain = domain.split(" ")[0];

			const favicon = document.createElement("img");
			favicon.src = `https://www.google.com/s2/favicons?domain=${domain}&sz=${iconSize}`;
			favicon.style = "vertical-align: middle;";

			element.prepend(" ");
			element.prepend(favicon);
		}
	}

})();