从内容脚本向后台脚本发送消息会中断Chrome扩展 [英] Sending message from content script to background script breaks chrome extension

查看:64
本文介绍了从内容脚本向后台脚本发送消息会中断Chrome扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过chrome扩展程序将消息从内容脚本发送到后台脚本,从而触发丰富的通知打开.我已经可以实现这一点,但是它破坏了我扩展的其余部分.

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.extension.sendMessage的调用,我在其中加载了扩展代码.一切正常,直到我添加了通知代码,我决定使用chrome Rich Notifications API,因为我最终希望在通知中包含按钮,而且我被认为只有后台脚本才能打开富通知,因此需要消息.如果我注释掉background.js中的chrome.runtime.OnMessage.addListener函数,则我的扩展逻辑将再次正确加载,因此该调用与inject.js中的chrome.extension.sendMessage函数冲突.

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.

任何人都可以解释为什么会发生这种情况以及如何解决吗?

Can anyone explain why this happens and how to fix it?

我的代码的简化版本如下:

A simplified version of my code is as follows:

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

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

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"
}});

推荐答案

问题是由于在background.js中的侦听器未返回响应而引起的.因此,我的chrome.extension.sendMessage的功能响应从未执行过.

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更改为:

I changed my background.js to be:

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

    sendResponse();
});

这篇关于从内容脚本向后台脚本发送消息会中断Chrome扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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