Handle download dialog box in SlimerJS

2019-02-25 12:18发布

I have written a script that clicks on a link which can download a mp3 file. The problem I am facing is when the script simulates the click on that link, a download dialog box pops up like this:
Download Dialog Box

Now, I want to save this file to some path of my choice and automate this whole process. I am clueless on how to handle this dialog box.

3条回答
叛逆
2楼-- · 2019-02-25 12:36

You can't control a dialog box. SlimerJS doesn't have API for this action.

查看更多
祖国的老花朵
3楼-- · 2019-02-25 12:44

Here's a script adapted from this blog post to download a file.

In SlimerJS it is possible to use response.body inside the onResourceReceived handler. However to prevent using too much memory it does not get anything by default. You have to first set page.captureContent to say what you want. You assign an array of regexes to page.captureContent to say which files to receive. The regex is applied to the mime-type. In the example code below I use /.*/ to mean "get everything". Using [/^image/.+$/] should just get images, etc.

var fs=require('fs');
var page = require('webpage').create();

fs.makeTree('contents');

page.captureContent = [ /.*/ ];

page.onResourceReceived = function(response) {

    if(response.stage!="end" || !response.bodySize)
    {
        return;
    }

    var matches = response.url.match(/[/]([^/]+)$/);
    var fname = "contents/"+matches[1];

    console.log("Saving "+response.bodySize+" bytes to "+fname);
    fs.write(fname,response.body);

    phantom.exit();
};

page.onResourceRequested = function(requestData, networkRequest) {
    //console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};

page.open("http://....mp3", function(){

});
查看更多
Deceive 欺骗
4楼-- · 2019-02-25 12:44

Firefox generates a temp "downloadfile.extension.part" file which contains the content. Just simply rename the file ex. myfile.csv.part > myfile.csv

locally if working on a mac you should find the .part file in the downloads directory, on linux /temp/ folder

Not the most elegant solution but should do the trick

查看更多
登录 后发表回答