Search code examples
javascriptfirefox-addondom-events

Similar Event onDeterminingFilename in Firefox


I have developed a Chrome extension, in which I have added a listener to onDeterminingFilename Event, it checks if download file name is of a particular format then it renames it and displays it in Save As. Dialog.

Now I want to develop a similar extension in Firefox, but I am unable to find any event which can be triggered before a file download is done.If there is no such event, Can anyone suggest how to rename a file programmatically before starting the download ?

Edit 1 : As antoyo suggested, I have updated the code

Here I can create a new Download to start with same URL and different filename but cannot cancel the existing download. I do not want user to download same file twice.

Calling cancel() does nothing. How do I cancel on going download?

Or is there a way to access download files before getting added to Download Summary?

var self = require('sdk/self');
const { Cc, Ci, Cu } = require('chrome');
Cu.import("resource://gre/modules/Downloads.jsm");
Cu.import("resource://gre/modules/osfile.jsm")
Cu.import("resource://gre/modules/Task.jsm");

Downloads.getList(Downloads.PUBLIC).then(function(downloadList) {
    downloadList.addView({
        onDownloadAdded: function(download) {
            var fname = download.target.path;
            var url = download.source.url;
            download.cancel();

            // check if name is not be changed
            if (true){
              label = "filename.txt";      
              d_path = "/home";            

              Task.spawn(function () {
                yield Downloads.fetch(url, OS.Path.join(d_path, label));
              }).then(null, Cu.reportError);
            }

            download.start().then(function(){
              console.log("start");
            });
            download.launch().then(function(){
              console.log("launch");
            });
            download.whenSucceeded().then(function() {
                console.log('WhenSucceeded');                    
            });
        },
        onDownloadChanged: download => console.log("Changed", download),
        onDownloadRemoved: download => console.log("Removed", download)
    });
});

Solution

  • You can solve your issue by using the Downloads.jsm module.

    To observe downloads event, you need to add a view on a DownloadList.

    Afterwards, you can do anything you want with a Download object.

    Here is an example code, using the Add-on SDK:

    const { Cu } = require('chrome');
    
    Cu.import('resource://gre/modules/Downloads.jsm');
    Cu.import('resource://gre/modules/osfile.jsm');
    Cu.import('resource://gre/modules/Task.jsm');
    
    Downloads.getList(Downloads.PUBLIC).then(function(downloadList) {
        downloadList.addView({
            onDownloadAdded: function(download) {
                download.cancel();
    
                let filename = download.target.path;
                let url = download.source.url;
                let name = 'filename.txt';
                let directory = '/home';
    
                Task.spawn(function() {
                    yield Downloads.fetch(url, OS.Path.join(directory, name));
                }).then(null, Cu.reportError);
            },
        });
    });
    

    This cancels all new download and starts a new download.