简体   繁体   English

Firefox引导扩展,在顶级导入时的竞争条件?

[英]Firefox bootstrapped extension, race conditions when importing in top-level?

I'm trying to get myself familiar with Firefox bootstrapped add-ons. 我试图让自己熟悉Firefox自举插件。 Consider following example: 考虑以下示例:

// bootstrap.js

'use strict'

function alert(message) {
    var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
    prompts.alert(null, "from my extension", message);
}

try {
    Components.utils.import('chrome://my-ext/content/foo.jsm');
    alert('ok');
} catch(e) {
    alert(e);
}

And chrome://my-ext/content/foo.jsm being just this.EXPORTED_SYMBOLS = []; chrome://my-ext/content/foo.jsm就是this.EXPORTED_SYMBOLS = []; .

The issue with an above code sample is that it does not work each time. 上面的代码示例的问题在于它并非每次都起作用。 It may fail with NS_ERROR_FILE_NOT_FOUND instead of importing, or may say OK — despite the fact later, when browser starts, I can access foo.jsm through location bar. 它可能会因NS_ERROR_FILE_NOT_FOUND而不是导入而失败,或者可能会说“确定”-尽管后来启动浏览器时,我可以通过位置栏访问foo.jsm

Does this mean I should not import anything in the top level, because chrome registration might not be done yet, or problem is in something else? 这是否意味着我不应该在顶层导入任何内容,因为可能尚未完成chrome注册,或者问题出在其他地方?

NB The following is my experience and may not be definite 注意 :以下是我的经验,可能不确定

bootstrap.js of bootstrapped addon run when browser starts and BEFORE any WINDOW or DOM is created. 自举插件的bootstrap.js在浏览器启动时以及创建任何WINDOW或DOM之前运行。

bootstrap.js also has a order and specific format for execution. bootstrap.js还具有执行的顺序和特定格式。

Is above example the way your bootstrap.js is? 以上示例是bootstrap.js的方式吗?

You can import Firefox modules anywhere (although I would always do so on top). 您可以在任何地方导入Firefox模块(尽管我总是会始终这样做)。

It is best to execute your Components.utils.import('chrome://my-ext/content/foo.jsm'); 最好执行您的Components.utils.import('chrome://my-ext/content/foo.jsm'); within function startup(data, reason) { ... } function startup(data, reason) { ... }

Additional Notes/Suggestions: 其他注释/建议:

Generally, I would assign var prompts outside a function so that it is not reassigned every time a function is run. 通常,我会在函数外部分配var prompts ,这样就不会在每次运行函数时都重新分配它。 I would also use Services.jsm for ease of use (but it make no difference at all) eg: 我也将使用Services.jsm以便于使用(但没有任何区别),例如:

Components.utils.import('resource://gre/modules/Services.jsm');

// then anywhere in the code
Services.prompt.alert(null, title, text);

Habitually, I do all Firefox built-in modules imports Components.utils.import() on top of the JS/JSM 通常,我将所有Firefox内置模块都导入到JS / JSM之上的Components.utils.import()
Firefox built-in modules (not addon's modules) are cached by Firefox and there is no need to Components.utils.unload() them. Firefox内置了Firefox内置模块 (而不是插件的模块),因此不需要Components.utils.unload()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM