首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用WebExtensions API操作下载的文件

使用WebExtensions API操作下载的文件
EN

Stack Overflow用户
提问于 2016-08-17 12:17:02
回答 1查看 810关注 0票数 0

我想使用他们的WebExtensions API创建一个火狐扩展。扩展名应该查找与名称中的特定模式匹配的任何试图下载的文件。如果扩展发现尝试下载的名称与特定模式匹配,它将将下载放到另一个硬编码目录中。

看起来很简单。但是,在查看了Firefox中的可用函数之后,我发现没有任何可用的功能可以轻松地做到这一点。下面是我看过的主要链接:

WebExtensionsJavaScript API下载

WebExtensionsJavaScript API下载downloads.onCreated

似乎都不符合我要找的东西。有人知道我怎么能做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2016-08-17 15:33:10

使用WebExtensions的“适当”方法看起来还不是(?)受Firefox支持。Google有一个'onDeterminingFilename‘事件,它允许你向用户“推荐”一个文件名。如果在这种情况下需要,允许用户覆盖扩展的“建议”可能会更好。

执行此操作的蛮力方法是向downloads.onCreated添加侦听器。该侦听器被传递给一个downloads.DownloadItem,它将为您提供urlfilename。然后,监听器可以downloads.cancel()下载,并使用您希望的downloads.download()启动一个新的filename

在现实中,这将如何工作取决于,确切地说,downloads.onCreated事件实际上在何时触发与用户的交互。它是在与用户进行“另存为”对话框之前还是之后触发的?您将需要测试这一点,以了解什么最适合您希望用户拥有的感觉。downloads.State类型可以包含的有限值集表明,事件仅在“另存为”对话框完成后才触发,但这可能不是一个准确的指示。如果它在“另存为”对话框之前触发,我们可以通过将saveAs: true属性添加到传递给downloads.download()的对象中来显示该对话框和文件名。

类似于(未经测试的):

代码语言:javascript
代码运行次数:0
运行
复制
var ignoreDownloadUrls=[];
var ignoreDownloadIds=[];

chrome.downloads.onCreated.addlistener(forceDownloadFilenameIfUrlMatch);

function forceDownloadFilenameIfUrlMatch(downloadItem){
    //Check the URL for duplication (may not be needed, need to test).
    let foundUrlIndex = ignoreDownloadUrls.findIndex(function(element,index){
        if(element === downloadItem.url){
            return true;
        }//else
        return false;
    });
    if(foundUrlIndex>-1) {
        //This is the callback resulting from our creating the download
        //  with the new filename. We only want to ignore the URL once,
        //  so, remove the URL from our ignore list.
        //  This makes the assumption that the events we get are reasonably
        //  orderly (only one created event per URL which we want to
        //  change, etc.). This may need to be more complex in real use.
        ignoreDownloadUrls.splice(foundUrlIndex,1);
        //We do not want to change this download, as we created it.
        return;
    }
    //Test the ID for one we created:
    let foundIdIndex = ignoreDownloadIds.findIndex(function(element,index){
        if(element === downloadItem.id){
            return true;
        }//else
        return false;
    });
    if(foundIdIndex>-1) {
        //This is an event resulting from our creating the download
        //  with the new filename.
        //We do not want to change this download, as we created it.
        return;
    }
    if(downloadItem.url.idexOf(myMatchString) >-1){
        chrome.downloads.cancel(downloadItem.id);
        //Remember the URL so we don't change it twice.
        //  Remembering the URL may not be the best way to do this. Need
        //  to test as to the order which events and the download callback
        //  occur.
        ignoreDownloadUrls.push(download.url);
        chrome.downloads.download({
            url: downloadItem.url,
            filename: myNewFilename
        }, function(id){
        //We could use the callback of the downloads.download() to remember
        //  the downloadId of the download we created and only ignore that
        //  one.  That would rely on the callback being called prior to the
        //  new onCreated event. Might work that way in practice. It
        //  would certainly be better to remember the specifically created
        //  downloadId rather than just ignore (once) a specific URL.
        //  Testing should indicate if we need to use the download ID,
        //  or ignore the URL (once).
            //Remember the downloadId so we don't change it twice.
            ignoreDownloadIds.push(id);
        });
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38996430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档