chrome.runtime.onMessage undefined in background script (chrome extension)

时间:2016-04-15 11:13:17

标签: javascript google-chrome google-chrome-extension background

I am trying to create a chrome extension, but for some reasons, sometimes, the chrome.runtime object seems incomplete, and a lot of methods are missing (including onMessage, which is the one I want).

It seems sometimes it works, sometimes not. I assumed it may be a time related issue, but I don't understand why I can't simply create a message listener on background?

My background script:

setTimeout(function () {
    console.log("gogo!");
    chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            console.log(sender.tab ?
                        "from a content script:" + sender.tab.url :
                        "from the extension");
            if (request.type == "tab") {
                console.log("tab!");
                sendResponse({status: "ok"});
            }
        }),
    2
});

Where "chrome.runtime.onMessage" is undefined.

Thanks!

Edit2: I have built a much simpler prototype, and it's failing again. Now I am really confused. Here is what I have:

$tree
.
├── manifest.json
├── src
│   ├── background.html
│   ├── background.js
│   └── test.js
└── vendor
    └── jquery.js

2 directories, 5 files

manifest.json file:

{
    "manifest_version": 2,

    "name": "test",
    "description": "test",
    "version": "1.0",
    "author": "test",

    "homepage_url": "http://www.test.com",

    "content_scripts": [
        {
            "run_at" : "document_idle",
            "matches": ["https://www.google*"],
            "js": ["vendor/jquery.js", "src/test.js"]
        }
    ],

    "background": {
        "page": "src/background.html",
        "persistent": false
    },

    "permissions": [
        "tabs",
        "https://www.google*"
    ]
}

background.html file:

<script src="../vendor/jquery.js"></script>
<script src="background.js"></script>

background.js file:

function run () {
    console.log("gogo!");
    chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            console.log(sender.tab ?
                        "from a content script:" + sender.tab.url :
                        "from the extension");
            if (request.type == "tab") {
                console.log("tab!");
                sendResponse({status: "ok"});
            }
        });
}

run();

test.js file:

'use strict';

run();

function run() {
    var url = window.location.href;

    // Error if no URI
    if (!url) {
        return 1;
    }

    var uriRe = /https:\/\/www\.google.*/;
    var reParse = uriRe.exec(url);
    if (!reParse) {
        return 2;
    }

    chrome.runtime.sendMessage({type: "tab"}, function(response) {
        console.log(response);
    });
}

I am using Chrome 49.0.2623.112 (64-bit) on OSX.

Edit: Here is a screenshot of what happens the times it fails:

Failure screenshot

I want to precise again that it doesn't fail all the time (maybe 50% of the time?), which makes it even more weird and makes me believe there is a kind of "race" condition somewhere I am not aware of.

3 个答案:

答案 0 :(得分:3)

问题解决了:我必须禁用,然后重新启用所有其他扩展程序。看起来像Chrome中的一个错误。

答案 1 :(得分:0)

单击顶部并在扩展程序环境中设置控制台

enter image description here

背景只运行一次。你应该运行更多来检查resutl

这是我的代码,它很好:

chrome.tabs.onUpdated.addListener(
	function(tabId,info,tab){
		if(info.status=="complete")
			console.log("complete")
		chrome.tabs.sendMessage(tabId, {greeting: "inupdate"}, function(response) {
			console.log(response);
		});
	}
)

答案 2 :(得分:-1)

我认为它与你正在使用的超时功能有关,谷歌已经有一些叫做警报的东西,有助于安排代码定期或在指定的时间运行。

  

https://developer.chrome.com/apps/alarms

尝试而不是超时