最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome-extension "Cannot read property 'onUpdated' of undefined" - Stack Overflow

programmeradmin1浏览0评论

I have a script which I want to inject to a page every 10 seconds. I use this code:

chrome.tabs.onUpdated.addListener(function(tabId,tab){
    chrome.tabs.executeScript(null,{file:"program.js"});
  }
);

program.js looks like this:

$("body").prepend("Hello World!");  
setTimeout(function(){location.reload();},10000);

Meaning every 10 seconds the page should refresh itself and inject this piece of code, it works ok, but sometimes (especially when running more than one tab) I get this error:

Uncaught TypeError: Cannot read property 'onUpdated' of undefined 

I'm guessing its not getting the tabId or something.
But I can't solve it.

Help would be very much appreciated.

I have a script which I want to inject to a page every 10 seconds. I use this code:

chrome.tabs.onUpdated.addListener(function(tabId,tab){
    chrome.tabs.executeScript(null,{file:"program.js"});
  }
);

program.js looks like this:

$("body").prepend("Hello World!");  
setTimeout(function(){location.reload();},10000);

Meaning every 10 seconds the page should refresh itself and inject this piece of code, it works ok, but sometimes (especially when running more than one tab) I get this error:

Uncaught TypeError: Cannot read property 'onUpdated' of undefined 

I'm guessing its not getting the tabId or something.
But I can't solve it.

Help would be very much appreciated.

Share Improve this question edited Jul 10, 2014 at 9:44 Nir Tzezana asked Jul 10, 2014 at 9:39 Nir TzezanaNir Tzezana 2,3423 gold badges34 silver badges61 bronze badges 8
  • Why null as first arguement? If you don't want it you can skip it, its optional I think. – j809 Commented Jul 10, 2014 at 9:44
  • Thanks, I did so but still, same error. – Nir Tzezana Commented Jul 10, 2014 at 9:47
  • Where are you writing this code? In a background script or content script? – j809 Commented Jul 10, 2014 at 9:48
  • Background script (background.js) – Nir Tzezana Commented Jul 10, 2014 at 9:51
  • 3 I've seen this bug many times, but can't reproduce it so I never report it. I've only ever seen it in development and unpacked. Especially when I'm using the reload button a lot. – Scott Commented Jul 12, 2014 at 23:23
 |  Show 3 more ments

1 Answer 1

Reset to default 2

I came across the same problem only with chrome.runtime.onMessageExternal.addListener and chrome would plain about onMessageExternal. Like @scott-f pointed out the reason for this bug is 'abusing' th reload button. I removed and reinstalled the extension and it worked.

In hindsight I would suggest you use the --load-extensions='extension/path' from the cli to test your extension with the mouse just to avoid the overhead of navigating around chrome's menus, and use Selenium to automate your tests.

发布评论

评论列表(0)

  1. 暂无评论