JavaScript Ninja - Extracting Text-Content Using Old-School XPATH Query-Evaluating

Posted at




XPathResult.prototype.to_array = function(){
var me = this
, len = me.snapshotLength
, arr = []
, index
;

for(index=0 ; index<len ; index+=1){
arr.push( me.snapshotItem(index) );
}

return arr;
};



(function(elements){
"use strict";

elements.forEach(function(){
console.log(arguments);
});
}(
document.evaluate(
'//text()[not(ancestor::head and ancestor::meta and ancestor::link and ancestor::script and ancestor::style and ancestor::noscript and ancestor::code)]'
, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
).to_array()
));