Background script chrome.tabs is undefined in chrome extension?

Viewed 1528

I tried this:

background.js

chrome.browserAction.onClicked.addListener(function (activeTab) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.farewell);
      });
    });
});

foo.js:

chrome.runtime.onMessage.addListener(

  function (request, sender, sendResponse) {
    new contentFunction()

    console.log(sender.tab ?
      "from a content script:" + sender.tab.url :
      "from the extension");
    if (request.greeting == "hello") {
      sendResponse({ farewell: "goodbye" });
    }
  });

manifest:

{
  "manifest_version": 2,
  "name": "foo",
  "browser_action": {

  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "activeTab",
    "tabs",
    "<all_urls>"

  ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["foo.js"]
    }
  ]
}

enter image description here

0 Answers
Related