Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(720)

Side by Side Diff: chrome/common/extensions/docs/static/content_scripts.html

Issue 437001: Docs polish (Closed)
Patch Set: Add an NPAPI warning Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 <div id="pageData-title" class="pageData">Content Scripts</div> 1 <div id="pageData-title" class="pageData">Content Scripts</div>
2 <div id="pageData-showTOC" class="pageData">true</div> 2 <div id="pageData-showTOC" class="pageData">true</div>
3 3
4 <p> 4 <p>
5 Content scripts are JavaScript files that run in the context of web pages. 5 Content scripts are JavaScript files that run in the context of web pages.
6 By using the standard 6 By using the standard
7 <a href="http://www.w3.org/TR/DOM-Level-2-HTML/">Document 7 <a href="http://www.w3.org/TR/DOM-Level-2-HTML/">Document
8 Object Model</a> (DOM), 8 Object Model</a> (DOM),
9 they can read details of the web pages the browser visits, 9 they can read details of the web pages the browser visits,
10 or make changes to them. 10 or make changes to them.
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 129
130 <h2 id="execution-environment">Execution environment</h2> 130 <h2 id="execution-environment">Execution environment</h2>
131 131
132 <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 t o any JavaScript variables or functions created by the page. It looks to each co ntent script as if there is no other JavaScript executing on the page it is runn ing on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts. 132 <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 t o any JavaScript variables or functions created by the page. It looks to each co ntent script as if there is no other JavaScript executing on the page it is runn ing on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.
133 133
134 <p>For example, consider this simple page: 134 <p>For example, consider this simple page:
135 135
136 <pre>hello.html 136 <pre>hello.html
137 ========== 137 ==========
138 &lt;html&gt; 138 &lt;html&gt;
139 &lt;button id="button"&gt;click me&lt;/button&gt; 139 &lt;button id="mybutton"&gt;click me&lt;/button&gt;
140 &lt;script&gt; 140 &lt;script&gt;
141 var greeting = "hello!"; 141 var greeting = "hello, ";
142 function sayGreeting() { 142 var button = document.getElementById("mybutton");
143 alert(greeting); 143 button.person_name = "Bob";
144 } 144 button.addEventListener("click", function() {
145 document.getElementById("button").onclick = sayGreeting; 145 alert(greeting + button.person_name + ".");
146 }, false);
146 &lt;/script&gt; 147 &lt;/script&gt;
147 &lt;/html&gt;</pre> 148 &lt;/html&gt;</pre>
148 149
149 <p>Now, suppose this content script was injected into hello.html: 150 <p>Now, suppose this content script was injected into hello.html:
150 151
151 <pre>contentscript.js 152 <pre>contentscript.js
152 ================ 153 ================
153 console.log(greeting); // undefined 154 var greeting = "hola, ";
154 console.log(sayGreeting); // undefined 155 var button = document.getElementById("mybutton");
155 console.log(document.getElementById("button").onclick); // still undefined 156 button.person_name = "Roberto";
156 document.getElementById("button").onclick = function() { 157 button.addEventListener("click", function() {
157 alert("hola!"); 158 alert(greeting + button.person_name + ".");
158 }</pre> 159 }, false);
160 </pre>
159 161
160 <p>Now, if the button is pressed, you will see both greetings. 162 <p>Now, if the button is pressed, you will see both greetings.
161 163
162 <p>Isolated worlds allow each content script to make changes to its JavaScript e nvironment without worrying about conflicting with the page or with other conten t scripts. For example, a content script could include JQuery v1 and the page co uld include JQuery v2, and they wouldn't conflict with each other. 164 <p>Isolated worlds allow each content script to make changes to its JavaScript e nvironment without worrying about conflicting with the page or with other conten t scripts. For example, a content script could include JQuery v1 and the page co uld include JQuery v2, and they wouldn't conflict with each other.
163 165
164 <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. 166 <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.
165 167
166 168
167 <h2 id="host-page-communication">Communication with the embedding page</h2> 169 <h2 id="host-page-communication">Communication with the embedding page</h2>
168 170
(...skipping 18 matching lines...) Expand all
187 189
188 document.getElementById('myCustomEventDiv').addEventListener('myCustomEvent', fu nction() { 190 document.getElementById('myCustomEventDiv').addEventListener('myCustomEvent', fu nction() {
189 var eventData = document.getElementById('myCustomEventDiv').innerText; 191 var eventData = document.getElementById('myCustomEventDiv').innerText;
190 port.postMessage({message: "myCustomEvent", values: eventData}); 192 port.postMessage({message: "myCustomEvent", values: eventData});
191 });</pre> 193 });</pre>
192 194
193 <p>In the above example, example.html (which is not a part of the extension) cre ates a custom event and then can decide to fire the event by setting the event d ata to a known location in the DOM and then dispatching the custom event. The co ntent script listens for the name of the custom event on the known element and h andles the event by inspecting the data of the element, and turning around to po st the message to the extension process. In this way the page establishes a line of communication to the extension. The reverse is possible through similar mean s.</p> 195 <p>In the above example, example.html (which is not a part of the extension) cre ates a custom event and then can decide to fire the event by setting the event d ata to a known location in the DOM and then dispatching the custom event. The co ntent script listens for the name of the custom event on the known element and h andles the event by inspecting the data of the element, and turning around to po st the message to the extension process. In this way the page establishes a line of communication to the extension. The reverse is possible through similar mean s.</p>
194 196
195 <h2 id="security-considerations">Security considerations</h2> 197 <h2 id="security-considerations">Security considerations</h2>
196 198
197 When writing a content script, you should be aware of two security issues. 199 <p>When writing a content script, you should be aware of two security issues.
198 First, be careful not to introduce security vulnerabilities into the web site 200 First, be careful not to introduce security vulnerabilities into the web site
199 your content script is injected into. For example, if your content script 201 your content script is injected into. For example, if your content script
200 receives content from another web site (e.g., by <a 202 receives content from another web site (e.g., by <a
201 href="messaging.html">asking your background page to make an 203 href="messaging.html">asking your background page to make an
202 XMLHttpRequest</a>), be careful to filter that content for <a 204 XMLHttpRequest</a>), be careful to filter that content for <a
203 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site 205 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site
204 scripting</a> attacks before injecting the content into the current page. 206 scripting</a> attacks before injecting the content into the current page.
205 For example, prefer to inject content via innerText rather than innerHTML. 207 For example, prefer to inject content via innerText rather than innerHTML.
206 Be especially careful when retrieving HTTP content on an HTTPS page because 208 Be especially careful when retrieving HTTP content on an HTTPS page because
207 the HTTP content might have been corrupted by a network <a 209 the HTTP content might have been corrupted by a network <a
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 just like you would any other URL, 251 just like you would any other URL,
250 as the following code shows. 252 as the following code shows.
251 </p> 253 </p>
252 254
253 255
254 <pre> 256 <pre>
255 <em>//Code for displaying &lt;extensionDir>/images/myimage.png:</em> 257 <em>//Code for displaying &lt;extensionDir>/images/myimage.png:</em>
256 var imgURL = <b>chrome.extension.getURL("images/myimage.png")</b>; 258 var imgURL = <b>chrome.extension.getURL("images/myimage.png")</b>;
257 document.getElementById("someImage").src = imgURL; 259 document.getElementById("someImage").src = imgURL;
258 </pre> 260 </pre>
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/static/browserAction.html ('k') | chrome/common/extensions/docs/static/getstarted.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698