如何从Firefox Add-on SDK扩展中启动普通下载

4
我正在为Firefox开发Add-on SDK扩展。我发现我需要能够启动下载,就像用户请求一样,即显示常规文件保存对话框或将文件保存到用户偏好的任何位置,因为它可以在“首选项>内容”下配置。
关于下载的每篇文章或文档似乎只考虑了我知道下载文件的位置的情况,但这不是我此时所需的。在这种情况下,需要像用户启动下载一样。
如何实现这一点,最好通过SDK的方法?

不错的问题。我也对答案很感兴趣,下载不是我的强项。 - Noitidart
1
我认为你需要深入了解Download.jsm,并查看它如何在Firefox UI中触发。这是一个好问题,我点赞了。 :) - therealjeffg
2个回答

2
“好的,你可以直接开始一个实际的保存操作。
从你的代码中启动一个保存链接的方法: 在上下文菜单中,oncommand值为gContextMenu.saveLink();。saveLink()的定义在chrome://browser/content/nsContextMenu.js中。它会做一些清理工作,然后调用saveHelper(),该函数在同一文件中定义。你可以使用适当的参数直接调用saveHelper()。它在chrome://browser/content/web-panels.xul中的面板中被包含。”
<script type="application/javascript" 
            src="chrome://browser/content/nsContextMenu.js"/>

然后在右键菜单的onpopupshowing事件处理程序中,声明在chrome://browser/content/browser.js中为nullgContextMenu变量被赋值为:
gContextMenu = new nsContextMenu(this, event.shiftKey);
它在onpopuphiding事件处理程序中返回到:
'gContextMenu = null;'

如果您想在自己的代码中使用它,可以这样做:

let urlToSave = "https://dev59.com/ZITba4cB1Zd3GeqP_M6g";
let linkText = "Some Link text";

//  Add a "/" to un-comment the code appropriate for your add-on type.
/* Overlay and bootstrap:
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
//*/
/* Add-on SDK:
var {Cc, Ci, Cr} = require("chrome");
//*/

if (window === null || typeof window !== "object") {
    //If you do not already have a window reference, you need to obtain one:
    //  Add a "/" to un-comment the code appropriate for your add-on type.
    /* Add-on SDK:
    var window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    /* Overlay and bootstrap (from almost any context/scope):
    var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator)
                         .getMostRecentWindow("navigator:browser");        
    //*/
}

//Create an object in which we attach nsContextMenu.js.
//  It needs some support properties/functions which
//  nsContextMenu.js assumes are part of its context.
let contextMenuObj = {
    makeURI: function (aURL, aOriginCharset, aBaseURI) {
      var ioService = Cc["@mozilla.org/network/io-service;1"]
                                .getService(Ci.nsIIOService);
      return ioService.newURI(aURL, aOriginCharset, aBaseURI);
    },
    gPrefService: Cc["@mozilla.org/preferences-service;1"]
                            .getService(Ci.nsIPrefService)
                            .QueryInterface(Ci.nsIPrefBranch),
    Cc: Cc,
    Ci: Ci,
    Cr: Cr
};

Cc["@mozilla.org/moz/jssubscript-loader;1"]
        .getService(Ci.mozIJSSubScriptLoader)
        .loadSubScript("chrome://browser/content/nsContextMenu.js"
                       ,contextMenuObj);

//Re-define the initMenu function, as there is not a desire to actually
//  initialize a menu.        
contextMenuObj.nsContextMenu.prototype.initMenu = function() { };

let myContextMenu = new contextMenuObj.nsContextMenu();
//Save the specified URL
myContextMenu.saveHelper(urlToSave,linkText,null,true,window.content.document);

使用loadSubScript加载nsContextMenu.js的替代方法:
我更喜欢使用loadSubScript从nsContextMenu.js中加载saveHelper代码。这样可以使代码保持最新,与未来Firefox版本所做的任何更改保持同步。然而,它引入了一个依赖关系,即您正在使用非官方API的功能。因此,在未来的Firefox版本中,它可能以某种方式发生变化,并要求您的插件进行更改。另一种选择是在您的扩展中复制saveHelper()代码。它被定义为以下内容:
// Helper function to wait for appropriate MIME-type headers and
// then prompt the user with a file picker
saveHelper: function(linkURL, linkText, dialogTitle, bypassCache, doc) {
  // canonical def in nsURILoader.h
  const NS_ERROR_SAVE_LINK_AS_TIMEOUT = 0x805d0020;

  // an object to proxy the data through to
  // nsIExternalHelperAppService.doContent, which will wait for the
  // appropriate MIME-type headers and then prompt the user with a
  // file picker
  function saveAsListener() {}
  saveAsListener.prototype = {
    extListener: null, 

    onStartRequest: function saveLinkAs_onStartRequest(aRequest, aContext) {

      // if the timer fired, the error status will have been caused by that,
      // and we'll be restarting in onStopRequest, so no reason to notify
      // the user
      if (aRequest.status == NS_ERROR_SAVE_LINK_AS_TIMEOUT)
        return;

      timer.cancel();

      // some other error occured; notify the user...
      if (!Components.isSuccessCode(aRequest.status)) {
        try {
          const sbs = Cc["@mozilla.org/intl/stringbundle;1"].
                      getService(Ci.nsIStringBundleService);
          const bundle = sbs.createBundle(
                  "chrome://mozapps/locale/downloads/downloads.properties");

          const title = bundle.GetStringFromName("downloadErrorAlertTitle");
          const msg = bundle.GetStringFromName("downloadErrorGeneric");

          const promptSvc = Cc["@mozilla.org/embedcomp/prompt-service;1"].
                            getService(Ci.nsIPromptService);
          promptSvc.alert(doc.defaultView, title, msg);
        } catch (ex) {}
        return;
      }

      var extHelperAppSvc = 
        Cc["@mozilla.org/uriloader/external-helper-app-service;1"].
        getService(Ci.nsIExternalHelperAppService);
      var channel = aRequest.QueryInterface(Ci.nsIChannel);
      this.extListener = 
        extHelperAppSvc.doContent(channel.contentType, aRequest, 
                                  doc.defaultView, true);
      this.extListener.onStartRequest(aRequest, aContext);
    }, 

    onStopRequest: function saveLinkAs_onStopRequest(aRequest, aContext, 
                                                     aStatusCode) {
      if (aStatusCode == NS_ERROR_SAVE_LINK_AS_TIMEOUT) {
        // do it the old fashioned way, which will pick the best filename
        // it can without waiting.
        saveURL(linkURL, linkText, dialogTitle, bypassCache, false,
                doc.documentURIObject, doc);
      }
      if (this.extListener)
        this.extListener.onStopRequest(aRequest, aContext, aStatusCode);
    },

    onDataAvailable: function saveLinkAs_onDataAvailable(aRequest, aContext,
                                                         aInputStream,
                                                         aOffset, aCount) {
      this.extListener.onDataAvailable(aRequest, aContext, aInputStream,
                                       aOffset, aCount);
    }
  }

  function callbacks() {}
  callbacks.prototype = {
    getInterface: function sLA_callbacks_getInterface(aIID) {
      if (aIID.equals(Ci.nsIAuthPrompt) || aIID.equals(Ci.nsIAuthPrompt2)) {
        // If the channel demands authentication prompt, we must cancel it
        // because the save-as-timer would expire and cancel the channel
        // before we get credentials from user.  Both authentication dialog
        // and save as dialog would appear on the screen as we fall back to
        // the old fashioned way after the timeout.
        timer.cancel();
        channel.cancel(NS_ERROR_SAVE_LINK_AS_TIMEOUT);
      }
      throw Cr.NS_ERROR_NO_INTERFACE;
    } 
  }

  // if it we don't have the headers after a short time, the user 
  // won't have received any feedback from their click.  that's bad.  so
  // we give up waiting for the filename. 
  function timerCallback() {}
  timerCallback.prototype = {
    notify: function sLA_timer_notify(aTimer) {
      channel.cancel(NS_ERROR_SAVE_LINK_AS_TIMEOUT);
      return;
    }
  }

  // set up a channel to do the saving
  var ioService = Cc["@mozilla.org/network/io-service;1"].
                  getService(Ci.nsIIOService);
  var channel = ioService.newChannelFromURI(makeURI(linkURL));
  if (channel instanceof Ci.nsIPrivateBrowsingChannel) {
    let docIsPrivate = PrivateBrowsingUtils.isWindowPrivate(doc.defaultView);
    channel.setPrivate(docIsPrivate);
  }
  channel.notificationCallbacks = new callbacks();

  let flags = Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;

  if (bypassCache)
    flags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;

  if (channel instanceof Ci.nsICachingChannel)
    flags |= Ci.nsICachingChannel.LOAD_BYPASS_LOCAL_CACHE_IF_BUSY;

  channel.loadFlags |= flags;

  if (channel instanceof Ci.nsIHttpChannel) {
    channel.referrer = doc.documentURIObject;
    if (channel instanceof Ci.nsIHttpChannelInternal)
      channel.forceAllowThirdPartyCookie = true;
  }

  // fallback to the old way if we don't see the headers quickly 
  var timeToWait = 
    gPrefService.getIntPref("browser.download.saveLinkAsFilenameTimeout");
  var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  timer.initWithCallback(new timerCallback(), timeToWait,
                         timer.TYPE_ONE_SHOT);

  // kick off the channel with our proxy object as the listener
  channel.asyncOpen(new saveAsListener(), null);
}

如何确定linkText? - Роман Коптев
@РоманКоптев,我不太确定你在问什么。如何确定链接文本取决于您已经拥有的信息。这个问题没有指定要下载链接的来源。因此,我没有太多上下文可以提供答案。如果您已经有了<A>元素,那么您可能可以使用Node.textContent来实现您想要的内容。我建议您提出一个新问题,并提供更多详细信息。[ask] - Makyen

1

就像@canuckistani所说,使用Downloads.jsm

let { Downloads } = require("resource://gre/modules/Downloads.jsm");
let { OS } = require("resource://gre/modules/osfile.jsm")
let { Task } = require("resource://gre/modules/Task.jsm");

Task.spawn(function () {

  yield Downloads.fetch("http://www.mozilla.org/",
                        OS.Path.join(OS.Constants.Path.tmpDir,
                                     "example-download.html"));

  console.log("example-download.html has been downloaded.");

}).then(null, Components.utils.reportError);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接