我正在尝试使用以下方法在无重新启动的插件中加载一个自定义模块:
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;
}但是,我得到了以下类型的警告消息(这一条用于shutdown(),但对于所有函数和在早期的全局范围尝试中基本上相同):
1409229174591 addons.xpi警告在test@extensions.codifier.nl上运行引导方法关闭的异常:[异常.“组件返回的失败代码: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) nsIXPCComponents_Utils.import”ns结果:"0x80070057 (NS_ERROR_ILLEGAL_VALUE)“位置:"JS框架::资源://gre/modules/addons/xpiProvider.jsm -> nsIXPCComponents_Utils.import ::shutdown ::第21行”数据: no]堆栈跟踪: shutdown()@resource://gre/modules/addons/XPIProvider.jsm -> file:///test< 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 <卸载()@extensions.xml:1541
chrome.manifest指令在bootstrap.js中还不可用吗?还是我在尝试某种违反安全的行为?还是我只是做了些微不足道的错事?
我希望实现的是,我可以做以下的事情:
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 );也许它一开始就有点过头了,但我只是有点喜欢将实现隐藏在模块和/或对象中并保持bootstrap.js超级干净的想法。
如果你碰巧对如何通过其他方法实现这一目标有建议的话:我会全神贯注。
发布于 2014-08-28 14:52:38
是的,你可以走错路了。
就这么做吧:
let test = Cu.import( 'chrome://test/content/modules/Test.jsm', {} ).Test;注意/content/
您不需要执行.Test,除非您希望小写的test保持它。你可以这样做:
Cu.import( 'chrome://test/content/modules/Test.jsm');并将其用作Test.blah,其中blah是JSM模块中的任何内容。
这段代码可以去任何地方,它不需要在install函数中。
确保卸载自定义的JSM模块,否则会导致僵尸分区,这对内存有害。请在此阅读:
发布于 2014-08-28 15:08:53
除了@Noitidart的回答之外,如果您唯一关心的是如何导入模块,则不必使用chrome.manifest‘注册内容包。
function install(data, reason) {
Components.utils.import(data.resourceURI.spec + "relative/path/to/your/module.jsm");
}https://stackoverflow.com/questions/25549173
复制相似问题