I'm attempting to build a chrome extension that will grab the current url when tabBtn is clicked. I am receiving an error messageCannot read properties of undefined (reading 'tabs')
I've used this method chrome.browser.tabs.query({currentWindow: true,active: true },(tabs) => {})
in vanilla JS without any issues but using React its not working. I've tried placing the above code in useEffect()
but the error is unresolved. I've tried examples from this article and this post which was unfortunately resolved.
*** error message now read Cannot read properties of undefined (reading 'query')
/*global chrome*/
import {useEffect, useState} from 'react';
import {TabBtn} from "./components/Buttons"
function App() {
/* useEffect(()=>{
chrome.tabs.query(
{ currentWindow: true, active: true },
(tabs) => {
// setMyLeads((prev) => [...prev,
tabs[0].url]);
console.log(tabs[0].url);
}
);
},[]) */
const tabBtn = () => {
chrome.tabs.query(
{ currentWindow: true, active: true },
(tabs) => {
console.log(tabs[0].url);
}
);
}
return (
<main>
<TabBtn tabBtn={tabBtn} />
</main>
);
}
export default App
manifest.json
{
"name": "chrome extension app",
"version": "1.0",
"manifest_version": 3,
"permissions": [
"activeTab",
"storage",
"tabs"
],
"action": {
"default_popup": "index.html"
},
"default_icon": "/img/icon.png"
}