• chrome extension 普通网页与插件直接通信


    跨扩展消息传递类似,您的扩展可以接收和响应来自常规网页的消息。要使用此功能,您必须首先在 manifest.json 中指定要与哪些网站进行通信。例如:

    1. "externally_connectable": {
    2. "matches": ["https://*.example.com/*"]
    3. }

    这会将消息传递 API 公开给与您指定的 URL 模式匹配的任何页面。URL 模式必须至少包含一个二级域- 即,禁止使用“*”、“*.com”、“*.co.uk”和“*.appspot.com”等主机名模式。在网页中,使用runtime.sendMessageruntime.connect API 将消息发送到特定应用程序或扩展程序。例如:

    1. // The ID of the extension we want to talk to.
    2. var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
    3. // Make a simple request:
    4. chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
    5. function(response) {
    6. if (!response.success)
    7. handleError(url);
    8. });

    在您的扩展中,您可以通过runtime.onMessageExternalruntime.onConnectExternal API收听来自网页的消息,类似于跨扩展消息传递。只有网页可以发起连接。这是一个例子:

    1. chrome.runtime.onMessageExternal.addListener(
    2. function(request, sender, sendResponse) {
    3. if (sender.url === blocklistedWebsite)
    4. return; // don't allow this web page access
    5. if (request.openUrlInEditor)
    6. openUrl(request.openUrlInEditor);
    7. });

    #externally_connectable 也是扩展与扩展之间通信的配置key

    1. "externally_connectable": {
    2. // Extension and app IDs. If this field is not specified, no
    3. // extensions or apps can connect.
    4. "ids": [
    5. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    6. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
    7. ...
    8. // Alternatively, to match all extensions and apps, specify only
    9. // "*".
    10. "*"
    11. ],
    12. // Match patterns for web pages. Does not affect content scripts.
    13. // If this field is not specified, no webpages can connect.
    14. "matches": [
    15. "https://*.google.com/*",
    16. "*://*.chromium.org/*",
    17. ...
    18. ],
    19. // Indicates that the extension would like to make use of the TLS
    20. // channel ID of the web page connecting to it. The web page must
    21. // also opt to send the TLS channel ID to the extension via setting
    22. // includeTlsChannelId to true in runtime.connect's connectInfo
    23. // or runtime.sendMessage's options.
    24. "accepts_tls_channel_id": false
    25. }

    externally_connectable 清单键可以具有以下属性:

    • ids(字符串数组) - 可选

      1. 允许连接的扩展程序或应用程序的 ID。如果留空或未指定,则无法连接任何扩展程序或应用程序

      2. 通配符"*"将允许所有扩展程序和应用程序连接

    • matches(字符串数组) - 可选

      1. 允许连接的网页的 URL 模式。这不会影响内容脚本。如果留空或未指定,则无法连接任何网页。

      2.  模式不能包括通配符域或(有效)顶级域的子域;*://google.com/*andhttp://*.chromium.org/*是有效的,而, http://*/*, *://*.com/*, 甚至http://*.appspot.com/*不是。

    • accepts_tls_channel_id(布尔) - 可选

      1. 如果为true,消息通过runtime.connect或runtime.sendMessagetrue发送的消息将设置runtime.MessageSender.tlsChannelId

      2. 如果为false,则在任何情况下都不会设置runtime.MessageSender.tlsChannelId

    注意点

    1. 同步接收信息时,使用async/await+promise的方式
    2. 当检测到匹配的URL时,浏览器会自动地将chrome.runtime注入到该page页面的全局变量中,所以可以通过chrome.runtime?的方式来检测是否安装了该插件,当然这不足以确定是否是自己的插件,最好还是与插件通信返回后,获取到确认信息,才能确定自己的插件是处于激活状态
  • 相关阅读:
    Windows操作系统搭建Lsky Pro
    OpenHarmony
    毕业设计:基于STM32与机智云平台的远程控制智能家居系统
    Go并发编程和调度器
    Java分布式ID
    基于element-plus定义表格行内编辑配置化
    WEB代码审计
    反射型XSS靶场练习
    Ubuntu平铺左、右、上、下、1/2、1/4窗口(脚本)
    USB 协议 (四) USB HOST 侧 的概念详解
  • 原文地址:https://blog.csdn.net/wangsenling/article/details/127837560