- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / templates / articles / content_scripts.html
1 <h1>Content Scripts</h1>
2
3
4 <p>
5 Content scripts are JavaScript files that run in the context of web pages.
6 By using the standard
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.
11 </p>
12
13 <p>
14 Here are some examples of what content scripts can do:
15 </p>
16
17 <ul>
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
21 </ul>
22
23 <p>
24 However, content scripts have some limitations.
25 They <b>cannot</b>:
26 </p>
27
28 <ul>
29   <li>
30     Use chrome.* APIs
31     (except for parts of
32     <a href="extension.html"><code>chrome.extension</code></a>)
33   </li>
34   <li>
35     Use variables or functions defined by their extension's pages
36   </li>
37   <li>
38     Use variables or functions defined by web pages or by other content scripts
39   </li>
40 </ul>
41
42 <p>
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,
52 and they can
53 <a href="#host-page-communication">communicate with web pages</a>
54 using the shared DOM.
55 For more insight into what content scripts can and can't do,
56 learn about the
57 <a href="#execution-environment">execution environment</a>.
58 </p>
59
60 <h2 id="registration">Manifest</h2>
61
62 <p>If your content script's code should always be injected,
63 register it in the
64 <a href="manifest.html">extension manifest</a>
65 using the <code>content_scripts</code> field,
66 as in the following example.
67 </p>
68
69 <pre data-filename="manifest.json">
70 {
71   "name": "My extension",
72   ...
73   <b>"content_scripts": [
74     {
75       "matches": ["http://www.google.com/*"],
76       "css": ["mystyles.css"],
77       "js": ["jquery.js", "myscript.js"]
78     }
79   ]</b>,
80   ...
81 }
82 </pre>
83
84 <p>
85 If you want to inject the code only sometimes,
86 use the
87 <a href="declare_permissions.html"><code>permissions</code></a> field instead,
88 as described in <a href="#pi">Programmatic injection</a>.
89 </p>
90
91 <pre data-filename="manifest.json">
92 {
93   "name": "My extension",
94   ...
95   <b>"permissions": [
96     "tabs", "http://www.google.com/*"
97   ]</b>,
98   ...
99 }
100 </pre>
101
102 <p>
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>
108
109 <table class="simple">
110   <tr>
111     <th>Name</th>
112     <th>Type</th>
113     <th>Description</th>
114   </tr>
115   <tr>
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>
124   </tr>
125   <tr>
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
130     injected into.
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>
135   </tr>
136   <tr>
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>
141   </tr>
142   <tr>
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>
147   </tr>
148   <tr id="run_at">
149     <td><code>run_at<code></td>
150     <td>string</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".
153
154     <br><br>
155
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.
157
158     <br><br>
159
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.
161
162     <br><br>
163
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.
165
166     <br><br>
167
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>
170   </tr>
171   <tr>
172     <td><code>all_frames<code></td>
173     <td>boolean</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.
176     <br><br>
177     Defaults to <code>false</code>, meaning that only the top frame is matched.</td>
178   </tr>
179   <tr>
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>
185   </tr>
186   <tr>
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>
193   </tr>
194 </table>
195
196 <h3 id="match-patterns-globs">Match patterns and globs</h3>
197
198 <p>
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.
200
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.
202 </p>
203
204 <p>
205 For example, assume <code>matches</code> is <code>["http://*.nytimes.com/*"]</code>:
206 </p>
207 <ul>
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>
211 </ul>
212 <p>
213
214 <p>
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.
216 </p>
217
218 <p>
219 For example, the glob "http://???.example.com/foo/*" matches any of the following:
220 </p>
221 <ul>
222   <li>"http://www.example.com/foo/bar"</li>
223   <li>"http://the.example.com/foo/"</li>
224 </ul>
225 <p>
226 However, it does <em>not</em> match the following:
227 </p>
228 <ul>
229   <li>"http://my.example.com/foo/bar"</li>
230   <li>"http://example.com/foo/"</li>
231   <li>"http://www.example.com/foo"</li>
232 </ul>
233
234 <h2 id="pi">Programmatic injection</h2>
235
236 <p>
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 &mdash;
241 for example, if you want a script to run
242 only when the user clicks a browser action's icon.
243 </p>
244
245 <p>
246 To insert code into a page,
247 your extension must have
248 <a href="xhr.html#requesting-permission">cross-origin permissions</a>
249 for the page.
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.
254 </p>
255
256 <p>
257 Once you have permissions set up,
258 you can inject JavaScript into a page by calling
259 $ref:tabs.executeScript.
260 To inject CSS, use
261 $ref:tabs.insertCSS.
262 </p>
263
264 <p>
265 The following code
266 (from the
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.
271 </p>
272
273 <pre data-filename="background.html">
274 chrome.browserAction.onClicked.addListener(function(tab) {
275   chrome.tabs.executeScript({
276     code: 'document.body.style.backgroundColor="red"'
277   }); 
278 });
279 </pre>
280 <pre data-filename="manifest.json">
281 "permissions": [
282   "activeTab"
283 ],
284 </pre>
285
286 <p>
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'.
290 The result,
291 unless the page has CSS that sets the background color,
292 is that the page turns red.
293 </p>
294
295 <p>
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:
299 </p>
300
301 <pre>chrome.tabs.executeScript(null, {file: "content_script.js"});</pre>
302
303
304 <h2 id="execution-environment">Execution environment</h2>
305
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.
307
308 <p>For example, consider this simple page:
309
310 <pre data-filename="hello.html">
311 &lt;html&gt;
312   &lt;button id="mybutton"&gt;click me&lt;/button&gt;
313   &lt;script&gt;
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 + ".");
319     }, false);
320   &lt;/script&gt;
321 &lt;/html&gt;
322 </pre>
323
324 <p>Now, suppose this content script was injected into hello.html:
325
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 + ".");
332 }, false);
333 </pre>
334
335 <p>Now, if the button is pressed, you will see both greetings.
336
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.
338
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.
340
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.
342
343 <h2 id="host-page-communication">Communication with the embedding page</h2>
344
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();
349
350 window.addEventListener("message", function(event) {
351   // We only accept messages from ourselves
352   if (event.source != window)
353     return;
354
355   if (event.data.type &amp;&amp; (event.data.type == "FROM_PAGE")) {
356     console.log("Content script received: " + event.data.text);
357     port.postMessage(event.data.text);
358   }
359 }, false);
360 </pre>
361 <pre data-filename="http://foo.com/example.html">
362 document.getElementById("theButton").addEventListener("click",
363     function() {
364   window.postMessage({ type: "FROM_PAGE", text: "Hello from the webpage!" }, "*");
365 }, false);</pre>
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>
367
368 <h2 id="security-considerations">Security considerations</h2>
369
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>
383
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 + ")")
392 </pre>
393 <pre data-filename="contentscript.js">
394 var elmt_id = ...
395 // WARNING! elmt_id might be "); ... evil script ... //"!
396 window.setTimeout("animate(" + elmt_id + ")", 200);
397 </pre>
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);
403 </pre>
404 <pre data-filename="contentscript.js">
405 var elmt_id = ...
406 // The closure form of setTimeout does not evaluate scripts.
407 window.setTimeout(function() {
408   animate(elmt_id);
409 }, 200);
410 </pre>
411
412 <h2 id="extension-files">Referring to extension files</h2>
413
414 <p>
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.
420 </p>
421
422
423 <pre>
424 <em>//Code for displaying &lt;extensionDir>/images/myimage.png:</em>
425 var imgURL = <b>chrome.extension.getURL("images/myimage.png")</b>;
426 document.getElementById("someImage").src = imgURL;
427 </pre>
428
429 <h2 id="examples"> Examples </h2>
430
431 <p>
432 You can find many
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#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.
439 </p>
440
441
442 <h2 id="videos"> Videos </h2>
443
444 <p>
445 The following videos discuss concepts that are important for content scripts.
446 The first video describes content scripts and isolated worlds.
447 </p>
448
449 <p>
450 <iframe title="YouTube video player" width="640" height="390" src="//www.youtube.com/embed/laLudeUmXHM?rel=0" frameborder="0" allowfullscreen></iframe>
451 </p>
452
453 <p>
454 The next video describes message passing,
455 featuring an example of a content script
456 sending a request to its parent extension.
457 </p>
458
459 <p>
460 <iframe title="YouTube video player" width="640" height="390" src="//www.youtube.com/embed/B4M_a7xejYI?rel=0" frameborder="0" allowfullscreen></iframe>
461 </p>