![](/img/trans.png)
[英]how to assign a top-level const from a promise when top-level await is not available
[英]Firefox bootstrapped extension, race conditions when importing in top-level?
我試圖讓自己熟悉Firefox自舉插件。 考慮以下示例:
// 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);
}
chrome://my-ext/content/foo.jsm
就是this.EXPORTED_SYMBOLS = [];
。
上面的代碼示例的問題在於它並非每次都起作用。 它可能會因NS_ERROR_FILE_NOT_FOUND
而不是導入而失敗,或者可能會說“確定”-盡管后來啟動瀏覽器時,我可以通過位置欄訪問foo.jsm
。
這是否意味着我不應該在頂層導入任何內容,因為可能尚未完成chrome注冊,或者問題出在其他地方?
注意 :以下是我的經驗,可能不確定
自舉插件的bootstrap.js在瀏覽器啟動時以及創建任何WINDOW或DOM之前運行。
bootstrap.js還具有執行的順序和特定格式。
以上示例是bootstrap.js的方式嗎?
您可以在任何地方導入Firefox模塊(盡管我總是會始終這樣做)。
最好執行您的Components.utils.import('chrome://my-ext/content/foo.jsm');
在function startup(data, reason) { ... }
其他注釋/建議:
通常,我會在函數外部分配var prompts
,這樣就不會在每次運行函數時都重新分配它。 我也將使用Services.jsm
以便於使用(但沒有任何區別),例如:
Components.utils.import('resource://gre/modules/Services.jsm');
// then anywhere in the code
Services.prompt.alert(null, title, text);
通常,我將所有Firefox內置模塊都導入到JS / JSM之上的Components.utils.import()
Firefox內置了Firefox內置模塊 (而不是插件的模塊),因此不需要Components.utils.unload()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.