TypeError: Cannot read properties of undefined (reading 'query')

13,995 views
Skip to first unread message

Suchintan Das

unread,
Nov 17, 2021, 12:40:54 AM11/17/21
to Chromium Extensions
While using manifest v2 I faced one issue while I was trying to use the APIs that chrome provide us for the extension like tabs. It was showing me a type error for one of its query parameters. I have already taken permission for tabs on my manifest and also checked the syntax that I am using. Can anyone help...

Manifest
{
    "name":"Welcome Page",
    "version": "1",
    "description":"Welcome page for the chrome extension",
    "permissions": [
        "tabs"
    ],
    "browser_action":{
        "default_title":"Welcome",
        "default_popup":"pages/starting.html"
    },
    "content_scripts": [
        {
          "matches": ["https://www.amazon.in/*"],
          "js": ["js/contentScript.js"]
        }
    ],
    "manifest_version":2,
    "content_security_policy":"script-src 'self' https://cdnjs.cloudflare.com; object-src 'self'"
}


Content Script
console.log("Content Script working!");
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
  console.log("Inside this function!")
    let url = tabs[0].url;
    console.log(url);
});

Browser Extenstion

unread,
Nov 17, 2021, 1:53:49 AM11/17/21
to Chromium Extensions, suchintand...@gmail.com
You need to run your code in background, not content script.

Suchintan Das

unread,
Nov 17, 2021, 6:25:36 AM11/17/21
to Chromium Extensions, Browser Extenstion, Suchintan Das
Thanks the script is working fine now.

Tanquen

unread,
Feb 10, 2022, 2:37:32 PM2/10/22
to Chromium Extensions, Browser Extenstion, suchintand...@gmail.com
I get the same error when using chrome.tabs.query in the popup.js and have seen posts that say it should work in the popup.js but not the content.js file.

The code works but I get the error whenever the matching page is loaded.

Uncaught TypeError: Cannot read properties of undefined (reading 'query')

Tanquen

unread,
Feb 10, 2022, 2:41:28 PM2/10/22
to Chromium Extensions, Tanquen, Browser Extenstion, suchintand...@gmail.com
I forgot to say it only has the error when the web page is loaded or reloaded not when the popup or any of the buttons I have on the popup that also use the  chrome.tabs.query function. ???

Simeon Vincent

unread,
Feb 12, 2022, 1:50:39 AM2/12/22
to Tanquen, Chromium Extensions, Browser Extenstion, suchintand...@gmail.com
Tanquen, since you haven't shared your code it is extremely difficult to say exactly what is happening. To try to get you on the right path, though, I've created a quick demo that shows the titles of all open tabs in an extension's popup.

manifest.json
{
"name": "tabs.query popup",
"version": "1.0",
"manifest_version": 3,
"action": {
"default_popup": "popup.html"
},
"permissions": ["tabs"]
}

popup.html
<!DOCTYPE html>
<head>
<script src="popup.js"></script>
</head>

popup.js
chrome.tabs.query({}, (tabs) => {
const tabsList = document.createElement('ul');

for (let tab of tabs) {
const listItem = document.createElement('li');
listItem.innerText = tab.title;
tabsList.append(listItem);
}

document.body.append(tabsList);
});

Simeon - @dotproto
Chrome Extensions DevRel


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/9cd662b3-9d99-4f51-a56a-5ebd7548b258n%40chromium.org.
Reply all
Reply to author
Forward
0 new messages