Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name favidenticon
// @namespace http://oq.la/
// @include *
// @require http://github.com/hgwr/identicon/raw/master/identicon.js
// ==/UserScript==
(function() {
var icon_1 = document.evaluate('//head/link[@rel="shortcut icon"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var icon_2 = document.evaluate('//head/link[@rel="icon"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if ( icon_1 || icon_2 ) {
return;
}else{
var req = new XMLHttpRequest();
req.open('GET', location.protocol + "//" + location.host + "/favicon.ico", false);
req.send(null);
if(req.status>=400)
insertFavidenticon();
}
function insertFavidenticon(){
var size = 32;
var domain = encodeURIComponent(document.domain);
domain = domain.replace(/[^0-9a-z]/ig,'');
var domain_id = parseInt(domain, 36);
while(Math.pow(2,32) > domain_id*10)
domain_id*=10;
while(Math.pow(2,32) < domain_id)
domain_id/=10;
domain_id = Math.floor(domain_id);
var can = document.createElement('canvas');
can.width = size;
can.height = size;
can.id = 'tempcan';
can.style.display = 'none';
document.body.appendChild(can);
var favicon = document.createElement('link');
favicon.rel = 'shortcut icon';
favicon.id = 'identifavicon';
var head = document.getElementsByTagName('head')[0];
new Identicon(can.id, domain_id, size);
favicon.href = can.toDataURL();
head.appendChild(favicon);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment