广播消息 (ping) 交叉颜色扩展

Broadcast message (ping) cross chrome extensions

本文关键字:颜色 扩展 消息 ping 广播      更新时间:2023-09-26

我正在尝试找出一种方法,允许我的扩展向浏览器中安装的所有扩展发送ping。

想法如下,我有 3 个单独的扩展。一个充当中间件,并尝试向浏览器上所有其他令人兴奋的扩展发送广播消息。这两个扩展在后台包含一个事件侦听器,它们将在其中回复其 Id。因此,中间件后台将能够与这些扩展建立连接。

您不能以这种方式广播消息,但有一种解决方法。

您可以请求"management"权限并获取已安装的所有扩展的列表,并逐个发送消息。

function broadcastExternal(message, callback) {
  // Get all installed extensions an apps
  chrome.management.getAll(function(extInfos) {
    // Cycle through them
    extInfos.forEach(function(extInfo) {
      // Use `connect` if needed
      chrome.runtime.sendMessage(extInfo.id, message, function(response) {
        var result = { id: extInfo.id, message: message };
        // Check for errors
        if(chrome.runtime.lastError) {
          result.error = chrome.runtime.lastError;
        } else {
          result.response = response;
        }
        // Report either a response or an error
        callback(result);
      });
    });
  });
}

如果不希望提升的权限导致权限警告,则必须维护希望侦听的所有 ID 的列表并循环访问它。您可能会将该列表存储在网络上的某个位置,并不时查询它,而不是更新扩展名。