简体   繁体   English

特定网页与chrome扩展之间的通信

[英]Communication between specific web page and chrome extension

I try to create a communication channel between specific webpage (per example : www.website.dev) and a chrome extension I created.我尝试在特定网页(例如:www.website.dev)和我创建的 chrome 扩展程序之间创建一个通信通道。

By using postMessage it's work from webpage to extension but I can't do that from extension to webpage.通过使用 postMessage,它可以从网页到扩展程序工作,但我不能从扩展程序到网页。

I tried Google example but it uses background page我试过谷歌例子,但它使用背景页面

Thanks for your help谢谢你的帮助

EDIT : sorry I don't understand the difference between content_script and background.js In my manifest I have content script = test.js What's about "background" ?编辑:抱歉,我不明白 content_script 和 background.js 之间的区别在我的清单中,我有 content script = test.js 什么是“背景”?

Following the documentation you can pass messages to your extension if you know the extension id: https://developer.chrome.com/extensions/messaging#external-webpage如果您知道扩展程序 ID,则可以按照文档将消息传递给您的扩展程序: https : //developer.chrome.com/extensions/messaging#external-webpage

manifest.json清单文件

"externally_connectable": {
  "matches": ["*://*.example.com/*"]
}

website:网站:

// The ID of the extension we want to talk to.
var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";

// Make a simple request:
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
  function(response) {
    if (!response.success)
      handleError(url);
  });

extension:延期:

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

You will find all the details in the documentation https://developer.chrome.com/extensions/messaging您将在文档https://developer.chrome.com/extensions/messaging 中找到所有详细信息

You would have one piece use sendMessage function while the other piece is listening for the event, which could be either a webpage or content script.您可以让一个部分使用 sendMessage 函数,而另一部分正在侦听事件,该事件可以是网页或内容脚本。

either the webpage's content script should initiate the communication (so you would get the tab id) or you can have the background query for tabs with specific url and then use sendMessage.要么网页的内容脚本应该启动通信(这样您就可以获得标签 ID),或者您可以对具有特定 url 的标签进行后台查询,然后使用 sendMessage。 Notice two separate functions chrome.extension.sendMessage and chrome.tabs.sendMessage used here.注意这里使用了两个单独的函数chrome.extension.sendMessagechrome.tabs.sendMessage

the following code works for me:以下代码对我有用:

content_script.js: content_script.js:

chrome.extension.sendMessage({"msg":"hello"});

background.js:背景.js:

chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
  if (request.msg == "hello"){
    senderTab = sender.tab.id;
    chrome.tabs.sendMessage(senderTab, {"msg": "ehlo"});
  };
})

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

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