Why is chrome.tabs undefined in the background page?

Viewed 2133

I'm trying to write a Google Chrome extension.

The documentation and even sample code say that a background page can run JavaScript on the active tab using the chrome.tabs.executeScript method, but chrome.tabs is always undefined when I break in the debugger.

This behavior is manifest in both my code and the Google sample code.

The Real Question: How do I run JavaScript on the active tab from a background page in a Chrome extension?


background.js:

chrome.browserAction.onClicked.addListener(function(tab) {
  debugger;
  // chrome.tabs is undefined here
  chrome.tabs.executeScript({
    code: "console.log('hi')"
  });
});

manifest.js:

{
  "manifest_version": 2,

  "name": "Hello World",
  "description": "Says 'hello' to the world.",
  "version": "0.1",

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

  "browser_action": {
    "default_title": "hi"
  },

  "background": {
    "scripts": ["background.js"]
  }
}

Things I've tried:

  • Setting the persistent flag on background in the manifest file to false, true, "false", and "true"
  • Including the "tabs" permission

The runtime throws this error when I try access chrome.tabs:

Lazy require of tabs.binding did not set the binding field

1 Answers
Related