Greasy Fork

Greasy Fork is available in English.

Google Favicons

Adds favicons to Google search results.

当前为 2015-12-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name	Google Favicons
// @description	Adds favicons to Google search results.
// @include	https://www.google.*/*
// @exclude	https://www.google.*/voice/*
// @exclude	https://www.google.*/maps/*
// @version	1.02
// @grant       GM_addStyle
// @icon        http://i.imgur.com/ezjWmEO.png
// @namespace   http://greasyfork.icu/users/14186
// ==/UserScript==

/**
 * CSS INFO
 * #res div.mnr-c div.g = In the news Sub Results Favicon Position
 * #res div.mnr-c div.g img.favicon = In the news Favicon Alignment
 * div._OKe img.favicon = Special Results e.g. "Diamond Pushup”
 * #res div.mnr-c div.card-section = In the news Block Adjustments
 * #res div.mnr-c div._K2 img.favicon = In the news Picture Result Favicon Position
 * #res div._cnc img.favicon, #res div.card-section a._sQb img.favicon = News Tab Favicon Position
 * QUERY INFO
 * #res div.mnr-c div.g div._K2 div.thumb > a = In the news Picture Result
 * #res div.mnr-c div.g > div._I2:first-child > a._Dk = In the news First Result No Picture
 * #res li.g div.rc h3 a = Normal Results
 * #res div.g div.rc h3 a = Sub Site Results e.g. "Apple"
 * #res div.intrlu div.g h3 a = Local Results e.g. "Hair Salon"
 * #res div._cnc a = News Tab Results
 * #res div.mnr-c div.g > a._Dk = In the news Sub Results
 * #res div.card-section a._sQb = 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 {\
        padding-right: 4px;\
        vertical-align: middle;\
        border: none;\
        left: -24px;\
        position: absolute;\
        top: 2px;\
        z-index: 9;\
}\
#res div.mnr-c div.g {\
        position: relative;\
}\
#res div.mnr-c div.g img.favicon {\
        top: 0;\
        left: 1px\
}\
#res div.mnr-c div._K2 img.favicon {\
        margin-left: -25px;\
}\
#res div.mnr-c div.card-section {\
        padding-left: 25px;\
        left: -25px;\
        margin-right: -25px;\
}\
div._OKe img.favicon {\
        display: none;\
}\
#res div.mnr-c div._I2 img.favicon {\
        position: static;\
        margin-left: -24px;\
        padding-right: 8px;\
        margin-top: -2px;\
}\
#res div._cnc img.favicon, #res div.card-section a._sQb img.favicon {\
        position: static;\
        left: 0px;\
        padding-right: 6px;\
        padding-bottom: 2.3px;\
}\
");

	var FAVICON_GRABBER = 'https://www.google.com/s2/favicons?domain='; // 'http://favicon.yandex.net/favicon/'
var QUERY = '#res li.g div.rc h3 a, #res div.g div.rc h3 a, #res div.intrlu div.g h3 a, #res div._cnc a, #res div.mnr-c div.g > a._Dk, #res div.mnr-c div.g div._K2 div.thumb > a, #res div.mnr-c div.g > div._I2:first-child > a._Dk, #res div.card-section a._sQb';

/**
 * @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);

})();