Revision 326530663566 () - Diff

Link to this snippet: http://friendpaste.com/29sHyVu1J3iljnGks30yY3
Embed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// jQuery function to find all the text nodes inside an element
$.fn.textNodes = function() {
// array we will store all the text nodes in
var ret = [];

// nodes we want to skip
var restricted_nodes = {'SCRIPT':1, 'NOSCRIPT':1, 'STYLE':1};

$.each(this[0].childNodes, function() {
if (this.nodeType == 3 && $.trim(this.nodeValue)) {
ret.push(this);
} else if (!restricted_nodes.hasOwnProperty(this.nodeName)) {
$.each(this.childNodes, arguments.callee);
}
});
return ret;
}

var textNodes = $('body').textNodes();

var text = '';
for (var i = 0; i < textNodes.length; ++i) {
text += textNodes[i].nodeValue + ' ';
}
// text is now a string containing the full text of a webpage with no html


// see http://docs.jquery.com/Release:jQuery_1.3.2#:visible.2F:hidden_Overhauled
// for details on how jquery handles finding hidden elements