拦截Firefox Addon SDK中的新下载 [英] Intercept new downloads in Firefox Addon SDK

查看:141
本文介绍了拦截Firefox Addon SDK中的新下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Windows编写了一个简单的下载管理器,并且我想为Firefox创建一个插件,当它启用时拦截Firefox中的新下载并将它们发送给下载管理器。



我已经为 Google Chrome 完成了这项工作:

  chrome.downloads.onCreated。 addListener(function(details){
//停止下载
chrome.downloads.cancel(details.id,null);
}

问题是如何使用 Firefox插件SDK 实现类似功能。



我看到拦截页面加载的方法来查看可能有用的内容/标题,但是我不知道请求是否会变成下载或不是。



Firefox加载项SDK:获取http响应标头



我也许可以找一个cont如果我没有正确处理所有的情况下,可能会导致问题。

是不是访问下载管理器使用JS SDK或某种方式知道什么时候下载已经开始/正在启动,并停止它? 解决方案

链接的问题讨论的 http-on-examine-response 观察者是错误的方法。它涉及的所有请求不仅仅是下载。



而是使用 Downloads.jsm 来观察新的下载内容,然后取消它们,等等。



在SDK中加载 Downloads.jsm 使用:

  const {Cu} = require(chrome); 
Cu.import(resource://gre/modules/Downloads.jsm);
Cu.import(resource://gre/modules/Task.jsm);

然后您可以添加您的侦听器。

  let view = {
onDownloadAdded:function(download){
console.log(Added,download);
},
onDownloadChanged:function(download){
console.log(Changed,download);
},
onDownloadRemoved:function(download){
console.log(Removed,download);
}
};

Task.spawn(function(){
try {
let list = yield Downloads.getList(Downloads.ALL);
yield list.addView(view) ;
} catch(ex){
console.error(ex);
}
});

链接的MDN文档包含更多信息和示例

由于您的加载项是一个无重启的SDK加载项,因此您需要在卸载时使用 .removeView 再次删除侦听器,否则将会有一个内存泄漏。

I have written a simple download manager for Windows and I would like to create an addon for Firefox that when enabled intercepts new downloads in Firefox and sends them to the download manager.

I have already done this for Google Chrome using:

chrome.downloads.onCreated.addListener(function(details) {
    // stop the download
    chrome.downloads.cancel(details.id, null);
}

The question is how can I achieve something similar using the Firefox add-on SDK.

I see there is a way of intercepting page loads to view the content / headers which might be helpful but then I won't know if the request will turn into a download or not.

Firefox add-on SDK: Get http response headers

I could perhaps look for a content type that is not text/html or check for a content disposition header but that could cause problems if I don't correctly handle all cases.

Is there no way of accessing the download manager using the JS SDK or some way of knowing when a download has been started / being started and stop it?

解决方案

The http-on-examine-response observer that the linked question discusses is the wrong way to go. It concerns all requests not just downloads.

Instead use the Downloads.jsm to observe new downloads, then cancel them, and so on.

To load Downloads.jsm in the SDK use:

const {Cu} = require("chrome");
Cu.import("resource://gre/modules/Downloads.jsm");
Cu.import("resource://gre/modules/Task.jsm");

Then you can add your listener.

let view = {
  onDownloadAdded: function(download) { 
    console.log("Added", download);
  },
  onDownloadChanged: function(download) {
    console.log("Changed", download);
  },
  onDownloadRemoved: function(download) {
    console.log("Removed", download);
  }
};

Task.spawn(function() {
  try {
    let list = yield Downloads.getList(Downloads.ALL);
    yield list.addView(view);
  } catch (ex) {
    console.error(ex);
  }
});

The linked MDN docs have more information and samples.

Since your add-on is a restartless SDK add-on, you'll need to remove the listener again using .removeView on unload, or else there will be a memory leak.

这篇关于拦截Firefox Addon SDK中的新下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆