Skip to content

Instantly share code, notes, and snippets.

@met
Last active January 11, 2021 18:25
Show Gist options
  • Save met/02ccdea916b2a3a2be91ec8048eaf75b to your computer and use it in GitHub Desktop.
Save met/02ccdea916b2a3a2be91ec8048eaf75b to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/pogigom find all text nodes inside "article p", exclude style, script etc.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<article>
<p>MacLeod has named this phenomenon the <a href="https://uwaterloo.ca/">“production effect”.</a> It means
that producing written words – that’s to say, reading them out loud – improves our memory of them.</p>
<p>The production effect has been replicated in numerous studies spanning more than
a decade. <a href="https://uwaterloo.ca/">In one study in Australia</a>, a group of
seven-to-10-year-olds were presented with a list of words and asked to read some silently,
and others aloud. Afterwards, they correctly recognised 87% of the words they’d read aloud,
but only 70% of the silent ones.</p>
<p>In another study, <a href="https://uwaterloo.ca/">adults aged 67 to 88</a> were given
the same task – reading words either silently or aloud – before then writing down all
those they could remember. They were able to recall 27% of the words they had read aloud,
but only 10% of those they’d read silently. When asked which ones they recognised,
they were able to correctly identify 80% of the words they had read aloud,
but only 60% of the silent ones. MacLeod and his team have found the effect can last up to
a week after the reading task.</p>
<p><!-- Komentar text node. Komentar text node. --></p>
<p><style>Style text node. Style text node.</style></p>
<p><script>/* Script text node. Script text node. */</script></p>
<p><noscript>/* Script text node. Script text node. */</noscript></p>
</article>
<script id="jsbin-javascript">
// textuji taky na
// https://www.bbc.com/future/article/20200917-the-surprising-power-of-reading-aloud
// https://www.irozhlas.cz/zpravy-domov/biochemik-trnka-navrat-do-normalniho-zivota-ockovani-covid-19-proockovanost-70_2101101650_vis
let pList = document.querySelectorAll("article p");
pList.forEach(function(item, index, objList) {
//console.log({ index, item})
runWalker(item);
});
function runWalker(item) {
const walker = document.createTreeWalker(
item,
NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
let parentName = node.parentNode.nodeName;
if (["script", "noscript", "style", "svg"].includes(parentName.toLowerCase())) {
console.log("Recected: " + parentName);
return NodeFilter.FILTER_REJECT;
}
else {
return NodeFilter.FILTER_ACCEPT;
}
}
});
let node;
while (node = walker.nextNode()) {
let text = node.wholeText;
const minimumLength = 20; // minimum text node length for processing
// skip whitespace only noder
// ship short nodes
// skip nodes without whitepaces (one words nodes)
if (text.match(/^\s+$/) || text.length < minimumLength || !text.match(/\s/) ) {
console.log("Skipped uninteresting text string: " + text);
continue;
}
console.log(node.nodeName + " : " + node.wholeText);
// TODO process text node here
filterNodes(node);
}
}
// filter text content here
function filterNodes(node) {
// TODO
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">// textuji taky na
// https://www.bbc.com/future/article/20200917-the-surprising-power-of-reading-aloud
// https://www.irozhlas.cz/zpravy-domov/biochemik-trnka-navrat-do-normalniho-zivota-ockovani-covid-19-proockovanost-70_2101101650_vis
let pList = document.querySelectorAll("article p");
pList.forEach(function(item, index, objList) {
//console.log({ index, item})
runWalker(item);
});
function runWalker(item) {
const walker = document.createTreeWalker(
item,
NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
let parentName = node.parentNode.nodeName;
if (["script", "noscript", "style", "svg"].includes(parentName.toLowerCase())) {
console.log("Recected: " + parentName);
return NodeFilter.FILTER_REJECT;
}
else {
return NodeFilter.FILTER_ACCEPT;
}
}
});
let node;
while (node = walker.nextNode()) {
let text = node.wholeText;
const minimumLength = 20; // minimum text node length for processing
// skip whitespace only noder
// ship short nodes
// skip nodes without whitepaces (one words nodes)
if (text.match(/^\s+$/) || text.length < minimumLength || !text.match(/\s/) ) {
console.log("Skipped uninteresting text string: " + text);
continue;
}
console.log(node.nodeName + " : " + node.wholeText);
// TODO process text node here
filterNodes(node);
}
}
// filter text content here
function filterNodes(node) {
// TODO
}
</script></body>
</html>
// textuji taky na
// https://www.bbc.com/future/article/20200917-the-surprising-power-of-reading-aloud
// https://www.irozhlas.cz/zpravy-domov/biochemik-trnka-navrat-do-normalniho-zivota-ockovani-covid-19-proockovanost-70_2101101650_vis
let pList = document.querySelectorAll("article p");
pList.forEach(function(item, index, objList) {
//console.log({ index, item})
runWalker(item);
});
function runWalker(item) {
const walker = document.createTreeWalker(
item,
NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
let parentName = node.parentNode.nodeName;
if (["script", "noscript", "style", "svg"].includes(parentName.toLowerCase())) {
console.log("Recected: " + parentName);
return NodeFilter.FILTER_REJECT;
}
else {
return NodeFilter.FILTER_ACCEPT;
}
}
});
let node;
while (node = walker.nextNode()) {
let text = node.wholeText;
const minimumLength = 20; // minimum text node length for processing
// skip whitespace only noder
// ship short nodes
// skip nodes without whitepaces (one words nodes)
if (text.match(/^\s+$/) || text.length < minimumLength || !text.match(/\s/) ) {
console.log("Skipped uninteresting text string: " + text);
continue;
}
console.log(node.nodeName + " : " + node.wholeText);
// TODO process text node here
filterNodes(node);
}
}
// filter text content here
function filterNodes(node) {
// TODO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment