Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created January 12, 2009 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edvakf/45842 to your computer and use it in GitHub Desktop.
Save edvakf/45842 to your computer and use it in GitHub Desktop.
javascript: (function(doc) {
function addListeners(){
var add = window.addEventListener;
add('mouseover',overHandler,false);
add('mouseout',outHandler,false);
add('click',clickHandler,false);
add('keypress',keyHandler,false);
}
function removeListeners(){
var rm = window.removeEventListener;
rm('mouseover',overHandler,false);
rm('mouseout',outHandler,false);
rm('click',clickHandler,false);
rm('keypress',keyHandler,false);
}
addListeners();
var theElm;
var theColor='orange';
var flag=true;
function overHandler(e){
var elm = e.target;
if(elm==doc.body||elm==doc.documentElement)return;
theElm = elm;
addBG(theElm);
}
function outHandler(e){
removeBG(theElm);
}
function clickHandler(e){
removeBG(theElm);
if(flag){
killOtherThan(theElm);
flag=false;
theColor='deepskyblue';
theElm=null;
}else kill(theElm);
/*removeListeners();*/
e.preventDefault();
}
function addBG(elm){
var st = elm.style;
if(st.backgroundColor)st.origBG = st.backgroundColor;
st.backgroundColor = theColor;
}
function removeBG(elm){
if(!elm)return;
var st = elm.style;
st.backgroundColor = st.origBG||'';
delete st.origBG;
}
function keyHandler(e){
try{Hash[e.keyCode||e.which](e)}catch(e){}
}
var Hash = {
27:function(e){removeBG(theElm);if(flag){flag=false;theColor='deepskyblue';addBG(theElm)}else removeListeners()}/*Esc*/
,38:function(e){if(theElm.parentNode!=doc.body){removeBG(theElm);addBG(theElm=theElm.parentNode);}e.preventDefault()}/*up*/
,13:function(e){clickHandler(e)}/*Enter*/
,115:function(e){/*s*/
removeBG(theElm);
var html = doc.documentElement.cloneNode(true);
var elms = [].slice.call(html.getElementsByTagName('*'));
elms.forEach(function(e){
var tag = e.tagName.toLowerCase();
if(tag=='script'||tag=='meta'||e.display=='none'){e.parentNode.removeChild(e);}
if(e.src)e.src=e.src;
if(e.href)e.href=e.href;
});
var xml=(new XMLSerializer).serializeToString(html);
location.href='data:application/octet-stream,'+encodeURIComponent(xml);
removeBG(theElm);removeListeners();
}
};
function killOtherThan(ele){
while(ele!=doc.body){
[].forEach.call(ele.parentNode.childNodes,function(e){if(e!=ele && e.style)e.style.display='none'});
var st = ele.style;
st.width='100%';
st.padding=st.margin=0;
ele=ele.parentNode;
}
}
function kill(ele){
if(!ele)return;
ele.parentNode.removeChild(ele);
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment