1 <h1>Content Scripts
</h1>
5 Content scripts are JavaScript files that run in the context of web pages.
7 <a href=
"http://www.w3.org/TR/DOM-Level-2-HTML/">Document
8 Object Model
</a> (DOM),
9 they can read details of the web pages the browser visits,
10 or make changes to them.
14 Here are some examples of what content scripts can do:
18 <li>Find unlinked URLs in web pages and convert them into hyperlinks
19 <li>Increase the font size to make text more legible
20 <li>Find and process
<a href=
"http://microformats.org/">microformat
</a> data in the DOM
24 However, content scripts have some limitations.
32 <a href=
"extension.html"><code>chrome.extension
</code></a>)
35 Use variables or functions defined by their extension's pages
38 Use variables or functions defined by web pages or by other content scripts
43 These limitations aren't as bad as they sound.
44 Content scripts can
<em>indirectly
</em> use the chrome.* APIs,
45 get access to extension data,
46 and request extension actions
47 by exchanging
<a href=
"messaging.html">messages
</a>
48 with their parent extension.
49 Content scripts can also
50 make
<a href=
"xhr.html">cross-site XMLHttpRequests
</a>
51 to the same sites as their parent extensions,
53 <a href=
"#host-page-communication">communicate with web pages
</a>
55 For more insight into what content scripts can and can't do,
57 <a href=
"#execution-environment">execution environment
</a>.
60 <h2 id=
"registration">Manifest
</h2>
62 <p>If your content script's code should always be injected,
64 <a href=
"manifest.html">extension manifest
</a>
65 using the
<code>content_scripts
</code> field,
66 as in the following example.
69 <pre data-filename=
"manifest.json">
71 "name":
"My extension",
73 <b>"content_scripts": [
75 "matches": [
"http://www.google.com/*"],
76 "css": [
"mystyles.css"],
77 "js": [
"jquery.js",
"myscript.js"]
85 If you want to inject the code only sometimes,
87 <a href=
"declare_permissions.html"><code>permissions
</code></a> field instead,
88 as described in
<a href=
"#pi">Programmatic injection
</a>.
91 <pre data-filename=
"manifest.json">
93 "name":
"My extension",
96 "tabs",
"http://www.google.com/*"
103 Using the
<code>content_scripts
</code> field,
104 an extension can insert multiple content scripts into a page;
105 each of these content scripts can have multiple JavaScript and CSS files.
106 Each item in the
<code>content_scripts
</code> array
107 can have the following properties:
</p>
109 <table class=
"simple">
116 <td><code>matches
</code></td>
117 <td>array of strings
</td>
118 <td><em>Required.
</em>
119 Specifies which pages this content script will be injected into.
120 See
<a href=
"match_patterns.html">Match Patterns
</a>
121 for more details on the syntax of these strings
122 and
<a href=
"#match-patterns-globs">Match patterns and globs
</a>
123 for information on how to exclude URLs.
</td>
126 <td><code>exclude_matches
</code></td>
127 <td>array of strings
</td>
128 <td><em>Optional.
</em>
129 Excludes pages that this content script would otherwise be
131 See
<a href=
"match_patterns.html">Match Patterns
</a>
132 for more details on the syntax of these strings
133 and
<a href=
"#match-patterns-globs">Match patterns and globs
</a>
134 for information on how to exclude URLs.
</td>
137 <td><code>css
<code></td>
138 <td>array of strings
</td>
139 <td><em>Optional.
</em>
140 The list of CSS files to be injected into matching pages. These are injected in the order they appear in this array, before any DOM is constructed or displayed for the page.
</td>
143 <td><code>js
<code></td>
144 <td><nobr>array of strings
</nobr></td>
145 <td><em>Optional.
</em>
146 The list of JavaScript files to be injected into matching pages. These are injected in the order they appear in this array.
</td>
149 <td><code>run_at
<code></td>
151 <td><em>Optional.
</em>
152 Controls when the files in
<code>js
</code> are injected. Can be
"document_start",
"document_end", or
"document_idle". Defaults to
"document_idle".
156 In the case of
"document_start", the files are injected after any files from
<code>css
</code>, but before any other DOM is constructed or any other script is run.
160 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.
164 In the case of
"document_idle", the browser chooses a time to inject scripts between
"document_end" and immediately after the
<code><a href=
"http://www.whatwg.org/specs/web-apps/current-work/#handler-onload">window.onload
</a></code> 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.
168 <b>Note:
</b> With
"document_idle", content scripts may not necessarily receive the
<code>window.onload
</code> event, because they may run after it has
169 already fired. In most cases, listening for the
<code>onload
</code> 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
<code>window.onload
</code>, you can check if
<code>onload
</code> has already fired by using the
<code><a href=
"http://www.whatwg.org/specs/web-apps/current-work/#dom-document-readystate">document.readyState
</a></code> property.
</td>
172 <td><code>all_frames
<code></td>
174 <td><em>Optional.
</em>
175 Controls whether the content script runs in all frames of the matching page, or only the top frame.
177 Defaults to
<code>false
</code>, meaning that only the top frame is matched.
</td>
180 <td><code>include_globs
</code></td>
181 <td>array of string
</td>
182 <td><em>Optional.
</em>
183 Applied after
<code>matches
</code> to include only those URLs that also match this glob. Intended to emulate the
<a href=
"http://wiki.greasespot.net/Metadata_Block#.40include"><code>@include
</code></a> Greasemonkey keyword.
184 See
<a href=
"#match-patterns-globs">Match patterns and globs
</a> below for more details.
</td>
187 <td><code>exclude_globs
</code></td>
188 <td>array of string
</td>
189 <td><em>Optional.
</em>
190 Applied after
<code>matches
</code> to exclude URLs that match this glob.
191 Intended to emulate the
<a href=
"http://wiki.greasespot.net/Metadata_Block#.40include"><code>@exclude
</code></a> Greasemonkey keyword.
192 See
<a href=
"#match-patterns-globs">Match patterns and globs
</a> below for more details.
</td>
196 <h3 id=
"match-patterns-globs">Match patterns and globs
</h3>
199 The content script will be injected into a page if its URL matches any
<code>matches
</code> pattern and any
<code>include_globs
</code> pattern, as long as the URL doesn't also match an
<code>exclude_matches
</code> or
<code>exclude_globs
</code> pattern.
201 Because the
<code>matches
</code> property is required,
<code>exclude_matches
</code>,
<code>include_globs
</code>, and
<code>exclude_globs
</code> can only be used to limit which pages will be affected.
205 For example, assume
<code>matches
</code> is
<code>[
"http://*.nytimes.com/*"]
</code>:
208 <li>If
<code>exclude_matches
</code> is
<code>[
"*://*/*business*"]
</code>, then the content script would be injected into
"http://www.nytimes.com/health" but not into
"http://www.nytimes.com/business".
</li>
209 <li>If
<code>include_globs
</code> is
<code>[
"*nytimes.com/???s/*"]
</code>, then the content script would be injected into
"http:/www.nytimes.com/arts/index.html" and
"http://www.nytimes.com/jobs/index.html" but not into
"http://www.nytimes.com/sports/index.html".
</li>
210 <li>If
<code>exclude_globs
</code> is
<code>[
"*science*"]
</code>, then the content script would be injected into
"http://www.nytimes.com" but not into
"http://science.nytimes.com" or
"http://www.nytimes.com/science".
</li>
215 Glob properties follow a different, more flexible syntax than
<a href=
"match_patterns.html">match patterns
</a>. Acceptable glob strings are URLs that may contain
"wildcard" asterisks and question marks. The asterisk (*) matches any string of any length (including the empty string); the question mark (?) matches any single character.
219 For example, the glob
"http://???.example.com/foo/*" matches any of the following:
222 <li>"http://www.example.com/foo/bar"</li>
223 <li>"http://the.example.com/foo/"</li>
226 However, it does
<em>not
</em> match the following:
229 <li>"http://my.example.com/foo/bar"</li>
230 <li>"http://example.com/foo/"</li>
231 <li>"http://www.example.com/foo"</li>
234 <h2 id=
"pi">Programmatic injection
</h2>
237 Inserting code into a page programmatically is useful
238 when your JavaScript or CSS code
239 shouldn't be injected into every single page
240 that matches the pattern
—
241 for example, if you want a script to run
242 only when the user clicks a browser action's icon.
246 To insert code into a page,
247 your extension must have
248 <a href=
"xhr.html#requesting-permission">cross-origin permissions
</a>
250 It also must be able to use the
<code>chrome.tabs
</code> module.
251 You can get both kinds of permission
252 using the manifest file's
253 <a href=
"declare_permissions.html">permissions
</a> field.
257 Once you have permissions set up,
258 you can inject JavaScript into a page by calling
259 $ref:tabs.executeScript.
267 <a href=
"http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/browserAction/make_page_red/">make_page_red
</a> example)
268 reacts to a user click
269 by inserting JavaScript into the current tab's page
270 and executing the script.
273 <pre data-filename=
"background.html">
274 chrome.browserAction.onClicked.addListener(function(tab) {
275 chrome.tabs.executeScript({
276 code: 'document.body.style.
backgroundColor=
"red"'
280 <pre data-filename=
"manifest.json">
287 When the browser is displaying an HTTP page
288 and the user clicks this extension's browser action,
289 the extension sets the page's
<code>bgcolor
</code> property to 'red'.
291 unless the page has CSS that sets the background color,
292 is that the page turns red.
296 Usually, instead of inserting code directly (as in the previous sample),
297 you put the code in a file.
298 You inject the file's contents like this:
301 <pre>chrome.tabs.executeScript(null, {file:
"content_script.js"});
</pre>
304 <h2 id=
"execution-environment">Execution environment
</h2>
306 <p>Content scripts execute in a special environment called an
<em>isolated world
</em>. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.
308 <p>For example, consider this simple page:
310 <pre data-filename=
"hello.html">
312 <button
id=
"mybutton">click me
</button
>
314 var greeting =
"hello, ";
315 var button = document.getElementById(
"mybutton");
316 button.person_name =
"Bob";
317 button.addEventListener(
"click", function() {
318 alert(greeting + button.person_name +
".");
324 <p>Now, suppose this content script was injected into hello.html:
326 <pre data-filename=
"contentscript.js">
327 var greeting =
"hola, ";
328 var button = document.getElementById(
"mybutton");
329 button.person_name =
"Roberto";
330 button.addEventListener(
"click", function() {
331 alert(greeting + button.person_name +
".");
335 <p>Now, if the button is pressed, you will see both greetings.
337 <p>Isolated worlds allow each content script to make changes to its JavaScript environment without worrying about conflicting with the page or with other content scripts. For example, a content script could include JQuery v1 and the page could include JQuery v2, and they wouldn't conflict with each other.
339 <p>Another important benefit of isolated worlds is that they completely separate the JavaScript on the page from the JavaScript in extensions. This allows us to offer extra functionality to content scripts that should not be accessible from web pages without worrying about web pages accessing it.
341 <p>It's worth noting what happens with JavaScript objects that are shared by the page and the extension - for example, the
<code>window.onload
</code> event. Each isolated world sees its own version of the object. Assigning to the object affects your independent copy of the object. For example, both the page and extension can assign to
<code>window.onload
</code>, but neither one can read the other's event handler. The event handlers are called in the order in which they were assigned.
343 <h2 id=
"host-page-communication">Communication with the embedding page
</h2>
345 <p>Although the execution environments of content scripts and the pages that host them are isolated from each other, they share access to the page's DOM. If the page wishes to communicate with the content script (or with the extension via the content script), it must do so through the shared DOM.
</p>
346 <p>An example can be accomplished using window.postMessage (or window.webkitPostMessage for Transferable objects):
</p>
347 <pre data-filename=
"contentscript.js">
348 var port = chrome.runtime.connect();
350 window.addEventListener(
"message", function(event) {
351 // We only accept messages from ourselves
352 if (event.source != window)
355 if (event.data.type
&& (event.data.type ==
"FROM_PAGE")) {
356 console.log(
"Content script received: " + event.data.text);
357 port.postMessage(event.data.text);
361 <pre data-filename=
"http://foo.com/example.html">
362 document.getElementById(
"theButton").addEventListener(
"click",
364 window.postMessage({ type:
"FROM_PAGE", text:
"Hello from the webpage!" },
"*");
366 <p>In the above example, example.html (which is not a part of the extension) posts messages to itself, which are intercepted and inspected by the content script, and then posted to the extension process. In this way, the page establishes a line of communication to the extension process. The reverse is possible through similar means.
</p>
368 <h2 id=
"security-considerations">Security considerations
</h2>
370 <p>When writing a content script, you should be aware of two security issues.
371 First, be careful not to introduce security vulnerabilities into the web site
372 your content script is injected into. For example, if your content script
373 receives content from another web site (for example, by making an
<a
374 href=
"xhr.html">XMLHttpRequest
</a>),
375 be careful to filter that content for
<a
376 href=
"http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site
377 scripting
</a> attacks before injecting the content into the current page.
378 For example, prefer to inject content via innerText rather than innerHTML.
379 Be especially careful when retrieving HTTP content on an HTTPS page because
380 the HTTP content might have been corrupted by a network
<a
381 href=
"http://en.wikipedia.org/wiki/Man-in-the-middle_attack">"man-in-the-middle"</a>
382 if the user is on a hostile network.
</p>
384 <p>Second, although running your content script in an isolated world provides
385 some protection from the web page, a malicious web page might still be able
386 to attack your content script if you use content from the web page
387 indiscriminately. For example, the following patterns are dangerous:
388 <pre data-filename=
"contentscript.js">
389 var data = document.getElementById(
"json-data")
390 // WARNING! Might be evaluating an evil script!
391 var parsed = eval(
"(" + data +
")")
393 <pre data-filename=
"contentscript.js">
395 // WARNING! elmt_id might be
"); ... evil script ... //"!
396 window.setTimeout(
"animate(" + elmt_id +
")",
200);
398 <p>Instead, prefer safer APIs that do not run scripts:
</p>
399 <pre data-filename=
"contentscript.js">
400 var data = document.getElementById(
"json-data")
401 // JSON.parse does not evaluate the attacker's scripts.
402 var parsed = JSON.parse(data);
404 <pre data-filename=
"contentscript.js">
406 // The closure form of setTimeout does not evaluate scripts.
407 window.setTimeout(function() {
412 <h2 id=
"extension-files">Referring to extension files
</h2>
415 Get the URL of an extension's file using
416 <code>chrome.extension.getURL()
</code>.
417 You can use the result
418 just like you would any other URL,
419 as the following code shows.
424 <em>//Code for displaying
<extensionDir
>/images/myimage.png:
</em>
425 var imgURL =
<b>chrome.extension.getURL(
"images/myimage.png")
</b>;
426 document.getElementById(
"someImage").src = imgURL;
429 <h2 id=
"examples"> Examples
</h2>
433 <a href=
"samples.html#script">examples that use content scripts
</a>.
434 A simple example of communication via messages is in the
435 <a href=
"samples.html#message-timer">Message Timer
</a>.
436 See
<a href=
"samples.html#page-redder">Page Redder
</a> and
437 <a href=
"samples.html#email-this-page-(by-google)">Email This Page
</a>
438 for examples of programmatic injection.
442 <h2 id=
"videos"> Videos
</h2>
445 The following videos discuss concepts that are important for content scripts.
446 The first video describes content scripts and isolated worlds.
450 <iframe title=
"YouTube video player" width=
"640" height=
"390" src=
"//www.youtube.com/embed/laLudeUmXHM?rel=0" frameborder=
"0" allowfullscreen
></iframe>
454 The next video describes message passing,
455 featuring an example of a content script
456 sending a request to its parent extension.
460 <iframe title=
"YouTube video player" width=
"640" height=
"390" src=
"//www.youtube.com/embed/B4M_a7xejYI?rel=0" frameborder=
"0" allowfullscreen
></iframe>