-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatsite.min.js
12 lines (11 loc) · 3.4 KB
/
whatsite.min.js
1
2
3
4
5
6
7
8
9
10
11
12
var WhatSite=(function(){"use strict";var WhatSite=function(){return this.init();},_WhatSite=null;WhatSite.prototype.init=function(userOpts){if(typeof userOpts!=="undefined"&&typeof userOpts!=="object"){if(console&&console.log){console.log("WhatSite: invalid options passed to WhatSite().");}
return false;}
this.defaults={"affects":["favicon","body"],"sites":{"local":{"host":"local(host)?$","color":"#ff6fcf"},"dev":{"host":"(^|\\.)dev\\.","color":"#66ccff"},"test":{"host":"(^|\\.)(test|testing|qa)\\.","color":"#ffcc66"}}};this.opts=this.extend(this.defaults,userOpts);this.hostname=window.location.hostname;return this;};WhatSite.prototype.apply=function(){var realm=this.thisSite();if(realm!==false){this.affect(realm);}
return this;};WhatSite.prototype.thisSite=function(){var realm,host,patt,result;for(realm in this.opts.sites){if(this.opts.sites.hasOwnProperty(realm)){host=this.opts.sites[realm].host;if(typeof host!=="undefined"){patt=new RegExp(host);result=this.hostname.match(patt);if(result!==-1&&result!=null){return realm;}}}}
return false;};WhatSite.prototype.affect=function(realm){var affects=this.extend(this.opts.affects,this.opts.sites[realm].affects),color=this.opts.sites[realm].color,i;for(i=affects.length;i>=0;i-=1){if(typeof affects[i]==="undefined"){continue;}else if(typeof affects[i]==="function"){affects[i](color,realm);}else{switch(affects[i]){case"favicon":this.affectFavicon(color);break;case"body":this.affectBody(color);break;}}}
return this;};WhatSite.prototype.affectFavicon=function(color){var i,linkTags=document.getElementsByTagName("link"),icon=null,rel,iconImg,flatColor;for(i=linkTags.length;i>=0;i-=1){if(typeof linkTags[i]!=="object"){continue;}
rel=linkTags[i].getAttribute("rel");if(typeof rel==="undefined"){continue;}
if(rel==="shortcut icon"||rel==="icon"){icon=linkTags[i];break;}}
iconImg=new Image();flatColor=this.splitColor(color).join(",");iconImg.onload=function(){var canvas,ctx,newIcon;canvas=document.createElement("canvas");canvas.setAttribute("width","16px");canvas.setAttribute("height","16px");ctx=canvas.getContext("2d");ctx.lineCap="butt";ctx.drawImage(iconImg,0,0);ctx.beginPath();ctx.strokeStyle="rgba("+flatColor+",1)";ctx.lineWidth=4;ctx.moveTo(0,0);ctx.lineTo(canvas.width,0);ctx.stroke();ctx.beginPath();ctx.strokeStyle="rgba(0,0,0,0.7)";ctx.lineWidth=1;ctx.moveTo(0,2.5);ctx.lineTo(canvas.width,2.5);ctx.stroke();try{(newIcon=icon.cloneNode(true)).setAttribute("href",ctx.canvas.toDataURL());icon.parentNode.replaceChild(newIcon,icon);}catch(e){}};iconImg.src=icon.href;return this;};WhatSite.prototype.affectBody=function(color){var stripe,body;stripe=document.createElement("div");stripe.style.backgroundColor=color;stripe.style.zIndex=1000;stripe.style.position="fixed";stripe.style.top="0px";stripe.style.left="0px";stripe.style.width="100%";stripe.style.height="3px";body=document.getElementsByTagName("body")[0];body.insertBefore(stripe,body.childNodes[0]);return this;};WhatSite.prototype.extend=function(obj,defaults){var p;for(p in defaults){if(defaults.hasOwnProperty(p)){try{if(defaults[p].constructor===Object){obj[p]=this.extend(obj[p],defaults[p]);}else{obj[p]=defaults[p];}}catch(e){obj[p]=defaults[p];}}}
return obj;};WhatSite.prototype.splitColor=function(color){var result=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);return result?[parseInt(result[1],16),parseInt(result[2],16),parseInt(result[3],16)]:null;};if(!_WhatSite){_WhatSite=new WhatSite();}
return _WhatSite;}());