chrome.extension有时在后台js中未定义 [英] chrome.extension sometimes undefined in background js

查看:100
本文介绍了chrome.extension有时在后台js中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将失败的代码缩小到了一个更小的情况。

请参阅< a href =https://github.com/deg/tmp-chrome-extension-bug =nofollow> https://github.com/deg/tmp-chrome-extension-bug



这是 https://稍作修改的副本developer.chrome.com/extensions/getstarted ,更改为包含内容脚本和背景页面。



即使在这个简单的扩展中,chrome.runtime.id有时是不确定的。它看起来像是一个时间问题。



重现的说明是repo readme。



[ORIGINAL TEXT]



我有一个带后台脚本background.js的chrome扩展程序。

在这个文件中, ,我有chrome.runtime.onMessage.addListener(...);

我通过在chrome:// extensions中对扩展进行重新加载来进行测试。这通常有效,但有时chrome.runtime.onMessage是未定义的。



在调试中,我发现,只要发生这种情况,chrome.extension和chrome.runtime。 ID也是未定义的。



我可以通过反复点击重新加载按钮使问题消失,直到它正确加载。



我最近首先注意到这个问题,所以认为这是我打破的。
但是,经过一会儿的git bisect和拉发,我发现问题甚至出现在我的最早版本中;只是出现次数少得多。



看起来像是在启动时有一些计时问题 - 也许有些东西在我的代码加载之后才会被初始化。所以,随着我的代码变长,我更经常看到这个问题。 (只是猜测)

在后台执行chrome.runtime.onMessage.addListener的正确方法是什么?它是否需要包裹在一个onSomething?我见过的例子也是在顶层做的。困惑!

解决方案

看起来像这个问题是由于我在我的机器上加载的另一个扩展与不良交互造成的;可能是赛门铁克的诺顿安全工具栏。有关 https://code.google.com/\">平行讨论中的更多详细信息p / chromium / issues / detail?id = 565384


[EDIT]

I have reduced my failing code to a much smaller case.

See https://github.com/deg/tmp-chrome-extension-bug

This is a slightly modified copy of https://developer.chrome.com/extensions/getstarted, changed to include a content script and background page.

Even in this simple extension, chrome.runtime.id is sometimes undefined. It looks like a timing problem.

Instructions to reproduce are the repo readme.

[ORIGINAL TEXT]

I have a chrome extension with a background script "background.js"

In this file, at top level, I have chrome.runtime.onMessage.addListener(...);

I test by doing reload on the extension in chrome://extensions. This usually works, but sometimes chrome.runtime.onMessage is undefined.

In debugging, I've found that, whenever this happens, chrome.extension and chrome.runtime.id are also undefined.

I can make the problem "go away" by repeatedly hitting the reload button until it loads correctly.

I first noticed this problem recently, so assumed it was something I had broken. But, after a session of git bisect and hair-pulling, I see that the problem was in even my earliest versions; just occurring much less frequently.

It looks like there is some timing problem at startup -- maybe something is not initialized until after my code has loaded. So, as my code gets longer, I'm seeing the problem more often. (Just a guess)

What is the correct way to do chrome.runtime.onMessage.addListener in the background. Does it need to be wrapped inside an onSomething? Examples I've seen also do it at top-level. Confused!

解决方案

It looks like this problem is caused by a bad interaction with another extension I had loaded on my machine; probably Symantec's "Norton Security Toolbar". More details in the parallel discussion at https://code.google.com/p/chromium/issues/detail?id=565384

这篇关于chrome.extension有时在后台js中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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