Skip to content

Instantly share code, notes, and snippets.

@mgiuffrida
Last active August 29, 2015 14:01
Show Gist options
  • Save mgiuffrida/f1971a9ef741c6c8fbf4 to your computer and use it in GitHub Desktop.
Save mgiuffrida/f1971a9ef741c6c8fbf4 to your computer and use it in GitHub Desktop.
Remove elements from a page by clicking on them.
(function() {
var d = document;
d.styleSheets[0].addRule('.h7374',
'background:red !important');
var escape = function(e) {
if (e.keyCode==27)
cancel();
},
getElements = function() {
return d.body.getElementsByTagName('*');
},
highlight = function(e) {
if (this.classList)
this.classList.add('h7374');
e.stopPropagation();
return false;
},
removeHighlight = function(e) {
if (this.classList)
this.classList.remove('h7374');
e.stopPropagation();
return false;
},
removeElement = function(e) {
var rightClick;
if (e.which) rightClick = (e.which == 3);
else if (e.button) rightClick = (e.button == 2);
if (rightClick)
cancel();
else
this.parentNode.removeChild(this);
e.preventDefault();
e.stopPropagation();
return false;
},
maybeCancel = function(e) {
var rightClick;
if (e.which) rightClick = (e.which == 3);
else if (e.button) rightClick = (e.button == 2);
if (rightClick)
cancel();
e.preventDefault();
e.stopPropagation();
return false;
},
cancel = function() {
var i = 0,
o = d,
els = getElements();
while (o = els.item(i++)) {
if (o.classList)
o.classList.remove('h7374');
o.removeEventListener('mouseover', highlight);
o.removeEventListener('mouseout', removeHighlight);
o.removeEventListener('mousedown', maybeCancel);
o.removeEventListener('mouseup', removeElement);
}
d.removeEventListener('keydown', escape);
},
i = 0,
o,
els = getElements();
while(o = els.item(i++)) {
o.addEventListener('mouseover', highlight);
o.addEventListener('mouseout', removeHighlight);
o.addEventListener('mousedown', maybeCancel);
o.addEventListener('mouseup', removeElement);
}
d.addEventListener('keydown', escape);
})()
(function(){function p(e){e.stopPropagation()}function r(o,e,l){o.addEventListener(e,l)}function s(o,e,l){o.removeEventListener(e,l)}function e(){for(var a=0,b=c,d=q('*');b=d.item(a++);){var z=b.classList;z&&z.remove('h7374');s(b,t,f),s(b,u,g),s(b,x,o),s(b,v,h);s(c,w,k);}}function h(a){(a.which==3||a.button==2)?e():this.parentNode.removeChild(this);a.preventDefault();p(a);return 0}function o(a){if(a.which==3||a.button==2)e();a.preventDefault();p(a);return 0}function g(a){var z=this.classList;z&&z.remove('h7374');p(a);return 0}function f(a){var z=this.classList;z&&z.add('h7374');p(a);return 0}function k(a){27==a.keyCode&&e()}var c=document,n=c.body,q=n.getElementsByTagName.bind(n),t='mouseover',u='mouseout',x='mousedown',v='mouseup',w='keydown';c.styleSheets[0].addRule('.h7374','background:red !important');for(var l=0,d,m=q('*');d=m.item(l++);)r(d,t,f),r(d,u,g),r(d,x,o),r(d,v,h);r(c,w,k)})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment