Chrome扩展程序:窗口的第一个创建的标签页未调用chrome.tabs.onUpdated

时间:2018-08-17 19:06:45

标签: javascript google-chrome-extension

我有一个功能,可以在新窗口中打开第一个选项卡,然后在该窗口中创建其余选项卡。创建第一个选项卡时,选项卡的标题不会更新,但是创建其余选项卡时,这些标题会更新。第一个标签的标题存储在tabIdsToTitles中,但由于某些原因未调用chrome.tabs.onUpdated.addListener(function(tabId, changeInfo)

控制台

set FIRST tabIdsToTitles[tab.id]: zzz
set tabIdsToTitles[tab.id]: aadf
set tabIdsToTitles[tab.id]: jjj
send tabIdsToTitles[tabId] to content.js: aadf, id: 3361
send tabIdsToTitles[tabId] to content.js: jjj, id: 3364

background.js的一部分

// stores title of tab for later use when tab is fully updated
var tabIdsToTitles = {};

/* creates the tabs in a new window */
function createWindowTabs(group, i, j)
{
    if (j == 0)
    {
        // creates first tab in a new window
        chrome.windows.create({"url": group["tabUrls" + i][j], "state": "maximized"}, function(tab)
        {
            /* onUpdated not being called on first tab of window for some reason */
            var tabTitle = group["tabNames" + i][j];
            tabIdsToTitles[tab.id] = tabTitle;
            console.log("set FIRST tabIdsToTitles[tab.id]: " + tabIdsToTitles[tab.id]);
        })
    }
    else
    {
        chrome.tabs.create({"url": group["tabUrls" + i][j], "active": false}, function(tab)
        {
            var tabTitle = group["tabNames" + i][j];
            tabIdsToTitles[tab.id] = tabTitle;
            console.log("set tabIdsToTitles[tab.id]: " + tabIdsToTitles[tab.id]);
        })
    }
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo)
{
    if (tabIdsToTitles[tabId] && changeInfo.status === "complete") 
    {
        chrome.tabs.sendMessage(tabId, {getTitle: tabIdsToTitles[tabId]}, function(response){});
        console.log("send tabIdsToTitles[tabId] to content.js: " + tabIdsToTitles[tabId] + ", id: " + tabId);
    }
})

content.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
    if (request.getTitle)
    {
        document.title = request.getTitle;
    }
})

0 个答案:

没有答案