0

I am trying to run a script after the youtube is loaded to querySelect a title but the script is not running. If I reload the page than it runs but I believe this happens because the previous page has already been loaded. I am trying to have it select the title on load, this is my manifest, I have tried run_at document_idle

{ 
"manifest_version": 2,

"version": "0.0.1", 

 "content_scripts": [ 
 {  "matches": ["<all_urls>"],    
    "js": ["content.js"],    
   "run_at": "document_end"    }  ],

  "background": {  
 "scripts": ["background.js"]  },
 "permissions": [   "activeTab", 
  "tabs" ]
}

In the script, I have tried window.onload,window.loaded, eventListener(load)

Ali
  • 2,702
  • 3
  • 32
  • 54

1 Answers1

-1

try this in your content script

document.addEventListener('readystatechange', event => {
  if (event.target.readyState === 'interactive') {
      //add your code here 
  }
});

this should work. However, if it doesn't change it from interactive to complete

Sven.hig
  • 4,449
  • 2
  • 8
  • 18
  • Content script is already running after DOMContentLoaded event by default so this doesn't change anything. The problem is that youtube scripts generates its page after the event. – wOxxOm Jun 25 '20 at 03:07
  • From the [documentation](https://developer.chrome.com/extensions/content_scripts): __i Content scripts running at "document_idle" do not need to listen for the window.onload event, they are guaranteed to run after the DOM is complete_. **If a script definitely needs to run after window.onload, the extension can check if onload has already fired by using the document.readyState property.** – Sven.hig Jun 25 '20 at 03:12
  • Your understanding of the documentation is incorrect: by the time DOMContentLoaded is fired readyState will be already `interactive` . Also, the problem here is not the subresources of the page which are loading between DOMContentLoaded and load event, but the fact that youtube scripts generate its page after the DOM is ready. This is how modern sites work. – wOxxOm Jun 25 '20 at 03:20
  • I am confused in your deleted comment you said that the documentation is incorrect and poorly maintained and now my understanding to the documentation is incorrect, can you elaborate please I am genuinely interested to know what is you interpretation to the documentation – Sven.hig Jun 25 '20 at 03:24
  • The documentation is often incorrect which is the first thing I incorrectly assumed. Then I've re-read your code and it occurred to me that the condition it checks doesn't make sense in case of content scripts. They run after DOMContentLoaded by default so readyState will be already interactive, see the comment above for more info. – wOxxOm Jun 25 '20 at 03:27
  • Yes I understand you completely and I agree with you. However, why then in the documentation they put this **If a script definitely needs to run after window.onload, the extension can check if onload has already fired by using the document.readyState property.** I am genuinely confused – Sven.hig Jun 25 '20 at 03:30
  • It meant you can check for `complete`, not for `interactive`, so I guess the documentation is rather bad after all. – wOxxOm Jun 25 '20 at 03:32
  • Thank you for your time and explanation, glad there are people like you around – Sven.hig Jun 25 '20 at 03:33