Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

267
Visualizações
Window title is not changed after pdf is loaded

I'm creating a chrome extension which changes the title of some pdf page, for instance https://arxiv.org/pdf/2005.01463v2.pdf.

The code works fine before the pdf is loaded, but when the pdf is fully loaded the window's title is changed back to the pdf name (instead of my title) and using document.title = "cool title" or Chrome messages to background.js does not work.

However doing a manual document.title = "cool title" from the console in a given pdf page works well.

// content_script.js
const makeTitle = (time) => {
    setTimeout(() => {
        title = "cool title"
        console.log("Updating pdf title");
        chrome.runtime.sendMessage({ type: "update-title", options: { title } })
        window.document.title = title;
    }, time)
}

makeTitle(0)
makeTitle(1 * 1000);
makeTitle(5 * 1000);
makeTitle(10 * 1000);
makeTitle(20 * 1000);
makeTitle(60 * 1000);
makeTitle(5 * 60 * 1000);
// background.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    console.log("Executing background message")
    if (request.type == "update-title") {
        const { title } = request.options;
        chrome.tabs.executeScript(sender.tab.id, { code: `document.title = "${title}"` });
    }
});

I have to use those setTimeout because I cannot detect if a pdf is done loading. makeTitle works fine before the pdf is loaded, but not after and I'd like to solve this. Thanks!

PS: I know there's a duplicate title change, it's just none of those 2 strategies work

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's a bug in Chrome. It sets the title internally and ignores DOM title element.

The workaround is to use chrome.tabs.onUpdated to set an empty title, then set the intended one.

background.js

const title = 'foo';
chrome.tabs.onUpdated.addListener((tabId, info, tab) => {
  if (info.title && info.title !== title && tab.url.endsWith('.pdf')) {
    chrome.tabs.executeScript(tabId, {
      code: `document.title=''; document.title="${title}"`,
      runAt: 'document_start',
      // runs immediately if this change occurs before DOMContentLoaded
    });
  }
});

There's another bug in Chrome: it shows the built-in title intermittently when you switch to another tab and back. This particular bug seems to be fixed in the upcoming "unseasoned" PDF viewer, which can be enabled currently via chrome://flags/#pdf-unseasoned

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda