我想使用他们的WebExtensions API创建一个火狐扩展。扩展名应该查找与名称中的特定模式匹配的任何试图下载的文件。如果扩展发现尝试下载的名称与特定模式匹配,它将将下载放到另一个硬编码目录中。
看起来很简单。但是,在查看了Firefox中的可用函数之后,我发现没有任何可用的功能可以轻松地做到这一点。下面是我看过的主要链接:
WebExtensions➞JavaScript API➞下载
WebExtensions➞JavaScript API➞下载➞downloads.onCreated
似乎都不符合我要找的东西。有人知道我怎么能做到这一点吗?
发布于 2016-08-17 15:33:10
使用WebExtensions的“适当”方法看起来还不是(?)受Firefox支持。Google有一个'onDeterminingFilename‘事件,它允许你向用户“推荐”一个文件名。如果在这种情况下需要,允许用户覆盖扩展的“建议”可能会更好。
执行此操作的蛮力方法是向downloads.onCreated
添加侦听器。该侦听器被传递给一个downloads.DownloadItem
,它将为您提供url
和filename
。然后,监听器可以downloads.cancel()
下载,并使用您希望的downloads.download()
启动一个新的filename
。
在现实中,这将如何工作取决于,确切地说,downloads.onCreated
事件实际上在何时触发与用户的交互。它是在与用户进行“另存为”对话框之前还是之后触发的?您将需要测试这一点,以了解什么最适合您希望用户拥有的感觉。downloads.State
类型可以包含的有限值集表明,事件仅在“另存为”对话框完成后才触发,但这可能不是一个准确的指示。如果它在“另存为”对话框之前触发,我们可以通过将saveAs: true
属性添加到传递给downloads.download()
的对象中来显示该对话框和文件名。
类似于(未经测试的):
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);
});
}
}
https://stackoverflow.com/questions/38996430
复制相似问题