Skip to content

Instantly share code, notes, and snippets.

@sonyseng
Last active August 29, 2015 13:58
Show Gist options
  • Save sonyseng/9967972 to your computer and use it in GitHub Desktop.
Save sonyseng/9967972 to your computer and use it in GitHub Desktop.
Delete Every other element
// 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
Copy link

mavalur commented Apr 4, 2014

That's nice.. How do you inject this javascript with in the page?. I am referring to an external website

@sonyseng
Copy link
Author

sonyseng commented Apr 4, 2014

@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