I have a Chrome extension where I need the click of the extension button to call my main context so it can then access gmail.js. My background.js is:
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.browserAction.onClicked.addListener(function(tab) {
      chrome.tabs.sendMessage(tab.id, {greeting: "hello"});
  });
});
My main.js is:
var gmail;
function refresh(f) {
  if ((/in/.test(document.readyState)) || (typeof Gmail === undefined)) {
    setTimeout('refresh(' + f + ')', 10);
  } else {
    f();
  }
}
var main = function() {
  gmail = new Gmail();  
  chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log(sender.tab ?  "from a content script:" + sender.tab.url : "from the extension");
  });
}
refresh(main);
My content.js is:
var j = document.createElement('script');
j.src = chrome.extension.getURL('jquery-1.10.2.min.js');
(document.head || document.documentElement).appendChild(j);
var g = document.createElement('script');
g.src = chrome.extension.getURL('gmail.js');
(document.head || document.documentElement).appendChild(g);
var s = document.createElement('script');
s.src = chrome.extension.getURL('main.js');
(document.head || document.documentElement).appendChild(s);
My manifest.json is:
{
  "name": "Test",
  "version": "1.0",
  "description": "Test Gmail chrome extension",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Export current Gmail message",
    "default_icon": "email_16x16.png"
  },
  "content_scripts": [
    {
      "matches": ["https://mail.google.com/*"],
      "js": ["content.js"]
    }
  ],
  "web_accessible_resources": [
    "jquery-1.10.2.min.js",
    "background.js",
    "gmail.js",
    "main.js"
  ],
  "manifest_version": 2
}
When the extension loads I get this error:
Uncaught TypeError: Cannot read property 'addListener' of undefined
at <anonymous>:9:27
at refresh (chrome-extension://lnndcckhmkllogdhmmlhfdoehbfgipdj/main.js:14)
at <anonymous>:1:1
Thanks for any tips or pointers.
 
     
    