0

I cannot figure out why I am getting returned the error Cannot read property 'query' of undefined. The error is thrown and the user is unable to click on any of the buttons.

JSON:

{  
   "name":"Jira Extension",
   "version":"1.0",
   "description":"Allow for quick access to changing SD and TA",
   "permissions":[  
      "activeTab",
      "tabs"
   ],
   "browser_action": {
      "default_popup": "popup.html"
   },
   "background":{
      "persistents": false,
      "scripts": ["background.js"]
   },
   "options_page":"options.html",
   "manifest_version":2
}

Javascript:

    chrome.tabs.query({'active': true, 'currentWindow': true}, function (tabs) {
           var url = tabs[0].url;
           var project = url.replace("https://jira2dev.cerner.com/browse/", "");
           document.getElementById("demo").innerHTML =
            "Current Project: " + project;

});

I would like the code to return the current project the user is on whilst being able to use the code. The error does not allow the user to press any keys further on the extension.

D. Foskett
  • 122
  • 1
  • 1
  • 9
  • 1
    The error tells you, that there is no `.tabs` property inside the `chrome` namespace (`.tabs` is `undefined`) and you cannot call a `.query` method from `undefined` because it is, well, `undefined`. – David Jan 25 '19 at 16:00
  • Also have a look here: https://developer.chrome.com/extensions/tabs. Do you have the permissions you need? – David Jan 25 '19 at 16:02
  • It sounds like you're trying to run this code in the web context. Make sure you're not opening the html files directly from disk, but instead open them via the designated way e.g. by clicking the extension icon or opening options from chrome://extensions page. Also make sure you're looking at the correct devtools: the popup has its [own separate devtools](https://stackoverflow.com/a/38920982), not related to the web page. – wOxxOm Jan 25 '19 at 16:48

1 Answers1

0

You must be running this code in a google extension. The chrome.tabs will not work without a context of chrome available to the code. Everything will run perfectly find in a extension but will not work in a chrome tab because the website you are currently on is that of your project.

D. Foskett
  • 122
  • 1
  • 1
  • 9