Handle download dialog box in SlimerJS

前端 未结 3 1506
轻奢々
轻奢々 2021-01-21 06:24

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 p

相关标签:
3条回答
  • 2021-01-21 07:03

    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

    0 讨论(0)
  • 2021-01-21 07:12

    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(){
    
    });
    
    0 讨论(0)
  • 2021-01-21 07:26

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

    0 讨论(0)
提交回复
热议问题