简体   繁体   English

Chrome扩展程序-onMessageExternal未定义

[英]Chrome Extension - onMessageExternal undefined

Im trying to build an extension that interacts with an internal app, and I'm looking at the documentation for the external messaging stuff, but whenever I try and access chrome.runtime.onMessageExternal.addListener , I get an error saying chrome.runtime.onMessageExternal is undefined. 我正在尝试构建与内部应用程序交互的扩展,并且正在查看有关外部消息传递内容的文档 ,但是每当尝试访问chrome.runtime.onMessageExternal.addListener ,我都会收到一条错误消息: chrome.runtime.onMessageExternal未定义。 Same thing for onConnectExternal onConnectExternal

manifest,json 舱单,JSON

{
    "manifest_version": 2,

    "name":           "My Extension",
    "description":    "My Desc",
    "version":        "0.2",

    "externally_connectable": {
        "matches": [
            "http://*.mysite.com/*"
        ]
    },

    "author": "Aaron Scherer",

    "content_scripts": [
        {
            "matches": [
                "http://*.mysite.com/*"
            ],
            "js":      ["js/script.js"]
        }
    ]
}

js/script.js JS /的script.js

chrome.runtime.onMessageExternal.addListener(
    function( request, sender, sendResponse ) {
        debugger;
        console.debug( request  );
        console.debug( sender );
        console.debug( sendResponse  );
} );

website's js 网站的js

chrome.runtime.sendMessage( 'myextid', { test: 'test' } );

chrome.runtime.onMessageExternal is only available in the background page, not in a content script. chrome.runtime.onMessageExternal仅在后台页面中可用,而在内容脚本中不可用。 If you want to use this api to talk to content scripts you could relay the messages through the background page. 如果您想使用此api与内容脚本对话,则可以通过后台页面中继消息。

Another alternative is to use the standard window.postMessage . 另一种选择是使用标准window.postMessage If you set a listener for 'message' with window.addEventListener in your content script you can receive messages sent from the page, and vice versa. 如果在内容脚本中使用window.addEventListener设置'message'的侦听器,则您可以接收从页面发送的消息,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM