Last active
February 20, 2021 13:35
-
-
Save ronekko/823b4b8df1e8579404ff to your computer and use it in GitHub Desktop.
faviconize google, fixed for update of google at 2015/07/23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Faviconize Google (nested icons) | |
// @namespace http://userscripts.ru/js/faviconize-google/ | |
// @description Adds favicons to each link offered by Google search results. | |
// @include https://www.google.*/search?* | |
// @include https://encrypted.google.*/search?* | |
// @include https://www.google.*/webhp* | |
// @include https://www.google.*/#* | |
// @include https://groups.google.*/groups/search?* | |
// @copyright 2009+, Nikita Vasilyev (http://userscripts.org/scripts/show/58177) | |
// @version 1.9.1 | |
// @licence Apache 2.0 | |
// @icon http://nv.github.com/faviconize-google.js/chrome/icon_48.png | |
// ==/UserScript== | |
(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); | |
})(".favicon {\ | |
padding-right: 4px;\ | |
vertical-align: middle;\ | |
border: none;\ | |
}\ | |
#res .favicon {\ | |
left: 0;\ | |
position: absolute;\ | |
top: 2px;\ | |
z-index: 9;\ | |
margin-left: -20px;\ | |
}\ | |
li.g,\ | |
div.g {\ | |
position: relative;\ | |
padding-left: 20px !important;\ | |
}"); | |
var FAVICON_GRABBER = 'https://www.google.com/s2/favicons?domain='; // 'http://favicon.yandex.net/favicon/' | |
var QUERY = '#res .g .yuRUbf a'; | |
/** | |
* @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.body.addEventListener('DOMNodeInserted', debounce(function handleNewFavicons(event){ | |
if (event.target.className != 'favicon') { | |
add_favicons_to(document.querySelectorAll(QUERY)); | |
} | |
}, 500) | |
, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment