简体   繁体   English

从内容脚本发送消息到后台脚本会破坏chrome扩展

[英]Sending message from content script to background script breaks chrome extension

I am trying to send a message from a content script to the background script in a chrome extension that triggers a rich notification to open. 我正在尝试将内容脚本中的消息发送到Chrome扩展程序中的后台脚本,以触发打开的丰富通知。 I can already achieve this but it breaks the rest of my extension. 我已经可以实现这一点,但它打破了我的扩展的其余部分。

In my content script I have a call to chrome.extension.sendMessage inside of which I load my extension code. 在我的内容脚本中,我调用了chrome.extension.sendMessage,其中我加载了我的扩展代码。 This was all working fine until I added my notification code, I decided to use chrome Rich Notifications API as I would like to have buttons in my notification eventually, and I am led to believe that only the background script can open the rich notification, hence the need for messages. 这一切都正常,直到我添加了我的通知代码,我决定使用chrome Rich Notifications API,因为我希望最终在我的通知中有按钮,并且我被引导相信只有后台脚本可以打开丰富的通知,因此对消息的需求。 If I comment out the chrome.runtime.OnMessage.addListener function in background.js my extension logic loads properly again, so something about that call is conflicting with the chrome.extension.sendMessage function in inject.js. 如果我在background.js中注释掉chrome.runtime.OnMessage.addListener函数,我的扩展逻辑会再次正确加载,因此有关该调用的内容与inject.js中的chrome.extension.sendMessage函数冲突。

Can anyone explain why this happens and how to fix it? 谁能解释为什么会发生这种情况以及如何解决这个问题

A simplified version of my code is as follows: 我的代码的简化版本如下:

manifest.json 的manifest.json

{
  "name": "Test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "Test
  "permissions": [
    "notifications"
  ],
  "background": {
    "persistent": false,
    "scripts": ["background.js"]
  },
  "content_scripts": [
    {
      "matches": [
        "mywebsite/*"
      ],
      "js": [
        "inject.js",
      ]
    }
  ],
  "web_accessible_resources": [
    "notificationIcon.png"
  ]
}

background.js background.js

chrome.runtime.onMessage.addListener(function(request, sender) {
    if (request.type == "notification")
      chrome.notifications.create('notification', request.options, function() { });
});

inject.js inject.js

chrome.extension.sendMessage({}, function(response) {
    //code to initialize my extension
});

//code to send message to open notification. This will eventually move into my extension logic
chrome.runtime.sendMessage({type: "notification", options: { 
    type: "basic", 
    iconUrl: chrome.extension.getURL("icon128.png"),
    title: "Test",
    message: "Test"
}});

The problem was caused because my listener in background.js was not returning a response. 问题是由于我在background.js中的监听器未返回响应而引起的。 So the function response of my chrome.extension.sendMessage was never being executed. 所以我的chrome.extension.sendMessage的函数响应从未被执行过。

I changed my background.js to be: 我改变了我的background.js:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.type == "worktimer-notification")
      chrome.notifications.create('worktimer-notification', request.options, function() { });

    sendResponse();
});

暂无
暂无

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

相关问题 将消息从后台脚本发送到内容脚本,然后在Chrome扩展程序中进行响应 - Send message from background script to content script then response in chrome extension 无法从Chrome扩展程序中的后台脚本向内容脚本发送消息 - unable to send message from background script to content script in chrome extension 将消息发送到Chrome扩展程序中的内容脚本 - Sending message to Content Script in Chrome Extension Chrome扩展程序背景和内容脚本发布消息 - Chrome extension Background and content script posting message Chrome扩展程序-如何将DOM从内容脚本发送到后台页面? - Chrome Extension - How to message DOM from content script to background page? 将消息传递给Chrome扩展程序内容脚本,而不将其指定为后台脚本 - Pass message to Chrome extension content script without specifying it as a background script chrome扩展-将消息从内容脚本发送到后台页面,然后再发送回内容脚本? - chrome extension - send message from content script to background page and back to content script? Chrome 消息扩展:从注入的脚本到后台 - Chrome message extension : From injected script to background Chrome扩展程序:从注入的脚本向后台脚本发回消息时出错 - Chrome extension : error when sending message back from injected script to background script 将消息从后台脚本发送到内容脚本,然后发送到注入的脚本 - Sending message from a background script to a content script, then to a injected script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM