【发布时间】:2022-06-19 02:39:35
【问题描述】:
我正在尝试构建一个 chrome 扩展,当点击 tabBtn 时它将获取当前 url。我收到一条错误消息Cannot read properties of undefined (reading 'tabs') 我已经在 vanilla JS 中使用了这个方法chrome.browser.tabs.query({currentWindow: true,active: true },(tabs) => {}) 没有任何问题,但是使用 React 它不起作用。我尝试将上述代码放在useEffect() 中,但错误未解决。我已经尝试了来自这个article 和这个post 的例子,不幸的是它已经解决了。
*** 错误信息现在阅读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"
}
【问题讨论】:
-
删除
browser. -
我现在收到错误消息“无法读取未定义的属性(读取 '查询')'
-
我猜你从本地主机或文件打开你的页面:URL。您应该单击工具栏中的扩展图标。另请注意,弹出窗口是一个单独的窗口,因此它有自己单独的开发工具。
-
在 google chrome 扩展程序中,我现在收到此错误 'Uncaught TypeError: window.chrome.query is not a function'
-
您可能看到了一个旧错误。如上所述使用弹出窗口的开发工具。
标签: reactjs google-chrome-extension