我有一个回调函数,它应该搜索给定网址的所有标签页,如果它打开并导航到该标签页,并且它没有打开,它应该打开它。包含网址的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