简体   繁体   English

为什么我的 content.js 脚本不能与我的 background.js 脚本(chrome 扩展)通信?

[英]Why won't my content.js script communicate with my background.js script (chrome extensions)?

I am trying to build a basic extension in google chrome which includes a content and background script.我正在尝试在 google chrome 中构建一个基本扩展,其中包括一个内容和背景脚本。 For some reason, when a new tab is created and i try to send a message from my background script to the content script, the event listener in content.js is not receiving the message.出于某种原因,当创建一个新选项卡并且我尝试从我的后台脚本向内容脚本发送消息时, content.js的事件侦听器未收到该消息。 I can't see any console.log in the new tabs dev tools.我在新标签开发工具中看不到任何console.log Can anyone explain where i am going wrong?谁能解释我哪里出错了?

content.js:内容.js:

//listen to background.js
/* global chrome */


chrome.runtime.onMessage.addListener(request => {
    console.log("message received")
    const el = document.createElement('injected');
    el.id = "injected-element"
    el.innerHTML = `<div> this has been injected </div>`
    document.body.appendChild(el)
    sendResponse({ "message":"sent from content script" });
  }
);

document.addEventListener('click', () => {
  this.setState({ count: this.state.count +1 });
});

background.js背景.js

chrome.runtime.onInstalled.addListener(() => {
  console.log("Extension installed successfully")
});

chrome.tabs.onCreated.addListener(tab => {
  chrome.tabs.sendMessage(
    tab.id,
    {"message":"hello from background script"}
  );
});

manifest.json清单文件

{
  "manifest_version": 2,
  "name": "Article Scorer",
  "author": "Sean Barker",
  "version": "1.0.1",
  "icons": {
    "192": "logo192.png",
    "512": "logo512.png"
  },
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "permissions": ["activeTab", "tabs", "contextMenus"],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ]
}

The problem is probably because when the onCreated listener is triggered, it attempts to send the message back to the tab, but content.js has not been loaded yet since new tabs by default do not have a url, and your manifest declares that the content script will be injected when it matches "<all_urls>" .问题可能是因为当onCreated侦听器被触发时,它尝试将消息发送回选项卡,但由于默认情况下新选项卡没有 url,因此尚未加载content.js ,并且您的清单声明内容脚本将在匹配"<all_urls>"时被注入。 content.js is only loaded once the new tab is directed to some site. content.js仅在新选项卡定向到某个站点后才加载。

To see this happening you can do the following:要查看这种情况,您可以执行以下操作:

  1. Place a breakpoint in the onCreated listener in background.js .background.js中的onCreated侦听器中放置一个断点。
  2. Open a new tab (breakpoint will get hit)打开一个新选项卡(断点会被击中)
  3. In the new tab, navigate to some site在新选项卡中,导航到某个站点
  4. Open dev tools and see content.js is loaded打开开发工具并查看content.js已加载
  5. Unbreak and continue exection in background.jsbackground.js解开并继续执行
  6. You'll see "message received" printed in the console.您将在控制台中看到“收到的消息”。

In effect, your background script is sending the message properly, but nothing is listening.实际上,您的后台脚本正在正确发送消息,但没有任何内容在侦听。

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

相关问题 为什么我的popup.js无法与background.js通信? - Why can't my popup.js communicate with my background.js? Background.js找不到使用内容脚本注入的内容 - Background.js Doesn't Find Content Injected with Content Script 我正在尝试让jQuery在我的chrome扩展程序的background.js中工作,并且它将无法工作。 为什么? - I'm trying to get jquery working in the background.js of my chrome extension and it won't work. why? Chrome-扩展程序:将消息从background.js发送到content.js时出错 - Chrome - Extension: Error when sending messages from background.js to content.js 如何知道当前环境是chrome扩展中的content.js或background.js - how to know current environment is content.js or background.js in chrome extension Chrome 扩展消息传递不起作用(background.js 到 content.js) - Chrome extension message passing not working (background.js to content.js) 从background.js到content.js的chrome扩展消息,带有响应,TypeError无法读取属性 - chrome extension message from background.js to content.js with response, TypeError cannot read property Chrome 扩展程序 v3:content.js 和 background.js 不通信 - Chrome extension v3: content.js & background.js not communicating 返回值background.js到content.js - return value background.js to content.js 将变量从 Content.js 传递到 Background.js - Passing variable from Content.js to Background.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM