5

Problem: Uncaught TypeError: Cannot read property 'onUpdated' of undefined

Google Chrome extension

My code:

main.js

I have a function getCookie and setcookie

var _a = getCookie("a");

if (_a != "") {
/// do something
} else {


chrome.tabs.onUpdated.addListener(function(tabId , info , tab) {
if (info.status == "complete") {

   var _a = document.getElementsByName('id_loaded_page')[0].value;
       setCookie("_a", value, 1);
       console.log("_a: " +_a);


   }
});

}
user3608126
  • 89
  • 2
  • 5
  • 1
    There is no question here to begin with; and on Stack Overflow questions of the format "here's my code (code dump) it doesn't work, help" are **not welcome**. Edit your post to contain a single, answerable question, and add a [minimal example](http://stackoverflow.com/help/mcve) with only relevant code. Also read [How to Ask](http://stackoverflow.com/help/how-to-ask) guide. – Xan Jun 03 '14 at 20:57

1 Answers1

10

You are calling chrome.tabs from a Content Script.

By design, content scripts are not allowed access to most of the Chrome APIs.

You need to make a background page to access chrome.tabs, but in your particular case you don't even need that wrapper: you're injecting at "document_end", which should mean that all static DOM is already loaded

And if the DOM node you are looking for is dynamically added, it may not exist when "complete" fires for a tab. You will need to listen to DOM mutations.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206