0

Hi I am trying to execute a function in background.js from popup.js but chrome.extension.getBackgroundPage(); always returns null :

popup.js

  var bkg = chrome.extension.getBackgroundPage();
  bkg.test(function(result) {
    alert(result);
  });

background.js

  function test(func) {
      alert('test');
    func.apply(this, ['bar']);
  }

and my manifest :

{
  "name": "Get pages source",
    "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": ["tabs", "<all_urls>",
    "downloads","http://*/*", "https://*/*","background"
  ]

}

someone knows why?

Rex
  • 50
  • 1
  • 7
  • `alert` may be blocked in the background page. Anyway, start by checking the popup's console for errors: right-click the popup and click Inspect. Then check the [background console](https://stackoverflow.com/a/10258029). Also, if you're trying it in Firefox, one of the recent versions has buggy getBackgroundPage which returns null. – wOxxOm Nov 23 '17 at 03:32
  • I am doing this on chrome and in the console it says : `Cannot read property 'test' of null` – Rex Nov 23 '17 at 03:35
  • Well, the posted code works so there's something else you're not showing us. Or maybe a bug in your browser. – wOxxOm Nov 23 '17 at 03:42
  • Yep just put all my code in `/**/` exept for that and it worked then removed the comments and it still worked... – Rex Nov 23 '17 at 03:52

0 Answers0