aboutsummaryrefslogtreecommitdiff
path: root/files/fr/mozilla/add-ons/webextensions/api/commands/oncommand/index.html
blob: 85c98b3207ad0149ada8f031ac0c1d076d299f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
title: onCommand
slug: Mozilla/Add-ons/WebExtensions/API/commands/onCommand
tags:
  - API
  - Add-ons
  - Event
  - Extensions
  - Non-standard
  - Reference
  - WebExtensions
  - commands
  - onCommand
translation_of: Mozilla/Add-ons/WebExtensions/API/commands/onCommand
---
<div>{{AddonSidebar()}}</div>

<div>Lancer quand une commande est exécutée à l'aide de son raccourci clavier associé.</div>

<div></div>

<div>L'écouteur reçoit  le nom de la commande. Cela correspond au nom donnée à la commande dans une  <a href="/fr/Add-ons/WebExtensions/manifest.json/commands">entrée manifest.json</a>.</div>

<h2 id="Syntaxe">Syntaxe</h2>

<pre class="syntaxbox brush:js">browser.commands.onCommand.addListener(listener)
browser.commands.onCommand.removeListener(listener)
browser.commands.onCommand.hasListener(listener)
</pre>

<p>Les événements ont trois fonctions :</p>

<dl>
 <dt><code>addListener(callback)</code></dt>
 <dd>Ajoute un écouteur à un événement.</dd>
 <dt><code>removeListener(listener)</code></dt>
 <dd>Arrêter d'écouter un événement. L'arguement <code>listener</code> est l'écouteur à supprimer.</dd>
 <dt><code>hasListener(listener)</code></dt>
 <dd>Vérifiez si <code>listener</code> est enregistré pour cet événement . Renvoie <code>true</code> s'il écoute, <code>false</code> sinon.</dd>
</dl>

<h2 id="Syntaxe_addListener">Syntaxe addListener</h2>

<h3 id="Paramètre">Paramètre</h3>

<dl>
 <dt><code>callback</code></dt>
 <dd>
 <p>Fonction qui sera appelée lorsqu'un utilisateur entre dans le raccourci de la commande. La fonction recevra les arguments suivants :</p>

 <dl class="reference-values">
  <dt><code>name</code></dt>
  <dd><code>string</code>. Nom de la commande. Cela correspond au nom donné à la commande dans son <a href="/fr/Add-ons/WebExtensions/manifest.json/commands">entrée manifest.json</a>.</dd>
 </dl>
 </dd>
</dl>

<h2 id="Compatibilité_du_navigateur">Compatibilité du navigateur</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("webextensions.api.commands.onCommand")}}</p>

<h2 id="Exemples">Exemples</h2>

<div>Etant donnée une entrée manifest.json comme ceci :</div>

<div></div>

<div>
<pre class="brush: json line-numbers language-json"><code class="language-json"><span class="key token">"commands":</span> <span class="punctuation token">{</span>
  <span class="key token">"toggle-feature":</span> <span class="punctuation token">{</span>
    <span class="key token">"suggested_key":</span> <span class="punctuation token">{</span>
      <span class="key token">"default":</span> <span class="string token">"Ctrl+Shift+Y"</span>
    <span class="punctuation token">}</span><span class="punctuation token">,</span>
    <span class="key token">"description":</span> <span class="string token">"Send a 'toggle-feature' event"</span>
  <span class="punctuation token">}</span>
<span class="punctuation token">}</span></code></pre>
</div>

<div>Vous pouvez écouter cette commande particulière comme ceci :</div>

<div></div>

<div>
<pre class="brush: js line-numbers language-js"><code class="language-js">browser<span class="punctuation token">.</span>commands<span class="punctuation token">.</span>onCommand<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>command<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  <span class="keyword token">if</span> <span class="punctuation token">(</span>command <span class="operator token">==</span> <span class="string token">"toggle-feature"</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><span class="string token">"toggling the feature!"</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>
</div>

<p>{{WebExtExamples}}</p>

<div class="note"><strong>Remerciements :</strong>

<p>Cette API est basée sur l'API Chromium <a href="https://developer.chrome.com/extensions/commands"><code>chrome.commands</code></a>.</p>
</div>