chrome.runtime.onMessage在后台脚本中未定义(扩展名为chrome) [英] chrome.runtime.onMessage undefined in background script (chrome extension)

查看:1531
本文介绍了chrome.runtime.onMessage在后台脚本中未定义(扩展名为chrome)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个chrome扩展,但由于某些原因,有时chrome.runtime对象看起来不完整,并且缺少很多方法(包括onMessage,这是我想要的)。



有时它似乎起作用,有时不起作用。我认为它可能是一个时间相关的问题,但我不明白为什么我不能简单地在后台创建一个消息监听器?



我的后台脚本:

p>

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

其中chrome.runtime.onMessage未定义。

谢谢!

Edit2:我构建了一个更简单的原型,并且它再次失败。现在我真的很困惑。这是我的:

  $ tree 

├──manifest.json
├──src
│├──background.html
│├──background.js
│└──测试。 js
└──供应商
└──jquery.js

2个目录,5个文件

manifest.json文件:

  {
manifest_version:2 ,

name:test,
description:test,
version:1.0,
author:测试,

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
},

权限:[
标签,
https://www.google*
]
}

background.html文件:

 < script src = ../供应商/的jquery.js >< /脚本> 
< script src =background.js>< / script>

background.js文件:

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

run();

test.js文件:

 'use strict'; 

run();

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

//错误如果没有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);
});
}

我在OSX上使用Chrome 49.0.2623.112(64位)。



编辑:下面是屏幕截图:



我想再次精确地说,它不会一直失败(可能是50%的时间?),这使得它更加怪异我相信在某种我不知道的地方存在一种种族状况。

解决方案

解决方案

问题解决了:我不得不禁用,然后重新启用我的所有其他分机。看起来像来自Chrome的错误。


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:

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.

解决方案

Problem solved: I had to disable, then re-enable all my other extensions. Seems like a bug from Chrome.

这篇关于chrome.runtime.onMessage在后台脚本中未定义(扩展名为chrome)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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