我有一個回調函數,它應該搜索給定網址的所有標籤頁,如果它打開並導航到該標籤頁,並且它沒有打開,它應該打開它。包含網址的Chrome瀏覽器打開標籤
我有以下查詢檢查標籤爲URL
chrome.tabs.query({url: destination}, function (result) {
//If it found no results then the page is not open
if(result == null) {
console.log(destination + " not found in tabs. Function will open Tab.")
//Appropriate function...
}
//If it's open, the function will switch to the tab instead.
else {
//Use the tab ID of the first result
var tabID = result[0].id;
//Appropriate function using id to open tab
}
的問題是,結果始終是null或undefined,永不返回一個數組(即使目的地==「http://*/*」)我本來如果有(result.length == 0),但它會給與使用
chrome.tabs.update(tabID, {
selected: true
});
切換到類型
我也嘗試錯誤標籤但這不起作用。我認爲這可能是因爲我需要將其放置在background.js頁面中,但我無法從此頁面調用它。
chrome.extension.getBackgroundPage().myFunction();
不起作用。
編輯:
您提到的權限會導致一個空字符串。這可能是答案,但我確實在權限中設置了標籤。這是我的權限在我的清單:
"permissions": [
"storage",
"notifications",
"activeTab",
"tabs",
"bookmarks",
"browsingData",
"identity",
"topSites",
"history"
]
您好。感謝您的回答,但您提到缺少導致空數組的權限?當我第一次遇到問題時,我確實檢查了我的清單,但我把它放在編輯中供您自己看。有什麼遺漏嗎? – user3542347
缺少權限不會導致數組爲空。如果該方法返回任何匹配項,則返回的TabObjects可能包含或不包含空的url字符串屬性,具體取決於您是否爲相應的url設置了權限。換句話說,您的權限設置不應該阻止查詢檢索準確的查詢結果。 –
你有什麼想法可能會導致這個問題? 它沒有返回一個數組給我。我從API複製了代碼示例,但它仍然無法工作。我將* tabs *重命名爲* result *,因爲它是在API中調用的。在我決定詢問之前,我花了很長時間在此,所以我幾乎可以確定問題在於檢索數組。 – user3542347