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?