-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dom): added snippet to clean up the page
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Removes all elements except for the trees rooted | ||
in the given selectors. | ||
For example, given a document | ||
body | ||
div.foo | ||
span | ||
div#bar | ||
div#baz | ||
hello | ||
and command with selectors ('.foo', '#baz') will leave | ||
everything in place, but command ('#baz') will leave just | ||
body | ||
div#bar | ||
div#baz | ||
hello | ||
*/ | ||
(function hideAllBut() { | ||
function toArray(what) { | ||
return Array.prototype.slice.call(what, 0); | ||
} | ||
const selectors = toArray(arguments); | ||
if (!selectors.length) { | ||
throw new Error('Need at least one selector to leave'); | ||
} | ||
|
||
const keep = selectors.map(function (selector) { | ||
return document.querySelector(selector); | ||
}); | ||
|
||
function shouldKeep(el) { | ||
return keep.some(function (keepElement) { | ||
return keepElement.contains(el) || el.contains(keepElement); | ||
}); | ||
} | ||
|
||
const all = toArray(document.body.querySelectorAll('*'), 0); | ||
var removed = 0; | ||
|
||
all.forEach(function (el) { | ||
if (!shouldKeep(el)) { | ||
el.parentNode.removeChild(el); | ||
removed += 1; | ||
} | ||
}); | ||
|
||
console.log('removed %d elements', removed); | ||
}('.foo', '#baz')); |
This file contains 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