| a | b | |
|---|
| 0 | + | // jQuery function to find all the text nodes inside an element |
|---|
| 0 | + | $.fn.textNodes = function() { |
|---|
| 0 | + | // array we will store all the text nodes in |
|---|
| 0 | + | var ret = []; |
|---|
| 0 | + | |
|---|
| 0 | + | // nodes we want to skip |
|---|
| 0 | + | var restricted_nodes = {'SCRIPT':1, 'NOSCRIPT':1, 'STYLE':1}; |
|---|
| 0 | + | |
|---|
| 0 | + | $.each(this[0].childNodes, function() { |
|---|
| 0 | + | if (this.nodeType == 3 && $.trim(this.nodeValue)) { |
|---|
| 0 | + | ret.push(this); |
|---|
| 0 | + | } else if (!restricted_nodes.hasOwnProperty(this.nodeName)) { |
|---|
| 0 | + | $.each(this.childNodes, arguments.callee); |
|---|
| 0 | + | } |
|---|
| 0 | + | }); |
|---|
| 0 | + | return ret; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | var textNodes = $('body').textNodes(); |
|---|
| 0 | + | |
|---|
| 0 | + | var text = ''; |
|---|
| 0 | + | for (var i = 0; i < textNodes.length; ++i) { |
|---|
| 0 | + | text += textNodes[i].nodeValue + ' '; |
|---|
| 0 | + | } |
|---|
| 0 | + | // text is now a string containing the full text of a webpage with no html |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | // see http://docs.jquery.com/Release:jQuery_1.3.2#:visible.2F:hidden_Overhauled |
|---|
| 0 | + | // for details on how jquery handles finding hidden elements |
|---|
| ... | |
|---|