Chrome扩展程序:刷新标签会导致页面标题丢失(YouTube)

时间:2018-09-29 01:48:56

标签: javascript google-chrome-extension

当我更改YouTube页面上的标签标题并刷新页面时,该标题会被默认的视频标题覆盖。我使用chrome.tabs.onUpdated来确保在更改标题之前页面已完全加载,但是在刷新页面时,在将新标题替换为原始页面标题之前,我可以短暂地看到它。这也发生在Twitch上,所以也许与视频网站有关?

background.js

/* keeps the tab's title and color persistent tab refresh */
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
    /* once content script has finished loading in the new tab, send a message with the tab's title to the content script; */

    if (changeInfo.status === "complete")
    {
        // tab's title was changed
        if (saveTitle[tabId] != null)
        {
            // tab's title sent to content script
            chrome.tabs.sendMessage(tabId, {title: saveTitle[tabId]}, function(response){});
        }
    }
})

内容脚本

/* sets the title of the tab */
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
    if (request.title)
    {
        document.title = request.title;
    }
})

0 个答案:

没有答案