Created
June 14, 2010 18:19
-
-
Save oquno/438056 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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