2

I'm trying to load a custom module in a restartless add-on, using the following:

chrome/content/modules/Test.jsm:

var EXPORTED_SYMBOLS = [ 'Test' ];

let Test = {};

chrome.manifest:

content   test  chrome/content/

bootstrap.js:

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;
}

However, I get the following types of WARN messages (this one was for shutdown(), but basically identical for all functions and in the earlier attempt in the global scope):

1409229174591 addons.xpi WARN Exception running bootstrap method shutdown on test@extensions.codifier.nl: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXPCComponents_Utils.import]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: resource://gre/modules/addons/XPIProvider.jsm -> file:///test/bootstrap.js :: shutdown :: line 21" data: no] Stack trace: shutdown()@resource://gre/modules/addons/XPIProvider.jsm -> file:///test/bootstrap.js:21 < XPI_callBootstrapMethod()@resource://gre/modules/addons/XPIProvider.jsm:4232 < XPI_updateAddonDisabledState()@resource://gre/modules/addons/XPIProvider.jsm:4347 < AddonWrapper_userDisabledSetter()@resource://gre/modules/addons/XPIProvider.jsm:6647 < uninstall()@extensions.xml:1541 < oncommand()@about:addons:1 <

Are chrome.manifest directives not yet available in bootstrap.js? Or is what I am attempting some kind of security violation, perhaps? Or am I simply doing something trivially wrong?


What I was hoping to achieve, is that I could do something like the following:

chrome/content/modules/Test.jsm:

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;
    }
}

bootstrap.js:

const Cu = Components.utils;
Cu.import( 'chrome://test/modules/Test.jsm' );
Test.bootstrap( this );

Perhaps it's a bit over the top to begin with, but I just kind of like the idea of hiding implementations in modules and/or objects and keeping bootstrap.js super clean.

If you happen to have suggestions on how to achieve this by other means: I'm all ears.

4

2 回答 2

2

是的,你可以你的路径是错误的。

只需这样做:

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

注意/content/

.Test除非您希望小写字母test保留它,否则您不必这样做。你可以这样做:

Cu.import( 'chrome://test/content/modules/Test.jsm');

并用作Test.blahJSM 模块中的任何内容。

这段代码可以去任何地方,它不必在install函数中。

确保卸载自定义 JSM 模块,否则会导致僵尸隔间,这对内存不利。在这里阅读:

于 2014-08-28T14:52:38.690 回答
1

除了@Noitidart 的回答,如果您唯一关心的是如何导入模块,则不必使用 chrome.manifest' 并注册内容包。

function install(data, reason) {
  Components.utils.import(data.resourceURI.spec + "relative/path/to/your/module.jsm");

}
于 2014-08-28T15:08:53.030 回答