2

This question teaches how to get all TextNodes inside the document, and this is getting me the Javascript texts as well. What is the best way to filter out all the Nodes that are Javascript code?

lvella
  • 12,754
  • 11
  • 54
  • 106

2 Answers2

13

Text inside <script> tags has only one thing in common: their parent is a <script> element.

if (node.parentNode.nodeName !== 'SCRIPT')

Another approach is to use the filter:

var rejectScriptTextFilter = {
  acceptNode: function(node) {
    if (node.parentNode.nodeName !== 'SCRIPT') {
      return NodeFilter.FILTER_ACCEPT;
    }
  }
};

var walker = document.createTreeWalker(
  document.body, 
  NodeFilter.SHOW_TEXT, 
  rejectScriptTextFilter,
  false
);

var node;
var textNodes = [];

while(node = walker.nextNode()) {
  textNodes.push(node.nodeValue);
}

console.log(textNodes);
<script> var str = "script here"; </script>
<p> text here </p>
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

You could clone the original document, remove <script> elements at cloned document, then iterate remaining nodes of cloned document

guest271314
  • 1
  • 15
  • 104
  • 177