Firefox addon 我可以在无重启插件的bootstrap.js中加载自定义jsm模块吗?

Firefox addon 我可以在无重启插件的bootstrap.js中加载自定义jsm模块吗?,firefox-addon,firefox-addon-restartless,jsm,Firefox Addon,Firefox Addon Restartless,Jsm,我正在尝试使用以下方法在无重启加载项中加载自定义模块: chrome/content/modules/Test.jsm: var EXPORTED_SYMBOLS = [ 'Test' ]; let Test = {}; content test chrome/content/ const Cu = Components.utils; // Tried this first, but figured perhaps chrome directives aren't loaded h

我正在尝试使用以下方法在无重启加载项中加载自定义模块:

chrome/content/modules/Test.jsm

var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {};
content   test  chrome/content/
const Cu = Components.utils;

// Tried this first, but figured perhaps chrome directives aren't loaded here yet
// let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;

function install() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function uninstall() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function startup() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function shutdown() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {
    install: function( data, reason ) {
    },

    /* etc */

    bootstrap: function( context ) {
        context.install = this.install;
        context.uninstall = this.uninstall;
        context.startup = this.startup;
        context.shutdown = this.shutdown;
    }
}
const Cu = Components.utils;
Cu.import( 'chrome://test/modules/Test.jsm' );
Test.bootstrap( this );
chrome.manifest

var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {};
content   test  chrome/content/
const Cu = Components.utils;

// Tried this first, but figured perhaps chrome directives aren't loaded here yet
// let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;

function install() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function uninstall() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function startup() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function shutdown() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {
    install: function( data, reason ) {
    },

    /* etc */

    bootstrap: function( context ) {
        context.install = this.install;
        context.uninstall = this.uninstall;
        context.startup = this.startup;
        context.shutdown = this.shutdown;
    }
}
const Cu = Components.utils;
Cu.import( 'chrome://test/modules/Test.jsm' );
Test.bootstrap( this );
bootstrap.js

var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {};
content   test  chrome/content/
const Cu = Components.utils;

// Tried this first, but figured perhaps chrome directives aren't loaded here yet
// let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;

function install() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function uninstall() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function startup() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function shutdown() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {
    install: function( data, reason ) {
    },

    /* etc */

    bootstrap: function( context ) {
        context.install = this.install;
        context.uninstall = this.uninstall;
        context.startup = this.startup;
        context.shutdown = this.shutdown;
    }
}
const Cu = Components.utils;
Cu.import( 'chrome://test/modules/Test.jsm' );
Test.bootstrap( this );
但是,我得到了以下类型的警告消息(这条消息用于
shutdown()
,但对于所有函数和之前在全局范围内的尝试基本相同):

1409229174591 addons.xpi警告运行引导方法时出现异常 关闭test@extensions.codifier.nl:[异常…”组件 返回的故障代码:0x80070057(NS\U错误\U非法值) [nsexpccomponents_Utils.import]“nsresult:”0x80070057 (NS\u错误\u非法\u值)“位置:”JS帧:: resource://gre/modules/addons/XPIProvider.jsm -> file:///test/bootstrap.js ::关机::第21行“数据:否]堆栈 跟踪:shutdown()@resource://gre/modules/addons/XPIProvider.jsm -> file:///test/bootstrap.js:21 < XPI_callBootstrapMethod()@resource://gre/modules/addons/XPIProvider.jsm:4232 < XPI_UpdatedDonDisabledState()@resource://gre/modules/addons/XPIProvider.jsm:4347 < AddonWrapper_userDisabledSetter()@resource://gre/modules/addons/XPIProvider.jsm:6647
bootstrap.js
中是否还没有
chrome.manifest
指令?或者我正在尝试的是某种安全违规行为,也许?还是我只是做错了一些小事


我希望实现的是,我可以做如下事情:

chrome/content/modules/Test.jsm

var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {};
content   test  chrome/content/
const Cu = Components.utils;

// Tried this first, but figured perhaps chrome directives aren't loaded here yet
// let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;

function install() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function uninstall() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function startup() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function shutdown() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {
    install: function( data, reason ) {
    },

    /* etc */

    bootstrap: function( context ) {
        context.install = this.install;
        context.uninstall = this.uninstall;
        context.startup = this.startup;
        context.shutdown = this.shutdown;
    }
}
const Cu = Components.utils;
Cu.import( 'chrome://test/modules/Test.jsm' );
Test.bootstrap( this );
bootstrap.js

var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {};
content   test  chrome/content/
const Cu = Components.utils;

// Tried this first, but figured perhaps chrome directives aren't loaded here yet
// let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;

function install() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function uninstall() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function startup() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
function shutdown() {
  let test = Cu.import( 'chrome://test/modules/Test.jsm', {} ).Test;
}
var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {
    install: function( data, reason ) {
    },

    /* etc */

    bootstrap: function( context ) {
        context.install = this.install;
        context.uninstall = this.uninstall;
        context.startup = this.startup;
        context.shutdown = this.shutdown;
    }
}
const Cu = Components.utils;
Cu.import( 'chrome://test/modules/Test.jsm' );
Test.bootstrap( this );
也许一开始有点过头了,但我只是有点喜欢将实现隐藏在模块和/或对象中,并保持
bootstrap.js
超级干净


如果你碰巧对如何通过其他方式实现这一目标提出了建议:我洗耳恭听。

是的,你可以说你的道路是错误的

只要这样做:

let test = Cu.import( 'chrome://test/content/modules/Test.jsm', {} ).Test;
注意
/content/

您不必执行
.Test
,除非您希望小写的
Test
保存它。您只需执行以下操作:

Cu.import( 'chrome://test/content/modules/Test.jsm');
并使用as
Test.blah
,其中blah是JSM模块中的任何内容

此代码可以放在任何地方,它不必位于
安装功能中

请确保卸载自定义JSM模块,否则会导致对内存有害的僵尸隔间。请看这里:

  • 最后一段:
  • 更多阅读,但可选:

除了@Noitidart的答案之外,如果您只关心如何导入模块,则不必使用chrome.manifest'并注册内容包

功能安装(数据、原因){
import(data.resourceURI.spec+“relative/path/to/your/module.jsm”);
}

要这样做,请确保在
安装
启动
功能中,它们都有
数据
的第一个参数(关闭和卸载也是如此,但这对于
卸载模块更为重要)。或者,如果要在安装函数之外执行此操作,请将全局变量或其他设置设置为安装的
data
arg。请参见此处了解数据对象中包含的内容:啊,这也很好。非常感谢。不过,目前我更喜欢在函数之外使用的解决方案。你不会碰巧知道在全局范围内实现这一点的方法吧?(可能不是,因为函数中只有
resourceURI.spec
可用是有意义的),但请参阅我的问题编辑,了解我希望实现的目标。也许你还有一些想法。谢谢。如果您的目标是将
bootstrap.js
最小化,您可能会对使用
loadSubScript
感到满意,谢谢!与此同时,我也发现了关于
\uu脚本\uuri\u规范\uuuu
(他使用的)的信息,这正是我希望找到的东西。如果在你的回答中提到这一点,我会尽可能地打分。谢谢你的回答!我稍后会尝试这个,但我相信它是有效的。但是,如果愿意,请帮助我理解:为什么这里需要//**content**/modules,而当我在一个非重启版本中使用
资源://
名称空间时,这是不需要的。(不幸的是,您可能已经知道,引导加载项中不允许使用资源)。我的意思是,目录
content
不是已经在指令
content test chrome/content/
中定义了吗?为什么我仍然需要将其添加到url中?我在哪里可以找到更多关于这种机制的信息?附言:我已经意识到可能的内存泄漏。不过还是要谢谢你提醒我!这就是chrome.manifest的工作原理,他们是这样做的:)我现在已经测试了你的建议。但是,不幸的是,该模块仅在
startup()
shutdown()
中可用;不在全局范围或其他函数中。在这些警告中:
没有注册chrome软件包chrome://test/content/modules/Test.jsm
。我希望我可以通过做类似于
Test.bootstrap(这个)的事情,将引导完全委托给我的jsm模块,但现在看来这是不可能的。我将用一个我希望实现的例子来更新我的问题。也许你,或者其他任何人,仍然对如何实现类似的目标有一些想法。这很有趣,我不知道。我想一旦chrome.manifest注册,你就可以在boostrap.js中使用它,好吧,这对我来说是一个新的。谢谢你;是的,我做了一些实验,你最早可以称之为“启动”: