Skip to content

Instantly share code, notes, and snippets.

@agibsonsw
Created January 8, 2016 22:09
Show Gist options
  • Save agibsonsw/409e59d0dcef906dbc41 to your computer and use it in GitHub Desktop.
Save agibsonsw/409e59d0dcef906dbc41 to your computer and use it in GitHub Desktop.
Bookmarklet to display element stats on mouse over and click
javascript:
(function(){
var d=document,useMine=true,prevEl,info;
function addHandler(orig,mine){
return function(e){
if(useMine){
mine(e);
}
else
if(orig){
orig(e);
}
}
;
}
function GS(el,sRule){
var result='';
if(d.defaultView&&d.defaultView.getComputedStyle){
result=d.defaultView.getComputedStyle(el,'').getPropertyValue(sRule);
}
else
if(el.currentStyle){
sRule=sRule.replace(/\-(\w)/g,function (strMatch,p1){return p1.toUpperCase();});
result=el.currentStyle[sRule];
}
else{
result='n/a';
}
return (result=='auto')?"":(sRule+': '+result);
}
function myover(e){
var el=e?e.target:window.event.srcElement;
el.style.outline='1px solid red';
var sInfo=el.nodeName;
if(el.id)
sInfo+=" ID: "+el.id;
sInfo+="\n";
if(el.className)
sInfo+="Class: "+el.className+"\n";
sInfo+=GS(el,'width')+" "+GS(el,'height');
sInfo+="\n"+GS(el,'padding');
sInfo+="\n"+GS(el,'border');
sInfo+="\n"+GS(el,'margin');
sInfo+="\n"+GS(el,'display')+" "+GS(el,'position');
metrics=GS(el,'left')+" "+GS(el,'top')+" "+GS(el,'bottom')+" "+GS(el,'right');
if(metrics.length!=3){
sInfo+="\n"+metrics;
}
sInfo+="\n"+GS(el,'font');
sInfo+="\n"+GS(el,'float');
sInfo+=" "+GS(el,'z-index');
info.value=sInfo;
prevEl=el;
}
function myout(e){
var el=e?e.target:window.event.srcElement;
if(!el.keepOl)
el.style.outline='';
}
function mymove(e){
var evt=e||window.event;
var el=evt.target||evt.srcElement;
info.style.left=(evt.pageX+20)+"px";
info.style.top=(evt.pageY+10)+"px";
}
function myclick(e){
var evt=e||window.event;
var el=evt.target||evt.srcElement;
info.style.left=(evt.pageX+4)+"px";
info.style.top=(evt.pageY+4)+"px";
el.keepOl=true;
createBox();
if(el.href){
var temp=el.href;
el.href="#";
window.setTimeout(function (){el.href=temp;},20);
}
evt.preventDefault;
return false;
}
function mydown(e){
var evt=e||window.event;
if(evt.keyCode==27){
if(!prevEl.keepOl)
prevEl.style.outline='';
useMine=false;
info.parentNode.removeChild(info);
}
}
function createBox(){
info=d.createElement('textarea');
info.style.position="absolute";
info.style.width="250px";
info.style.height="180px";
info.style.zIndex="999";
info.style.fontFamily="Consolas";
info.style.fontSize="12px";
info.style.color="black";
info.style.backgroundColor="lightyellow";
info.style.paddingLeft="3px";
d.body.appendChild(info);
}
d.onmouseover=addHandler(d.onmouseover,myover);
d.onmouseout=addHandler(d.onmouseout,myout);
d.onmousemove=addHandler(d.onmousemove,mymove);
d.onclick=addHandler(d.onclick,myclick);
d.onkeydown=addHandler(d.onkeydown,mydown);
createBox();
}
)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment