0

I've looked at other posts talking about using document.title to rename the title of a tab but I can't seem to get it to work.

I'm using a background script to send a message to the content script.

background.js

/* gets current tab */
        chrome.tabs.query({ active: true, currentWindow: true }, function (tab) 
        {
            var promptUser = prompt("Rename tab:");

            chrome.tabs.sendMessage(tabs[0].id, {title: promptUser}, function(response) {});
        });

content.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
    /* custom title */
    if (request.title)
    {
        document.title = request.title;
    }
})
Marcus Martin
  • 99
  • 1
  • 8
  • "I can't seem to get it to work" is too vague. Inspect and debug the [background script's console](https://stackoverflow.com/a/10258029). I guess `prompt` doesn't work there. – wOxxOm Aug 04 '18 at 15:49
  • Also, if this is the entire background.js, it's hardly useful as you would want to run this code in response to some event or action whereas currently it runs only at the start of the browser session. Try to provide an [MCVE](/help/mcve) in your question. – wOxxOm Aug 04 '18 at 16:01
  • Ah, when I checked the background script's console I had been trying to use tabs[0].id instead of tab[0].id! – Marcus Martin Aug 04 '18 at 16:03
  • I have a command that uses this function! Before I was trying to look at the console by right clicking the extension icon and going to inspect popup, is this the wrong way of looking at the console? – Marcus Martin Aug 04 '18 at 16:05
  • The popup is a separate page, the background script is part of the background page. Each page is a separate thing with its own URL, life cycle, devtools, console, DOM. – wOxxOm Aug 04 '18 at 16:12
  • Thank you for your help! – Marcus Martin Aug 04 '18 at 16:17

0 Answers0