chrome.runtime.sendMessage 没有按预期工作 [英] chrome.runtime.sendMessage not working as expected

查看:39
本文介绍了chrome.runtime.sendMessage 没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有内容脚本和后台脚本的 Chrome 插件,并试图让两者进行交流.

I am writing a Chrome plugin with a content script and a background script, and I am trying to make the two communicate.

在我的内容脚本中,我正在做

In my content script, I am doing

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
      console.log(response.farewell);
});

在我的后台脚本中,我正在做

and in my background script, I am doing

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log(sender.tab ?
          "from a content script:" + sender.tab.url :
          "from the extension");
        if (request.greeting == "hello")
            sendResponse({farewell: "goodbye"});
    }
);

我的清单看起来像这样:

My manifest looks like this:

{
    "manifest_version": 2,
    "name": "Tesing Phase",
    "version": "1.0",
    "background": {
        "persistent": false,
        "scripts": ["bgscript.js"]
    },
    "content_scripts": [{
        "js": ["contentscript.js"],
        "all_frames": true,
        "run_at" : "document_start",
        "matches": ["*://*/*"]
    }],
    "web_accessible_resources": ["script.js"]
}

当我运行我的插件时,出现以下错误:

When I run my plugin, I get the following error:

Uncaught TypeError: Object #<Object> has no method 'sendMessage' 

我尝试记录 chrome.runtime,但没有方法 sendMessage.我在 Ubuntu 上使用 Chromium 25.0 版.我也尝试使用 sendRequest,但它说它已折旧,应该使用 sendMessage.

I tried logging chrome.runtime, and there was no method sendMessage. I am using version 25.0 of Chromium on Ubuntu. I tried using sendRequest as well, but it said it's depreciated and sendMessage should be used.

谁能指出我在这里遗漏了什么?是否需要任何权限才能使其工作?

Can anyone point me out what I am missing here? Are there any permissions needed for this to work?

推荐答案

chrome.runtime.sendMessage/onMessage(以及其他相关事件/方法,例如 connect) 是在 Chrome 26 中引入的.

chrome.runtime.sendMessage / onMessage (and other related events/methods such as connect) were introduced in Chrome 26.

如果您想编写与 Chrome 20 - 25 兼容的扩展程序,请使用 chrome.extension.sendMessage.

If you want to write an extension which is compatible with Chrome 20 - 25, use chrome.extension.sendMessage.

实现最佳兼容性的一种方法是自己定义 chrome.runtime 方法.例如,在其余代码(背景/内容脚本)之前运行以下代码:

A way to achieve optimal compatibility is to define the chrome.runtime methods yourself. For example, run the following code before the rest of your code (background/content script):

if (!chrome.runtime) {
    // Chrome 20-21
    chrome.runtime = chrome.extension;
} else if(!chrome.runtime.onMessage) {
    // Chrome 22-25
    chrome.runtime.onMessage = chrome.extension.onMessage;
    chrome.runtime.sendMessage = chrome.extension.sendMessage;
    chrome.runtime.onConnect = chrome.extension.onConnect;
    chrome.runtime.connect = chrome.extension.connect;
}

然后就可以使用最新的API格式了:

Then you can just use the latest API format:

// Bind event:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    // Do something
});

// Send message:
chrome.runtime.sendMessage({greeting: 'hello'});

如果您对修改 chrome.runtime 对象上的方法感到不舒服,您可以使用以下方法代替:

If you feel uncomfortable with modifying methods on the chrome.runtime object, you can use the following approach instead:

var runtimeOrExtension = chrome.runtime && chrome.runtime.sendMessage ?
                         'runtime' : 'extension';

// Bind event:
chrome[runtimeOrExtension].onMessage.addListener(
  function(message, sender, sendResponse) {
    // Do something
});

// Send message:
chrome[runtimeOrExtension].sendMessage({greeting: 'hello'});

这篇关于chrome.runtime.sendMessage 没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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