Chrome扩展程序:chrome.tabs.query

时间:2018-07-24 17:41:19

标签: javascript google-chrome-extension

即使存储URL的变量在chrome.tabs.query中进行了更新,我似乎也无法返回更新的URL。执行命令后,将调用getFavicon函数,然后尝试在选项卡的url上添加已初始化的url变量。

background.js

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)
};

0 个答案:

没有答案