Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome exstension chrome.tabs.onUpdated.addListener

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);


   }
});

}
like image 674
user3608126 Avatar asked Feb 12 '23 19:02

user3608126


1 Answers

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.

like image 81
Xan Avatar answered Feb 15 '23 10:02

Xan