From fa7b6b547368d12ccd61f6cbf251f2ff0d37b504 Mon Sep 17 00:00:00 2001 From: "aa@chromium.org" Date: Tue, 3 Nov 2009 05:02:30 +0000 Subject: Re-commit "Add new user script injection point: document_idle." Original code review: http://codereview.chromium.org/339064 BUG=26126 TBR=rafaelw@chromium.org git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30796 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/common/extensions/docs/content_scripts.html | 19 ++++++++++++++++++- .../extensions/docs/static/content_scripts.html | 19 ++++++++++++++++++- chrome/common/extensions/extension.cc | 2 ++ chrome/common/extensions/extension_constants.cc | 1 + chrome/common/extensions/extension_constants.h | 1 + chrome/common/extensions/user_script.h | 11 ++++++++--- chrome/common/extensions/user_script_unittest.cc | 2 +- 7 files changed, 49 insertions(+), 6 deletions(-) (limited to 'chrome/common/extensions') diff --git a/chrome/common/extensions/docs/content_scripts.html b/chrome/common/extensions/docs/content_scripts.html index 51beb4d..feba6ba 100644 --- a/chrome/common/extensions/docs/content_scripts.html +++ b/chrome/common/extensions/docs/content_scripts.html @@ -346,7 +346,24 @@ learn about the run_at string - Optional. Controls when the files in js are injected. Can be "document_start" or "document_end". Defaults to "document_end". In the case of "document_start", the files are injected after any files from "css", but before any other DOM is constructed or any other script is run. In the case of "document_end", the files are injected after the DOM is complete, but before subresources like images and frames have necessarily loaded. + Optional. Controls when the files in js are injected. Can be "document_start", "document_end", or "document_idle". Defaults to "document_idle". + +

+ + In the case of "document_start", the files are injected after any files from "css", but before any other DOM is constructed or any other script is run. + +

+ + In the case of "document_end", the files are injected immediately after the DOM is complete, but before subresources like images and frames have loaded. + +

+ + In the case of "document_idle", the browser chooses a time to inject scripts between "document_end" and immediately after the window.onload event fires. The exact moment of injection depends on how complex the document is and how long it is taking to load, and is optimized for page load speed. + +

+ + NOTE: In document_idle, content scripts may not necessarily receive the window.onload event, because they may run after it has + already fired. In most cases, listening for the onload event is unnecessary for content scripts running at document_idle because they are guaranteed to run after the DOM is complete. If your script definitely needs to run after window.onload you can check if it has already fired by using the document.readyState property. diff --git a/chrome/common/extensions/docs/static/content_scripts.html b/chrome/common/extensions/docs/static/content_scripts.html index cbd738f..9f1edf1 100644 --- a/chrome/common/extensions/docs/static/content_scripts.html +++ b/chrome/common/extensions/docs/static/content_scripts.html @@ -104,7 +104,24 @@ learn about the run_at string - Optional. Controls when the files in js are injected. Can be "document_start" or "document_end". Defaults to "document_end". In the case of "document_start", the files are injected after any files from "css", but before any other DOM is constructed or any other script is run. In the case of "document_end", the files are injected after the DOM is complete, but before subresources like images and frames have necessarily loaded. + Optional. Controls when the files in js are injected. Can be "document_start", "document_end", or "document_idle". Defaults to "document_idle". + +

+ + In the case of "document_start", the files are injected after any files from "css", but before any other DOM is constructed or any other script is run. + +

+ + In the case of "document_end", the files are injected immediately after the DOM is complete, but before subresources like images and frames have loaded. + +

+ + In the case of "document_idle", the browser chooses a time to inject scripts between "document_end" and immediately after the window.onload event fires. The exact moment of injection depends on how complex the document is and how long it is taking to load, and is optimized for page load speed. + +

+ + NOTE: In document_idle, content scripts may not necessarily receive the window.onload event, because they may run after it has + already fired. In most cases, listening for the onload event is unnecessary for content scripts running at document_idle because they are guaranteed to run after the DOM is complete. If your script definitely needs to run after window.onload you can check if it has already fired by using the document.readyState property. diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index 429dfb5..c617232 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -208,6 +208,8 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, result->set_run_location(UserScript::DOCUMENT_START); } else if (run_location == values::kRunAtDocumentEnd) { result->set_run_location(UserScript::DOCUMENT_END); + } else if (run_location == values::kRunAtDocumentIdle) { + result->set_run_location(UserScript::DOCUMENT_IDLE); } else { *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidRunAt, IntToString(definition_index)); diff --git a/chrome/common/extensions/extension_constants.cc b/chrome/common/extensions/extension_constants.cc index 8259009..59e7c14 100644 --- a/chrome/common/extensions/extension_constants.cc +++ b/chrome/common/extensions/extension_constants.cc @@ -52,6 +52,7 @@ const wchar_t* kOptionsPage = L"options_page"; namespace extension_manifest_values { const char* kRunAtDocumentStart = "document_start"; const char* kRunAtDocumentEnd = "document_end"; +const char* kRunAtDocumentIdle = "document_idle"; const char* kPageActionTypeTab = "tab"; const char* kPageActionTypePermanent = "permanent"; } // namespace extension_manifest_values diff --git a/chrome/common/extensions/extension_constants.h b/chrome/common/extensions/extension_constants.h index 7f44bf6..b7826a7 100644 --- a/chrome/common/extensions/extension_constants.h +++ b/chrome/common/extensions/extension_constants.h @@ -54,6 +54,7 @@ namespace extension_manifest_keys { namespace extension_manifest_values { extern const char* kRunAtDocumentStart; extern const char* kRunAtDocumentEnd; + extern const char* kRunAtDocumentIdle; extern const char* kPageActionTypeTab; extern const char* kPageActionTypePermanent; } // namespace extension_manifest_values diff --git a/chrome/common/extensions/user_script.h b/chrome/common/extensions/user_script.h index 28b3aae..bdd1e54 100644 --- a/chrome/common/extensions/user_script.h +++ b/chrome/common/extensions/user_script.h @@ -28,6 +28,10 @@ class UserScript { // anything else happens. DOCUMENT_END, // After the entire document is parsed. Same as // DOMContentLoaded. + DOCUMENT_IDLE, // Sometime after DOMContentLoaded, as soon as the document + // is "idle". Currently this uses the simple heuristic of: + // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no + // particular injection point is guaranteed. RUN_LOCATION_LAST // Leave this as the last item. }; @@ -88,9 +92,10 @@ class UserScript { typedef std::vector FileList; - // Constructor. Default the run location to document end, which is like - // Greasemonkey and probably more useful for typical scripts. - UserScript() : run_location_(DOCUMENT_END) {} + // Constructor. Default the run location to document idle, which is similar + // to Greasemonkey but should result in better page load times for fast- + // loading pages. + UserScript() : run_location_(DOCUMENT_IDLE) {} // The place in the document to run the script. RunLocation run_location() const { return run_location_; } diff --git a/chrome/common/extensions/user_script_unittest.cc b/chrome/common/extensions/user_script_unittest.cc index b171f4e..58ef77e 100644 --- a/chrome/common/extensions/user_script_unittest.cc +++ b/chrome/common/extensions/user_script_unittest.cc @@ -122,5 +122,5 @@ TEST(UserScriptTest, Pickle) { TEST(UserScriptTest, Defaults) { UserScript script; - ASSERT_EQ(UserScript::DOCUMENT_END, script.run_location()); + ASSERT_EQ(UserScript::DOCUMENT_IDLE, script.run_location()); } -- cgit v1.1