Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does tab.url from the callback of "chrome.tabs.onUpdated.addListener()" return undefined?

For my Chrome extension, I need to show the pageAction only on a certain website. I used to do this with declarativeContent, but it isn't supported on Firefox, so I have to do it the manual way. The answers to this question suggested that you could simply use something like this:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (changeInfo.status === "complete" && tab.url) {
    if (tab.url.match(/google.com/)) {
      chrome.pageAction.show(tabId);
    }
  }
});

This didn't work for me, so I modified the code in the background script to look like this:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  console.log("Tab updated! Tab URL: " + tab.url);
});

Every time you update a tab the console just prints Tab updated! Tab URL: undefined a few times. I also tried to query the tab like the answers to this question said, which produces the same output.

The other files of the extension are:

manifest.json:

{
  "name": "Test",
  "version": "1.0",
  "description": "Test for StackOverflow",

  "permissions": ["activeTab"],

  "background": {
    "scripts": ["background.js"]
  },
  "page_action": {
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

popup.html:

<!DOCTYPE html>
<html>
  <body>
    This is a popup!
  </body>
</html>
like image 785
R. B. Avatar asked Dec 07 '25 06:12

R. B.


1 Answers

You are missing the tabs permission from your manifest.json file. With this permission the URL of the tab should also be logged to the console.

"permissions": ["activeTab", "tabs"] // <-- This

manifest.json:

{
  "name": "Test",
  "version": "1.0",
  "description": "Test for StackOverflow",

  "permissions": ["activeTab", "tabs"],

  "background": {
    "scripts": ["background.js"]
  },
  "page_action": {
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}
like image 125
lejlun Avatar answered Dec 08 '25 19:12

lejlun