我需要将设置从旧版本的扩展(XUL / Components.interfaces.nsIPrefService)迁移到新版本(WebExtension / browser.storage.sync)。为此,我从Mozilla迁移示例(embedded-webextension-overlay)导入了代码。在此示例中,使用browser.runtime.sendMessage从嵌入式webextension中,数据从嵌入式webextension发送到覆盖代码。它工作正常。但是我需要改变方向,为此我在代码的覆盖部分调用browser.runtime.sendMessage,我收到消息: browser.runtime.sendMessage不是函数 这是一个修改后的覆盖代码:
{
const addonId = "myAddon@mySite.com";
const {
AddonManager,
} = Components.utils.import("resource://gre/modules/AddonManager.jsm", {});
AddonManager.getAddonByID(addonId, addon => {
const baseURI = addon.getResourceURI("/");
const {
LegacyExtensionsUtils,
} = Components.utils.import("resource://gre/modules/LegacyExtensionsUtils.jsm");
const myOverlayEmbeddedWebExtension = LegacyExtensionsUtils.getEmbeddedExtensionFor({
id: addonId, resourceURI: baseURI,
});
myOverlayEmbeddedWebExtension.startup().then(({browser}) => {
dump(`${addonId} - embedded webext started\n`);
// My code
browser.runtime.sendMessage({msg:"update-settings", data:my_data});
/* Original Mozilla code
browser.runtime.onMessage.addListener(msg => {
dump(`${addonId} - received message from embedded webext ${msg}\n`);
console.log(`===${addonId} - received message from embedded webext ${msg}\n`);
});
*/
}).catch(err => {
Components.utils.reportError(`${addonId} - embedded webext startup failed: ${err.message} ${err.stack}\n`);
console.log(`===${addonId} - embedded webext startup failed: ${err.message} ${err.stack}\n`);
});
});
}
我做错了什么?
答案 0 :(得分:1)
使用browser.runtime.onConnect
等待旧版加载项中的传入端口,并从嵌入式WebExtension中调用browser.runtime.connect()
以打开port。您应该在connect
调用中设置一个名称,以便onConnect
侦听器仅使用专门用于迁移过程的端口。