Last active
August 29, 2015 13:58
-
-
Save sonyseng/9967972 to your computer and use it in GitHub Desktop.
Delete Every other element
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
// Get an existing child element that is not the passed in element | |
function getFirstChildThatIsNot(parent, child) { | |
return parent.children[0] != child ? parent.children[0] : parent.children[1]; | |
} | |
// Get the parent node and delete everything except the passed in element | |
function deleteSiblingsOf(element) { | |
var parent = element.parentNode; | |
while (getFirstChildThatIsNot(parent, element)) { | |
parent.removeChild(getFirstChildThatIsNot(parent, element)); | |
} | |
} | |
// Bottom Up recursive removal of DOM elements except for the passed in element | |
function removeEverythingBut(element) { | |
if (!element) { | |
throw new ReferenceError("HTMLElement does not exist"); | |
} | |
deleteSiblingsOf(element); | |
if (element.parentNode.tagName != 'HTML') { | |
removeEverythingBut(element.parentNode); | |
} | |
} | |
////////////// Test it out with different sites ////////////// | |
// Remove everything but the Hulu Flash Video. There's more than one embed tag on hulu's main page | |
removeEverythingBut(document.querySelectorAll('embed')[1]); | |
// Remove everything but the youtube flash Video | |
removeEverythingBut(document.querySelector('embed')); | |
// Remove everything but the youtube HTML5 Video | |
removeEverythingBut(document.querySelector('video')); |
@mavalur I use the javascript console. In Chrome, I just add it as a snippet and then execute it on some DOM element.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's nice.. How do you inject this javascript with in the page?. I am referring to an external website