我如何利用port.postmessage从背景页面发送信息的内容脚本在谷歌Chrome扩展 [英] how can i use the port.postmessage to send info from the background page to the content script in a google chrome extension

查看:455
本文介绍了我如何利用port.postmessage从背景页面发送信息的内容脚本在谷歌Chrome扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够从背景页面发送数据到内容脚本。但是这是通过使用 sendRequest将()。我将需要发送的数据来回,所以我试图找出正确的语法使用 port.postmessage 从背景页内容的脚本。我已经看过好几次,在谷歌的网页。消息并我不似乎得到它。我甚至直接从网页复制的code和没有结果的测试。所有我想要的,现在要做的就是利用后台页面发送数据到内容脚本连接,而不是 sendRequest将 。从内容脚本,我将处理后为code。与这种反应一直是主要的刺的响应。我只是想了解在一个时间过程一步到位,不发送回一个响应的额外知识。

i've been able to send data from the background page to the content script. but this is done using sendrequest(). I will need to send data back and forth so I'm trying to figure out the correct syntax for using the port.postmessage from background page to content script. I have already read, several times, the google page on Messaging and i dont seem to get it. I even copied the code directly from the page and tested with no result. all i'm trying to do for now is send data from background page to content script using connect as opposed to sendrequest. The response from the content script i will deal with later as code with this response has been the main thorn. I just want to understand the process one step at a time without the extra knowledge of sending a response back.

我不知道这是否违反本板的规则,但有人可以请给我一些code的例子来做到这一点(背景页面和内容脚本摘录,背景页是发送者)。

I'm not sure if this contravenes the rules of this board but can someone PLEASE give me an example of some code to do this (background page and content script excerpt, the background page is the sender).

我已经要求协助多次在这个网站只被告知要阅读文档,或检查出我已经访问过的网站。

I've asked for assistance several times on this site only to be told to read the documentation or check out sites I've already visited.

任何人能帮助吗?

推荐答案

如果你只是想从外延打开一个端口到内容脚本的任何例如,这里是我能想到的最简单的。背景只是打开一个端口,并发送你好标签!在端口和内容脚本将消息发送到后台任何时候你点击该网页。

If you just want any example of opening a port from the extension to a content script, here's the simplest I can think of. The background just opens a port and sends "Hello tab!" over the port, and the content script sends a message to the background any time you click on the webpage.

我觉得这是pretty简单,所以我不知道你为什么如此强调。只要确保当背景试图(通过等待,直到完成事件中,我这样做)连接内容标签已在侦听。

I think this is pretty simple, so I don't know why you are so stressed. Just make sure that the content tab is already listening when the background tries to connect (I do this by waiting until the "complete" event).

manifest.json的:

manifest.json:

{
  "name": "TestExt",
  "version": "0.1",
  "background_page": "background.html",
  "content_scripts": [{
    "matches": ["http://localhost/*"],  // same as background.html regexp
    "js": ["injected.js"]
  }],
  "permissions": [
    "tabs"  // ability to inject js and listen to onUpdated
  ]
}

background.html:

background.html:

<script>
var interestingTabs = {};
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  // same as manifest.json wildcard
  if (changeInfo.url && /http:\/\/localhost(:\d+)?\/(.|$)/.test(changeInfo.url)) {
    interestingTabs[tabId] = true;
  }
  if (changeInfo.status === 'complete' && interestingTabs[tabId]) {
    delete interestingTabs[tabId];
    console.log('Trying to connect to tab ' + tabId);
    var port = chrome.tabs.connect(tabId);
    port.onMessage.addListener(function(m) {
      console.log('received message from tab ' + tabId + ':');
      console.log(m);
    });
    port.postMessage('Hello tab!');
  }
});
</script>

injection.js:

injection.js:

chrome.extension.onConnect.addListener(function(port) {
  console.log('Connected to content script!');
  port.onMessage.addListener(function(m) {
    console.log('Received message:');
    console.log(m);
  });
  document.documentElement.addEventListener('click', function(e) {
    port.postMessage('User clicked on a ' + e.target.tagName);
  }, true);
});

这篇关于我如何利用port.postmessage从背景页面发送信息的内容脚本在谷歌Chrome扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆