简体   繁体   中英

Trigger multiple links to download files

I have a webpage where I can find a random number of files to download. I wanted to develop a little FireFox extension to download all files at once. So far I have the following function to download these files:

function getFilesJQ () {
    var $dlLinks = $('a').filter(function(index) { return $(this).text() === "Download"; });

    $dlLinks.each(function () {
        $(this).trigger('click');
    });
}

The problem is, that only the first link is clicked and the corresponding file starts to download. The others are not triggered.

Where lies the problem? Is this even possible?

Edit:

This is how $dlLinks looks like:

{…}
0: <a id="exerciseResourcesForm:a3:0:linkDownload" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('exerciseResourcesForm'),{'exerciseResourcesForm:a3:0:linkDownload':'exerciseResourcesForm:a3:0:linkDownload'},'');}return false">
1: <a id="exerciseResourcesForm:a3:1:linkDownload" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('exerciseResourcesForm'),{'exerciseResourcesForm:a3:1:linkDownload':'exerciseResourcesForm:a3:1:linkDownload'},'');}return false">
2: <a id="exerciseResourcesForm:a3:2:linkDownload" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('exerciseResourcesForm'),{'exerciseResourcesForm:a3:2:linkDownload':'exerciseResourcesForm:a3:2:linkDownload'},'');}return false">
3: <a id="exerciseResourcesForm:a3:3:linkDownload" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('exerciseResourcesForm'),{'exerciseResourcesForm:a3:3:linkDownload':'exerciseResourcesForm:a3:3:linkDownload'},'');}return false">
length: 4
prevObject: Object { 0: a, 1: a, 2: a, … }
__proto__: Object { jquery: "3.2.1 -ajax,-ajax/jsonp,-ajax/load,-ajax/parseXML,-ajax/script,-ajax/var/location,-ajax/var/nonce,-ajax/var/rquery,-ajax/xhr,-manipulation/_evalUrl,-event/ajax,-effects,-effects/Tween,-effects/animatedSelector", constructor: r(), length: 0, … }
jack_download.js:10:2

When one of those links is clicked, to POST requests are issued:

POST 
https://*******/emp/ExerciseEdit.jsf 
[HTTP/1.1 200 OK 28ms]
POST 
https://sb-ssl.google.com/safebrowsing/clientreport/download 
[HTTP/2.0 200 OK 109ms]
$(this).trigger('click');

This probably opens the link on the current page, which terminates the current page. You may try to open it in a new one:

window.open(this.href, "Download");

One thing to take into consideration it the target attribute of these links. If the target is not defined, then your click action is going to open a new session, and your subsequent downloads will be cancelled. One simple way around this is to "force" these links to open in new windows:

function getFilesJQ () {
    var $dlLinks = $('a').filter(function(index) { return $(this).text() === "Download"; });

    $dlLinks.each(function () {
        window.open(this.href);
    });
}

However, please be aware that your browser may interpret this as multiple popups and try to block them. You may also have to turn off your popup blocker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM