[英]Chrome Extension: getting active url from popup undefined
當我的 Chrome 擴展程序的彈出窗口打開時,我正在嘗試獲取選項卡 url。 我查看了其他答案並想出了這個:
export const getTab = () => {
return new Promise((res) => {
chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => {
console.log('tabs:', tabs);
res(tabs[0]);
});
});
};
Promise 解析為
{
"active": true,
"audible": false,
"autoDiscardable": true,
"discarded": false,
"groupId": -1,
"height": 624,
"highlighted": true,
"id": 2297,
"incognito": false,
"index": 1,
"mutedInfo": {
"muted": false
},
"openerTabId": 128,
"pinned": false,
"selected": true,
"status": "complete",
"width": 160,
"windowId": 1
}
選項卡的 url undefined
!
我已經嘗試將"tabs"
和"activeTab"
添加到我的清單 v3 的permissions
數組中,但 url 仍然未定義。 幫助!
編輯:
manifest.json
{
"manifest_version": 3,
"name": "Test",
"version": "1.0.0",
"action": {
"default_title": "Test",
"default_popup": "popup.html"
},
"permissions": ["tabs"],
"background": {
"service_worker": "src/background.js",
"type": "module"
},
"content_scripts": [
{
"js": ["src/contentScript.js"],
"matches": ["<all_urls>"],
"run_at": "document_start",
"all_frames": true
}
],
}
如何等待 chrome.tabs.query 返回選項卡 ID
如果您將其從 id 更改為 url(由 Lakshya Thakur 發布),則此問題的答案將是您的問題的解決方案。
當我試圖將當前標簽 url 轉換為 chrome 擴展時,這確實解決了我的問題。
function getTabID() {
return new Promise((resolve, reject) => {
try {
chrome.tabs.query({
active: true,
}, function (tabs) {
resolve(tabs[0].id);
})
} catch (e) {
reject(e);
}
})
}
//function where you need it
async function something() {
let responseTabID = await getTabID();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.