【问题标题】:How to communicate with chrome extension background page in electron?如何与电子中的chrome扩展背景页面通信?
【发布时间】:2022-01-21 01:31:49
【问题描述】:

由于电子没有实现chrome.runtime.onMessageExternal。 我们如何在 main 或 renderer 进程中与 chrome 扩展后台页面进行通信?

【问题讨论】:

    标签: google-chrome-extension electron


    【解决方案1】:

    其实扩展背景页是webContents之一。

    1. 使用webContents.getAllWebContents(),您可以找到所有网页内容。
    2. 并使用wc.getType() === 'backgroundPage' 过滤。
    3. 然后加载预加载文件。

    你可以在预加载文件中做任何事情。

      webContents.getAllWebContents().forEach((wc) => {
        if (wc.getType() === 'backgroundPage') {
          const preloadPath = path.join(__dirname, 'preloadForExtension.js');
          if (!wc.session.getPreloads().includes(preloadPath)) {
            wc.session.setPreloads(wc.session.getPreloads().concat(preloadPath));
          }
        }
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      相关资源
      最近更新 更多