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
1 Answer
Reset to default 2I 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.