即使存储URL的变量在chrome.tabs.query
中进行了更新,我似乎也无法返回更新的URL。执行命令后,将调用getFavicon
函数,然后尝试在选项卡的url上添加已初始化的url
变量。
chrome.commands.onCommand.addListener(colorTabs);
function colorTabs()
{
var favicon;
if ("left-key-toggle-feature")
{
favicon = getFavicon();
alert(favicon); // does not work
}
else if ("right-key-toggle-feature")
{
favicon = getFavicon();
alert(favicon); // does not work
}
};
/* gets the url that displays the favicon of the website */
function getFavicon()
{
var url = "https://www.google.com/s2/favicons?domain=";
var addFaviconUrl;
chrome.tabs.query({currentWindow: true, active: true}, function(tabs)
{
addFaviconUrl = tabs[0].url;
url += addFaviconUrl;
alert("right url: " + url); // shows correct url
})
return url; // returns incorrect url (not updated)
};