diff options
Diffstat (limited to 'files/fr/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html')
-rw-r--r-- | files/fr/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/files/fr/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html b/files/fr/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html index ea3f421968..d77c14c446 100644 --- a/files/fr/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html +++ b/files/fr/mozilla/add-ons/webextensions/api/runtime/onconnect/index.html @@ -19,7 +19,7 @@ translation_of: Mozilla/Add-ons/WebExtensions/API/runtime/onConnect <h2 id="Syntaxe">Syntaxe</h2> -<pre class="syntaxbox brush:js">browser.runtime.onConnect.addListener(listener) +<pre class="brush: js">browser.runtime.onConnect.addListener(listener) browser.runtime.onConnect.removeListener(listener) browser.runtime.onConnect.hasListener(listener) </pre> @@ -44,7 +44,7 @@ browser.runtime.onConnect.hasListener(listener) <dd> <p>Une fonction de rappel qui sera appelée lorsque cet événement se produira. La fonction recevra les arguments suivants:</p> - <dl class="reference-values"> + <dl> <dt><code>port</code></dt> <dd>Un objet {{WebExtAPIRef('runtime.Port')}} connectant le script courant à l'autre contexte auquel il se connecte.</dd> </dl> @@ -65,19 +65,19 @@ browser.runtime.onConnect.hasListener(listener) <li>Envoie des messages au script d'arrière-plan, en utilisant <code>myPort</code>, lorsque l'utilisateur clique sur le document</li> </ul> -<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// content-script.js</span> +<pre class="brush: js">// content-script.js -<span class="keyword token">var</span> myPort <span class="operator token">=</span> browser<span class="punctuation token">.</span>runtime<span class="punctuation token">.</span><span class="function token">connect</span><span class="punctuation token">(</span><span class="punctuation token">{</span>name<span class="punctuation token">:</span><span class="string token">"port-from-cs"</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> -myPort<span class="punctuation token">.</span><span class="function token">postMessage</span><span class="punctuation token">(</span><span class="punctuation token">{</span>greeting<span class="punctuation token">:</span> <span class="string token">"hello from content script"</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +var myPort = browser.runtime.connect({name:"port-from-cs"}); +myPort.postMessage({greeting: "hello from content script"}); -myPort<span class="punctuation token">.</span>onMessage<span class="punctuation token">.</span><span class="function token">addListener</span><span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span>m<span class="punctuation token">)</span> <span class="punctuation token">{</span> - console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="string token">"In content script, received message from background script: "</span><span class="punctuation token">)</span><span class="punctuation token">;</span> - console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>m<span class="punctuation token">.</span>greeting<span class="punctuation token">)</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +myPort.onMessage.addListener(function(m) { + console.log("In content script, received message from background script: "); + console.log(m.greeting); +}); -document<span class="punctuation token">.</span>body<span class="punctuation token">.</span><span class="function token">addEventListener</span><span class="punctuation token">(</span><span class="string token">"click"</span><span class="punctuation token">,</span> <span class="keyword token">function</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - myPort<span class="punctuation token">.</span><span class="function token">postMessage</span><span class="punctuation token">(</span><span class="punctuation token">{</span>greeting<span class="punctuation token">:</span> <span class="string token">"they clicked the page!"</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre> +document.body.addEventListener("click", function() { + myPort.postMessage({greeting: "they clicked the page!"}); +});</pre> <p>Le script d'arrière-plan correspondant :</p> @@ -93,28 +93,28 @@ document<span class="punctuation token">.</span>body<span class="punctuation tok <li>Envoie des messages au script de contenu, en utilisant <code>portFromCS</code>, quand l'utilisateur clique sur l'action du navigateur de l'extension</li> </ul> -<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// background-script.js</span> +<pre class="brush: js">// background-script.js -<span class="keyword token">var</span> portFromCS<span class="punctuation token">;</span> +var portFromCS; -<span class="keyword token">function</span> <span class="function token">connected</span><span class="punctuation token">(</span>p<span class="punctuation token">)</span> <span class="punctuation token">{</span> - portFromCS <span class="operator token">=</span> p<span class="punctuation token">;</span> - portFromCS<span class="punctuation token">.</span><span class="function token">postMessage</span><span class="punctuation token">(</span><span class="punctuation token">{</span>greeting<span class="punctuation token">:</span> <span class="string token">"hi there content script!"</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> - portFromCS<span class="punctuation token">.</span>onMessage<span class="punctuation token">.</span><span class="function token">addListener</span><span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span>m<span class="punctuation token">)</span> <span class="punctuation token">{</span> - console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="string token">"In background script, received message from content script"</span><span class="punctuation token">)</span> - console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>m<span class="punctuation token">.</span>greeting<span class="punctuation token">)</span><span class="punctuation token">;</span> - <span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span> +function connected(p) { + portFromCS = p; + portFromCS.postMessage({greeting: "hi there content script!"}); + portFromCS.onMessage.addListener(function(m) { + console.log("In background script, received message from content script") + console.log(m.greeting); + }); +} -browser<span class="punctuation token">.</span>runtime<span class="punctuation token">.</span>onConnect<span class="punctuation token">.</span><span class="function token">addListener</span><span class="punctuation token">(</span>connected<span class="punctuation token">)</span><span class="punctuation token">;</span> +browser.runtime.onConnect.addListener(connected); -browser<span class="punctuation token">.</span>browserAction<span class="punctuation token">.</span>onClicked<span class="punctuation token">.</span><span class="function token">addListener</span><span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - portFromCS<span class="punctuation token">.</span><span class="function token">postMessage</span><span class="punctuation token">(</span><span class="punctuation token">{</span>greeting<span class="punctuation token">:</span> <span class="string token">"they clicked the button!"</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre> +browser.browserAction.onClicked.addListener(function() { + portFromCS.postMessage({greeting: "they clicked the button!"}); +});</pre> <p>{{WebExtExamples}}</p> -<div class="note"><strong>Remerciements :</strong> +<div class="note"><p><strong>Note :</strong></p> <p>Cette API est basée sur l'API Chromium <a href="https://developer.chrome.com/extensions/runtime#event-onConnect"><code>chrome.runtime</code></a>. Cette documentation est dérivée de <a href="https://chromium.googlesource.com/chromium/src/+/master/extensions/common/api/runtime.json"><code>runtime.json</code></a> dans le code de Chromium code.</p> |