2016-12-10 9 views
7

Mam następujący kod:Grupa zagnieżdżone odnośniki według pierwszego poziomu iframe wykorzystujące casperjs

var casper = require("casper").create({ 
     // verbose: true, 
     // logLevel: "debug", 
     webSecurityEnabled : false 
    }); 
var links = []; 
function get_links(obj) { 
    return obj.evaluate(function() { 
     var i, 
      l = document.querySelectorAll("a"), 
      l2 = []; 
     for (i = 0; i < l.length; i++) { 
      l2[i] = l[i].href; 
     } 
     return l2 
    }); 
} 
function unique(arr) { 
    var obj = {}; 
    for (var i = 0; i < arr.length; i++) { 
     if (/http(.*)?/.test(arr[i])) { 
      var str = arr[i]; 
      obj[str] = true; 
     } 
    } 
    return Object.keys(obj); 
} 

function getLinksFromIframes(callback) { 
    this.echo("Here we come: " + this.getCurrentUrl() + "\n"); 
    function to_frame(obj) { 
     var iframes = to_evaluate(obj); 
     iframes.forEach(function (index) { 
      this.withFrame(index, function() { 
       this.echo("We are here: " + this.getCurrentUrl()); 
       var l = unique(get_links(this)); 
       var i; 
       for (i = 0; i < l.length; i++) { 
        console.log(l[i]); 
        links.push(l[i]) 
       } 
       links = unique(links); 
       console.log(""); 
       to_frame(this) 
      }); 
     }, obj); 
    } 
    function to_evaluate(obj) { 
     return obj.evaluate(function() { 
      var iframes = []; 
      [].forEach.call(document.querySelectorAll("iframe"), function (iframe, i) { 
       iframes.push(i); 
      }); 
      return iframes; 
     }) 
    } 
    to_frame(this); 
    this.then(function() { 
     callback.call(this); 
    }); 
} 

casper.start("http://domu-test-2/node/1", function() { 
    getLinksFromIframes.call(this, function() { 
     console.log("Done!\n"); 
     var i; 
     for (i = 0; i < links.length; i++) { 
      console.log(links[i]); 
     } 
    }); 
}).then(function() {}).run(); 

a teraz jest pytanie:

jeśli chcę dostać linki autorem pierwszego poziomu iframe, w jaki sposób powinienem zmienić funkcję getLinksFromIframes(). Obecnie mają one "globalną" zmienną links. Myślę, że zdecydowanie links będzie listą linków i zainicjować nową listę w ramach funkcji withFrame, a następnie przekazać to nowe odniesienie do elementów podrzędnych iframe. Jak mam go przekazać i "z powrotem śledzić" wszystkie linki w zagnieżdżonych elementach iframe?

Odpowiedz

1

Jeśli zrobię to poprawnie, wybierz, który element iframe otrzymasz od linków i tylko z tego elementu iframe. Jeśli tak jest, to możesz po prostu użyć switchToChildFrame, aby przejść do pożądanego elementu iframe, a następnie po prostu uzyskać linki, dzwoniąc pod numer get_links(obj).

Mój przykład ma 3 strony. An index.html, który ładuje iframe1.html wewnątrz iframe i iframe1.html ma inne iframe wewnątrz, które ładuje iframe2.html. Każdy plik ma 3 linki wewnętrzne:

index.html

<a href="link1/from/index">Link 1 from index</a> 
<a href="link2/from/index">Link 2 from index</a> 
<a href="link3/from/index">Link 3 from index</a> 

<iframe src="iframe1.html"></iframe> 

iframe1.html

<a href="link1/from/iframe1">Link 1 from iframe 1</a> 
<a href="link2/from/iframe1">Link 2 from iframe 1</a> 
<a href="link3/from/iframe1">Link 3 from iframe 1</a> 

<iframe src="iframe2.html"></iframe> 

iframe2.html

<a href="link1/from/iframe2">Link 1 from iframe 2</a> 
<a href="link2/from/iframe2">Link 2 from iframe 2</a> 
<a href="link3/from/iframe2">Link 3 from iframe 2</a> 

i refactored getLinksFromIframes funkcja byłoby tak:

function getLinksFromIframes(callback) { 
    this.echo("Here we come: " + this.getCurrentUrl() + "\n"); 

    function to_frame(obj) { 
     obj.echo("We are here: " + obj.getCurrentUrl()); 
     var l = unique(get_links(obj)); 
     var i; 
     for (i = 0; i < l.length; i++) { 
      console.log(l[i]); 
      links.push(l[i]) 
     } 
     links = unique(links); 
     console.log(""); 
    } 
    function to_evaluate(obj) { 
     return obj.evaluate(function() { 
      var iframes = []; 
      [].forEach.call(document.querySelectorAll("iframe"), function (iframe, i) { 
       iframes.push(i); 
      }); 
      return iframes; 
     }) 
    } 

    // Leave both switchToChildFrame as comments to get the "index.html" links 
    this.page.switchToChildFrame(0); // Uncomment to get the links of "iframe1.html" 
    //this.page.switchToChildFrame(0); // Uncomment to get the links of "iframe2.html" 
    to_frame(this); 

    this.then(function() { 
     callback.call(this); 
    }); 
} 

WYNIKI

Jeśli komentować zarówno switchToChildFrame dostaniesz więzi między index.html:

casperjs caspers-read-iframes.js 
Here we come: http://pjs.lytrax.net/node/1/ 

We are here: http://pjs.lytrax.net/node/1/ 
http://pjs.lytrax.net/node/1/link1/from/index 
http://pjs.lytrax.net/node/1/link2/from/index 
http://pjs.lytrax.net/node/1/link3/from/index 

Done! 

http://pjs.lytrax.net/node/1/link1/from/index 
http://pjs.lytrax.net/node/1/link2/from/index 
http://pjs.lytrax.net/node/1/link3/from/index 

Jeśli odkomentowaniu pierwszy switchToChildFrame, otrzymasz linki pierwszego poziomu iframe1.html:

casperjs caspers-read-iframes.js 
Here we come: http://pjs.lytrax.net/node/1/ 

We are here: http://pjs.lytrax.net/node/1/iframe1.html 
http://pjs.lytrax.net/node/1/link1/from/iframe1 
http://pjs.lytrax.net/node/1/link2/from/iframe1 
http://pjs.lytrax.net/node/1/link3/from/iframe1 

Done! 

http://pjs.lytrax.net/node/1/link1/from/iframe1 
http://pjs.lytrax.net/node/1/link2/from/iframe1 
http://pjs.lytrax.net/node/1/link3/from/iframe1 

A jeśli Odkomentuj zarówno pierwszy jak i drugi switchToChildFrame, dostaniesz więzi drugim poziomie iframe2.html:

casperjs caspers-read-iframes.js 
Here we come: http://pjs.lytrax.net/node/1/ 

We are here: http://pjs.lytrax.net/node/1/iframe2.html 
http://pjs.lytrax.net/node/1/link1/from/iframe2 
http://pjs.lytrax.net/node/1/link2/from/iframe2 
http://pjs.lytrax.net/node/1/link3/from/iframe2 

Done! 

http://pjs.lytrax.net/node/1/link1/from/iframe2 
http://pjs.lytrax.net/node/1/link2/from/iframe2 
http://pjs.lytrax.net/node/1/link3/from/iframe2 
Powiązane problemy