1

I'm working on a Chrome extension that finds the desired tag name and changes its style.

However, when the web page is still loading, the content script is injected, so the desired tag name cannot be found and the program ends.

I want the content script injected after the web page is fully loaded. What should I do

perear
  • 15
  • 1
  • 4

2 Answers2

1

Chrome has a chrome.tabs.onUpdated event you can listen to. There is a property called changeInfo.status that you can check to see if the page is loaded or not. You need to add this to the Background script.

https://developer.chrome.com/docs/extensions/mv3/background_pages/

In your case to check if the page has loaded or not, you can add this condition

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    if(changeInfo.status === 'complete') {
    //do stuff
    }
}
sagar1025
  • 616
  • 9
  • 22
-1

You can add an eventListener on the 'DOMContentLoaded' event on the window object.

 window.addEventListener('DOMContentLoaded', (event) => {
    console.log('DOM fully loaded and parsed');
});
anuragb26
  • 1,467
  • 11
  • 14