[英]Download multiple files from a page using javascript and jQuery in firefox extension
I developed a Firefox extension where downloads multiple files from a page by triggering on different anchors with different links. 我开发了Firefox扩展程序,通过在具有不同链接的不同锚点上触发来从页面下载多个文件。
What I did in javascript with jQuery: 我在jQuery的javascript中所做的工作:
var $tr = $("tr");
var downloadLinks = [];
$.each($tr, function() {
var tds = $(this).find("td");
var name = tds.eq(1).find("span");
var text = name.text();
if (text.match(/\.Update\./gi)) {
return true;
}
downloadLinks.push(tds.eq(2).find("a"));
});
if (downloadLinks.length > 0 && confirm("Found " + downloadLinks.length + " items to be downloaded. Download them now ?")) {
for (var i = 0; i < downloadLinks.length; i++) {
var $a = downloadLinks[i];
var url = $a.prop("href");
if (url.indexOf("http") < 0) {
url = windows.location.protocol + "//" + windows.location.host + "/" + $a.prop("href").trim('/');
}
$a.prop("href", url);
setTimeout(function() {
$a.get(0).click();
}, 800 * (i + 1));
}
}
The problem is that only last item is downloaded downloadLinks.length
times. 问题是只有最后一项下载了
downloadLinks.length
次。 If length is 20 then last item is downloaded 20 times. 如果长度为20,则最后一项下载20次。
Where is my mistake ? 我的错误在哪里?
$a is the problem here, when time out is fired $a is equals to the last object read by the loop. $ a是这里的问题,当触发超时时,$ a等于循环读取的最后一个对象。
try with: 尝试:
setTimeout(function(j) {
var $a = downloadLinks[j];
$a.get(0).click();
}, 800 * (i + 1), i);
this should work. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.