| OLD | NEW |
| 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>Content Scripts are JavaScript files that run in the context of web pages. By
using the standard <a href="http://www.w3.org/TR/DOM-Level-2-HTML/">Document Ob
ject Model</a> (DOM), they can read details of the web pages the browser visits,
or make changes to them. | 4 <p>Content Scripts are JavaScript files that run in the context of web pages. By
using the standard <a href="http://www.w3.org/TR/DOM-Level-2-HTML/">Document Ob
ject Model</a> (DOM), they can read details of the web pages the browser visits,
or make changes to them. |
| 5 | 5 |
| 6 <p>Some examples of things that content scripts can do include: | 6 <p>Some examples of things that content scripts can do include: |
| 7 | 7 |
| 8 <ul> | 8 <ul> |
| 9 <li>Find unlinked URLs in web pages and convert them into hyperlinks | 9 <li>Find unlinked URLs in web pages and convert them into hyperlinks |
| 10 <li>Increase the font size to make text more legible | 10 <li>Increase the font size to make text more legible |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 ================ | 117 ================ |
| 118 var e = document.getElementById("login"); | 118 var e = document.getElementById("login"); |
| 119 | 119 |
| 120 // Create a short-lived named channel to the extension and send a single | 120 // Create a short-lived named channel to the extension and send a single |
| 121 // message through it. | 121 // message through it. |
| 122 var port = chrome.extension.connect({name: "notifyChannel"}); | 122 var port = chrome.extension.connect({name: "notifyChannel"}); |
| 123 port.postMessage({found: (e != undefined)}); | 123 port.postMessage({found: (e != undefined)}); |
| 124 | 124 |
| 125 // Also listen for new channels from the extension for when the button is | 125 // Also listen for new channels from the extension for when the button is |
| 126 // pressed. | 126 // pressed. |
| 127 chrome.extension.onConnect.addListener(function(port, name) { | 127 chrome.extension.onConnect.addListener(function(port) { |
| 128 console.assert(name == "buttonClickedChannel"); | 128 console.assert(port.name == "buttonClickedChannel"); |
| 129 port.onMessage.addListener(function(msg) { | 129 port.onMessage.addListener(function(msg) { |
| 130 if (msg.buttonClicked) { | 130 if (msg.buttonClicked) { |
| 131 e.value = msg.passwordValue; | 131 e.value = msg.passwordValue; |
| 132 } | 132 } |
| 133 }); | 133 }); |
| 134 }); | 134 }); |
| 135 </pre> | 135 </pre> |
| 136 | 136 |
| 137 <p>with a toolstrip that looks like: | 137 <p>with a toolstrip that looks like: |
| 138 | 138 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 167 <p>Although the execution environments of content scripts and the pages that hos
t 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 t
he content script), it must do so through the shared DOM.</p> | 167 <p>Although the execution environments of content scripts and the pages that hos
t 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 t
he content script), it must do so through the shared DOM.</p> |
| 168 | 168 |
| 169 <p>An example can be accomplished using custom DOM events and storing data in a
known location. Consider: </p> | 169 <p>An example can be accomplished using custom DOM events and storing data in a
known location. Consider: </p> |
| 170 | 170 |
| 171 <pre>http://foo.com/example.html | 171 <pre>http://foo.com/example.html |
| 172 ================================ | 172 ================================ |
| 173 var customEvent = document.createEvent('Event'); | 173 var customEvent = document.createEvent('Event'); |
| 174 customEvent.initEvent('myCustomEvent', true, true); | 174 customEvent.initEvent('myCustomEvent', true, true); |
| 175 | 175 |
| 176 function fireCustomEvent(data) { | 176 function fireCustomEvent(data) { |
| 177 hidenDiv = document.getElementById('myCustomEventDiv'); | 177 hiddenDiv = document.getElementById('myCustomEventDiv'); |
| 178 hidenDiv.innerHTML = data | 178 hiddenDiv.innerHTML = data |
| 179 hidenDiv.dispatchEvent(customEvent); | 179 hiddenDiv.dispatchEvent(customEvent); |
| 180 }</pre> | 180 }</pre> |
| 181 | 181 |
| 182 <pre>contentscript.js | 182 <pre>contentscript.js |
| 183 ===================== | 183 ===================== |
| 184 var port = chrome.extension.connect(); | 184 var port = chrome.extension.connect(); |
| 185 | 185 |
| 186 document.getElementById('myCustomEventDiv').addEventListener('myCustomEvent', fu
nction() { | 186 document.getElementById('myCustomEventDiv').addEventListener('myCustomEvent', fu
nction() { |
| 187 var eventData = document.getElementById('myCustomEventDiv').innerHTML; | 187 var eventData = document.getElementById('myCustomEventDiv').innerHTML; |
| 188 port.postMessage({message: "myCustomEvent", values: eventData}); | 188 port.postMessage({message: "myCustomEvent", values: eventData}); |
| 189 });</pre> | 189 });</pre> |
| 190 | 190 |
| 191 <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> | 191 <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> |
| 192 | 192 |
| 193 <h2 id="extension-files">Referring to extension files</h2> | 193 <h2 id="extension-files">Referring to extension files</h2> |
| 194 | 194 |
| 195 <p> | 195 <p> |
| 196 Get the URL of an extension's file using | 196 Get the URL of an extension's file using |
| 197 <code>chrome.extension.getURL()</code>. | 197 <code>chrome.extension.getURL()</code>. |
| 198 You can use the result | 198 You can use the result |
| 199 just like you would any other URL, | 199 just like you would any other URL, |
| 200 as the following code shows. | 200 as the following code shows. |
| 201 </p> | 201 </p> |
| 202 | 202 |
| 203 | 203 |
| 204 <pre> | 204 <pre> |
| 205 <em>//Code for displaying <extensionDir>/images/myimage.png:</em> | 205 <em>//Code for displaying <extensionDir>/images/myimage.png:</em> |
| 206 var imgURL = <b>chrome.extension.getURL("images/myimage.png")</b>; | 206 var imgURL = <b>chrome.extension.getURL("images/myimage.png")</b>; |
| 207 document.getElementById("someImage").src = imgURL; | 207 document.getElementById("someImage").src = imgURL; |
| 208 </pre> | 208 </pre> |
| OLD | NEW |