K
K
k1zn2021-08-20 18:38:09
JavaScript
k1zn, 2021-08-20 18:38:09

How to change page title before DOM is loaded (Chrome extension)?

I'm trying to change the title of the page through the chrome extension before it is loaded, but the DOM has not yet been loaded and this does not work. Are there any options? You need to change the title before the page loads so that the old one is not noticeable.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
k1zn, 2021-08-21
@k1zn

I found a non-crutch solution (someone like it, maybe it's better):

var title = document.createElement('title')
title.innerText = 'всем привет я из internet'
document.documentElement.appendChild(title)

Works at the start of the document.

N
Nadim Zakirov, 2021-08-20
@zkrvndm

This of course can be done, but only through a crutch. Specify the run_at option with a value of document_start in your extension's manifest to have your content script run before the document is ready:

{
  
  "name" : "Имя расширения",
  
  "manifest_version" : 2,
  
  // ............................
  
  "content_scripts" : [
    
    {	
      "matches" : [ "https://*.site.ru/*", ],
      "run_at": "document_start",
      "js" : [ "content-script.js" ]
    }
    
  ]
  
}

Next, in the content script, call:
document.write('');
document.close();

This will instantly overwrite the content of the page with a void, and then do with it (with the page) what you want.
PS Why do you need it, if not a secret? Maybe I can suggest another solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question