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

时间:2022-08-26 21:33:09

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. I can already achieve this but it breaks the rest of my extension.

我正在尝试将内容脚本中的消息发送到Chrome扩展程序中的后台脚本,该扩展程序会触发打开的丰富通知。我已经可以实现这一点,但它打破了我的扩展的其余部分。

In my content script I have a call to chrome.extension.sendMessage inside of which I load my extension code. 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. 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.

在我的内容脚本中,我调用了chrome.extension.sendMessage,其中加载了我的扩展代码。这一切都正常,直到我添加了我的通知代码,我决定使用chrome Rich Notifications API,因为我希望最终在我的通知中有按钮,并且我被认为只有后台脚本可以打开丰富的通知,因此对消息的需求。如果我在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"
}});

1 个解决方案

#1


4  

The problem was caused because my listener in background.js was not returning a response. So the function response of my chrome.extension.sendMessage was never being executed.

问题是由于我在background.js中的监听器未返回响应而引起的。所以我的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();
});

#1


4  

The problem was caused because my listener in background.js was not returning a response. So the function response of my chrome.extension.sendMessage was never being executed.

问题是由于我在background.js中的监听器未返回响应而引起的。所以我的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();
});