【问题标题】:Firefox Extension DevelopmentFirefox 扩展开发
【发布时间】:2014-09-25 15:24:21
【问题描述】:

在 Chrome 扩展开发中,我们有后台页面概念。 Firefox 扩展开发中是否也有类似的东西。在开发 Chrome 扩展程序时,我见过像

这样的方法

window.Bkg = chrome.extension.getBackgroundPage().Bkg;

$(function () {
  var view = null;
  if (Bkg.account.isLoggedIn()) {
    view = new Views.Popup();
    $("#content").append(view.render().el);
  } else {
    $("#content").append(Template('logged_out')());
    Bkg.refresh();
  }
}...........

主要逻辑写在后台页面(如 isLoggedIn 等)和扩展弹出页面中,我们称为后台页面。例如,这里总是加载管理会话的背景页面。我们如何在 Firefox 扩展开发中拥有类似的功能。

【问题讨论】:

  • 公平地说,这不是编写 Chrome 扩展的好方法。

标签: google-chrome-extension firefox-addon-sdk


【解决方案1】:

后台页面 (main.js) 和内容脚本(您的弹出脚本)之间的所有通信都是通过事件进行的。您不能立即调用函数,因此您不会收到任何返回值,但您可以将事件从内容脚本发送到后台脚本,后台脚本将事件发送回内容脚本并调用新函数,如下所示:

ma​​in.js 部分

// See docs below on how to create a panel/popup
panel.port.on('loggedIn', function(message) {
  panel.port.emit('isLoggedIn', someBoolean);
});
panel.port.on('refresh', function() {
  // do something to refresh the view
});

popup.js

var view = null;
addon.port.on('isLoggedIn', function(someBool) {
  if (someBool) {
    // Obviously not code that's going to work in FF, just want you to know how to structure
    view = new Views.Popup();
    $("#content").append(view.render().el);
  } else {
    $("#content").append(Template('logged_out')());
    addon.port.emit('refresh');
  }
});
addon.port.emit('loggedIn', 'This is a message. I can pass any var along with the event, but I don't have to');

您应该阅读以下内容:

  1. Panel
  2. Communicating between scripts

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多