Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 2.19 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*UNIT TESTED CODE FOR CRAWLING A PAGE AND GATHERING THE TEXT USING CHROME DEBUGGER...
  2. * OPEN THE URL:'http://download.oracle.com/javase/1.5.0/docs/api/index.html'
  3. * fire the script
  4. d = new DocumentSaver();
  5. d.processDocument(document, function(text_t){
  6.                 console.log('********HTML TEXT******** ');
  7.                 console.log(text_t)
  8. })
  9. RESULT: you will see the HTML text gathered from the page...
  10. */
  11.  
  12. function DocumentSaver() {
  13.         this.textContent = ''; // Root of the cloned document
  14.         this.ignoredTypes = [ 'SCRIPT', 'TITLE', 'META', 'STYLE', 'LINK','CANVAS', 'VIDEO','AUDIO', 'AREA', 'IMG', 'MAP', 'EMBED', 'OBJECT'
  15.                                                 , 'PARAM', 'SOURCE', 'DEVICE', 'NOSCRIPT' ,'HEAD']
  16.  
  17.         this.indexOf = function(arr, item, from) {
  18.                 if (arr.indexOf) return arr.indexOf(item, from);
  19.                 var len = arr.length;
  20.                 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++) {
  21.                         if (arr[i] === item) return i;
  22.                 }
  23.                 return -1;
  24.         };
  25.         this.processRecursive = function(rootNode) {
  26.  
  27.                 for (var child = rootNode.firstChild; child != null; child = child.nextSibling){
  28.                         if(child.tagName  && child.tagName.toUpperCase()=='FRAME'){
  29.                                 var self = this;
  30.                                 var iframeSaver = new DocumentSaver();
  31.                                
  32.                                 iframeSaver.processDocument(child.contentDocument, function(Iframetext){
  33.                                         self.textContent = self.textContent +" "+Iframetext;
  34.                                 })
  35.                         }
  36.                         if(child.tagName && this.indexOf(this.ignoredTypes, child.tagName.toUpperCase()) != -1){
  37.                                 continue;
  38.                         }
  39.                         //console.log(child);
  40.                         this.processRecursive(child);
  41.                        
  42.                         switch (child.nodeType) {
  43.                         case child.TEXT_NODE:
  44.                                 if(child.textContent.trim()!=''){
  45.                                 //      console.log(child);
  46.                                         this.textContent = " "+this.textContent+" "+child.textContent;
  47.                                 }
  48.                                 break;
  49.                         default:
  50.                         //  console.log('Unhandled node: ' + child.nodeName);
  51.                           break; /* TODO */
  52.                         }
  53.                 }
  54.  
  55.         }
  56.    
  57.   this.processDocument = function(doc, callback) {
  58.     // TODO check content type
  59.     this.doc = doc;
  60.     this.callback = callback;
  61.    
  62.     var rootNode = doc.getElementsByTagName('html')[0];
  63.     if (!rootNode) {
  64.       console.error("No html node in document");
  65.       return;
  66.     }
  67.     this.textContent = "";
  68.     // TODO process html root too
  69.     this.processRecursive( rootNode );
  70.     this.callback(this.textContent );
  71.   }
  72. }