如何在协作的 Firefox WebExtensions 之间共享信息
How to share information between cooperating Firefox WebExtensions
我想为 Firefox 创建一个简单的扩展。
用户将鼠标悬停在任何页面上的某个词上
从扩展中的文件中提取该词的字典定义
悬停显示
我是 Firefox 附加组件和 WebExtensions 的新手,所以我想知道的是:
我希望扩展使用的字典 文件是本地的,而不是每次都引用某个在线网站。
任何给定的用户可能对整个词典的不同部分感兴趣(它包含不同语言的条目,用户可能只想要他们自己的 1 种或 2 种语言)所以我想避免强迫每个用户下载整个字典库.
我见过类似的附加组件处理 Firefox 5 之前的方式。* 是它们提供搜索和显示附加组件,与字典文件分开,每个字典文件都可以作为自己的附加组件使用是的,只有安装了 master 附加组件才能实际执行操作。
但是,这些示例中的 none 似乎已针对 WebExtensions API 进行了更新,并且不支持更新版本的 Firefox。
到目前为止,我也一直无法找到如何在 Web 扩展之间进行通信。
我的问题是,我如何才能在 2 个或多个合作扩展程序之间共享信息 以实现我所描述的内容。
实际上,如果由于某种我不知道的原因这看起来真的很愚蠢,请指出任何更合理的替代方案,让我 与主扩展分开处理字典文件 .
我发现的可能相关的问题:
Communicating between 2 Firefox Add-Ons (Cross-Extension Communication)
然而,这是 2010 年的,因此据我所知已经过时了。
有点类似的情况,但他们想从在线资源而不是本地资源中提取定义。
我在 Mozilla 浏览器扩展网站上发现的最接近的东西是在附加组件和一些本机应用程序之间进行通信,这不是我认为的需要。
加载项之间的通信是 runtime.sendMessage()
, runtime.connect()
, runtime.onMessage
, and runtime.onConnect
.
功能的正常组成部分
runtime.sendMessage()
和 runtime.connect()
都有一个可选的第一个参数:
extensionId
对于runtime.sendMessage()
,这是:
string
. The ID of the extension to send the message to. Include this to send the message to a different extension. If the intended recipient has set an ID explicitly using the applications
key in manifest.json, then extensionId should have that value. Otherwise it should have the ID that was generated for the intended recipient.
If extensionId is omitted, the message will be sent to your own extension.
对于runtime.connect()
,这是:
string
. The ID of the extension to connect to. If the target has set an ID explicitly using the applications
key in manifest.json, then extensionId
should have that value. Otherwise it should be have the ID that was generated for the target.
runtime.onMessage
和 runtime.onConnect
都提供了一个 sender
属性 或参数,或者与消息一起,或者作为端口的一部分。此 parameter/property 是一个 runtime.MessageSender
,其中包含一个 id
属性,即:
id
string
. The ID of the extension that sent the message, if the message was sent by an extension. If the sender set an ID explicitly using the applications
key in manifest.json, then id
will have this value. Otherwise it will have the ID that was generated for the sender.
Note that in Firefox, before version 54, this value was the extension's internal ID (that is, the UUID that appears in the extension's URL).
我想为 Firefox 创建一个简单的扩展。
用户将鼠标悬停在任何页面上的某个词上
从扩展中的文件中提取该词的字典定义
悬停显示
我是 Firefox 附加组件和 WebExtensions 的新手,所以我想知道的是:
我希望扩展使用的字典 文件是本地的,而不是每次都引用某个在线网站。
任何给定的用户可能对整个词典的不同部分感兴趣(它包含不同语言的条目,用户可能只想要他们自己的 1 种或 2 种语言)所以我想避免强迫每个用户下载整个字典库.
我见过类似的附加组件处理 Firefox 5 之前的方式。* 是它们提供搜索和显示附加组件,与字典文件分开,每个字典文件都可以作为自己的附加组件使用是的,只有安装了 master 附加组件才能实际执行操作。
但是,这些示例中的 none 似乎已针对 WebExtensions API 进行了更新,并且不支持更新版本的 Firefox。 到目前为止,我也一直无法找到如何在 Web 扩展之间进行通信。
我的问题是,我如何才能在 2 个或多个合作扩展程序之间共享信息 以实现我所描述的内容。 实际上,如果由于某种我不知道的原因这看起来真的很愚蠢,请指出任何更合理的替代方案,让我 与主扩展分开处理字典文件 .
我发现的可能相关的问题: Communicating between 2 Firefox Add-Ons (Cross-Extension Communication) 然而,这是 2010 年的,因此据我所知已经过时了。
我在 Mozilla 浏览器扩展网站上发现的最接近的东西是在附加组件和一些本机应用程序之间进行通信,这不是我认为的需要。
加载项之间的通信是 runtime.sendMessage()
, runtime.connect()
, runtime.onMessage
, and runtime.onConnect
.
runtime.sendMessage()
和 runtime.connect()
都有一个可选的第一个参数:
extensionId
对于runtime.sendMessage()
,这是:
string
. The ID of the extension to send the message to. Include this to send the message to a different extension. If the intended recipient has set an ID explicitly using theapplications
key in manifest.json, then extensionId should have that value. Otherwise it should have the ID that was generated for the intended recipient.If extensionId is omitted, the message will be sent to your own extension.
对于runtime.connect()
,这是:
string
. The ID of the extension to connect to. If the target has set an ID explicitly using theapplications
key in manifest.json, thenextensionId
should have that value. Otherwise it should be have the ID that was generated for the target.
runtime.onMessage
和 runtime.onConnect
都提供了一个 sender
属性 或参数,或者与消息一起,或者作为端口的一部分。此 parameter/property 是一个 runtime.MessageSender
,其中包含一个 id
属性,即:
id
string
. The ID of the extension that sent the message, if the message was sent by an extension. If the sender set an ID explicitly using theapplications
key in manifest.json, thenid
will have this value. Otherwise it will have the ID that was generated for the sender.Note that in Firefox, before version 54, this value was the extension's internal ID (that is, the UUID that appears in the extension's URL).