Google Favicons

Adds favicons to Google search results.

目前为 2018-08-10 提交的版本。查看 最新版本

// ==UserScript==
// @name	Google Favicons
// @description	Adds favicons to Google search results.
// @include	https://www.google.*/search*
// @version	1.05
// @grant       GM_addStyle
// @icon        http://i.imgur.com/ezjWmEO.png
// @namespace   https://gf.qytechs.cn/users/14186
// ==/UserScript==

/**
 * CSS INFO
 * #res img.favicon = Normal Results Favicon Position
 * #res div.gG0TJc img.favicon, #res div.card-section a.RTNUJf img.favicon = News Tab Favicon Position
 * QUERY INFO
 * #res .g div.rc h3 a = Normal Results
 * #res div#search g-section-with-header g-inner-card g-card-section a = Normal Results 'Top stories' Without Carousel/Pictures Block
 * #res div.gG0TJc a = News Tab First Results
 * #res div.card-section a.RTNUJf = News Tab Sub Results
 */

(function(){

	(typeof GM_addStyle != 'undefined' ? GM_addStyle : function addStyle(css) {
		var head = document.getElementsByTagName('head')[0];
		var style = document.createElement("style");
		style.type = "text/css";
		style.appendChild(document.createTextNode(css));
		head.appendChild(style);
	})("#res img.favicon {\
        margin-right: 13px;\
        vertical-align: middle;\
        border: none;\
        left: 1px;\
        position: relative;\
        top: -1px;\
        z-index: 9;\
		}\
		#res div.gG0TJc img.favicon, #res div.card-section a.RTNUJf img.favicon {\
        position: static;\
        left: 0px;\
        margin-right: 6px;\
        padding-bottom: 2.3px;\
		}\
		");

	var FAVICON_GRABBER = 'https://www.google.com/s2/favicons?domain='; // 'http://favicon.yandex.net/favicon/'
var QUERY = '#res .g div.rc h3 a, #res div#search g-section-with-header g-inner-card g-card-section a, #res div.gG0TJc a, #res div.card-section a.RTNUJf';

/**
 * @param {NodeList} links
 */
function add_favicons_to(links) {
	for (var i=0; i<links.length; i++) {
		if (links[i].firstChild.className != 'favicon') {
			var host = links[i].href.replace(/.*https?:\/\//, '').replace(/\/.*$/,'');
			var img = document.createElement('IMG');
			img.src = FAVICON_GRABBER + host;
			img.width = '16';
			img.height = '16';
			img.className = 'favicon';
			links[i].insertBefore(img, links[i].firstChild);
		}
	}
}

add_favicons_to(document.querySelectorAll(QUERY));

/**
 * Debounce function from http://code.google.com/p/jquery-debounce/
 */
function debounce(fn, timeout, invokeAsap, context) {
	if (arguments.length == 3 && typeof invokeAsap != 'boolean') {
		context = invokeAsap;
		invokeAsap = false;
	}
	var timer;
	return function() {
		var args = arguments;
		if(invokeAsap && !timer) {
			fn.apply(context, args);
		}
		clearTimeout(timer);
		timer = setTimeout(function() {
			if(!invokeAsap) {
				fn.apply(context, args);
			}
			timer = null;
		}, timeout);
	};
}

document.addEventListener('DOMNodeInserted', debounce(function handleNewFavicons(event){
		if (event.target.className != 'favicon') {
			add_favicons_to(document.querySelectorAll(QUERY));
		}
	}, 500)
, false);

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址