Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a run_at: document_idle content script run?

There are three options for running content scripts:

  1. document_start - injected at the start of the <head>
  2. document_end - injected right after DOMContentLoaded
  3. document_idle - injected when???
like image 615
Paul Irish Avatar asked Oct 20 '15 23:10

Paul Irish


People also ask

How do content scripts work?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.

How do you get a content script to load after a page's JavaScript has executed?

Add a script tag as the last element of the page (last thing in the body element). This script will be executed after all other scripts have finished and after the body has been converted to a DOM tree.

What is content script Chrome extension?

A content script is a part of your extension that runs in the context of a particular web page (as opposed to background scripts which are part of the extension, or scripts which are part of the website itself, such as those loaded using the <script> element).

What is the difference between content script and background script?

Background Script - Provides persistence and handles background events. Content Script - Scripts that run in isolation in the context of the web page. Injected Script - Scripts that are programmatically injected into the web page.


1 Answers

According to the current Chromium source:

We try to run idle in two places: here and DidFinishLoad. DidFinishDocumentLoad() corresponds to completing the document's load, whereas DidFinishLoad corresponds to completing the document and all subresources' load. We don't want to hold up script injection for a particularly slow subresource, so we set a delayed task from here - but if we finish everything before that point (i.e., DidFinishLoad() is triggered), then there's no reason to keep waiting.

Translated into web developer speak that basically means…

document_idle scripts will run the earliest one of these things is true:

  1. window.onload has fired
  2. It's been 200ms since DOMContentLoaded has fired.

On typical pages, these scripts will likely run at #2.

like image 73
Paul Irish Avatar answered Nov 08 '22 03:11

Paul Irish