Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1132631 of Interact with the clipboard

  • Revision slug: Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard
  • Revision title: Interact with the clipboard
  • Revision id: 1132631
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment

Revision Content

WebExtensions can interact with the system clipboard using document.execCommand():

* document.execCommand("copy") copies the current selection to the clipboard.
* document.execCommand("cut") cuts the current selection to the clipboard.
* document.execCommand("paste") pastes the clipboard contents at the insertion point.

Writing to the clipboard

You can use the "cut" and "copy" commands without any special permission if you are using them in a short-lived event handler for a user action (for example, a click handler).

For example, suppose you've got a popup that includes the following HTML:

<input id="input" type="text"/>
<button id="copy">Copy</button>
<br/>

To make the "copy" button copy the contents of "input", you can use code like this:

function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("Copy");
}

document.querySelector("#copy").addEventListener("click", copy);

Because the execCommand() call is inside a click event handler, you don't need any special permissions here.

However, let's say that instead, you trigger the copy from an alarm:

function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("Copy");
}

chrome.alarms.create({
  delayInMinutes: 0.1
});

chrome.alarms.onAlarm.addListener(copy);

Depending on the browser, this may not work. On Firefox, it will not work, and you'll see a message like this in your console:

"document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler."

To enable this use case, you need to ask for the "clipboardWrite" permission. So: "clipboardWrite" enables you to write to the clipboard outside a short-lived event handler for a user action.

Browser-specific considerations

In Google Chrome:

  • you can write to the clipboard like this in all execution contexts - background pages, content scripts, options pages, and popups.
  • you don't actually need "clipboardWrite", even to write to the clipboard outside a user-generated event handler.

In Firefox:

  • you can write to the clipboard like this in all execution contexts except background pages. In Firefox you can't select text or focus an input field in background pages, so you can't write to the clipboard from a background page.
  • the "clipboardWrite" permission is only supported from version 51 onwards.

Reading from the clipboard

To use the "paste" command, you must have the "clipboardRead" permission. For example, suppose your HTML includes something like this:

<input id="output" type="text"/>
<button id="paste">Paste</button>
<br/>

To set the content of "output" from the clipboard when the user clicks "paste", you can use code like this:

function paste() {
  var pasteText = document.querySelector("#output");
  pasteText.focus();
  document.execCommand("Paste");
}

document.querySelector("#paste").addEventListener("click", paste);

This will require "clipboardRead", even though it's in a user-generated event handler.

Browser-specific considerations

Firefox doesn't yet support "clip

Revision Source

<p>WebExtensions can interact with the system clipboard using <code><a href="/en-US/docs/Web/API/Document/execCommand">document.execCommand()</a></code>:</p>

<p>* <code>document.execCommand("copy")</code> copies the current selection to the clipboard.<br />
 * <code>document.execCommand("cut")</code> cuts the current selection to the clipboard.<br />
 * <code>document.execCommand("paste")</code> pastes the clipboard contents at the insertion point.</p>

<h2>Writing to the clipboard</h2>

<p>You can use the "cut" and "copy" commands without any special permission if you are using them in a short-lived event handler for a user action (for example, a click handler).</p>

<p>For example, suppose you've got a popup that includes the following HTML:</p>

<pre class="brush: html">
&lt;input id="input" type="text"/&gt;
&lt;button id="copy"&gt;Copy&lt;/button&gt;
&lt;br/&gt;</pre>

<p>To make the "copy" button copy the contents of "input", you can use code like this:</p>

<pre class="brush: js">
function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("Copy");
}

document.querySelector("#copy").addEventListener("click", copy);</pre>

<p>Because the <code>execCommand()</code> call is inside a click event handler, you don't need any special permissions here.</p>

<p>However, let's say that instead, you trigger the copy from an alarm:</p>

<pre class="brush: js">
function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("Copy");
}

chrome.alarms.create({
  delayInMinutes: 0.1
});

chrome.alarms.onAlarm.addListener(copy);</pre>

<p>Depending on the browser, this may not work. On Firefox, it will not work, and you'll see a message like this in your console:</p>

<pre>
"document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler."</pre>

<p>To enable this use case, you need to ask for the "clipboardWrite" permission. So: "clipboardWrite" enables you to write to the clipboard outside a short-lived event handler for a user action.</p>

<h3>Browser-specific considerations</h3>

<p>In Google Chrome:</p>

<ul>
 <li>you can write to the clipboard like this in all execution contexts - background pages, content scripts, options pages, and popups.</li>
 <li>you don't actually need "clipboardWrite", even to write to the clipboard outside a user-generated event handler.</li>
</ul>

<p>In Firefox:</p>

<ul>
 <li>you can write to the clipboard like this in all execution contexts <em>except background pages</em>. In Firefox you can't select text or focus an input field in background pages, so you can't write to the clipboard from a background page.</li>
 <li>the "clipboardWrite" permission is only supported from version 51 onwards.</li>
</ul>

<h2>Reading from the clipboard</h2>

<p>To use the "paste" command, you must have the "clipboardRead" permission. For example, suppose your HTML includes something like this:</p>

<pre class="brush: html">
&lt;input id="output" type="text"/&gt;
&lt;button id="paste"&gt;Paste&lt;/button&gt;
&lt;br/&gt;</pre>

<p>To set the content of "output" from the clipboard when the user clicks "paste", you can use code like this:</p>

<pre class="brush: js">
function paste() {
  var pasteText = document.querySelector("#output");
  pasteText.focus();
  document.execCommand("Paste");
}

document.querySelector("#paste").addEventListener("click", paste);</pre>

<p>This will require "clipboardRead", even though it's in a user-generated event handler.</p>

<h3>Browser-specific considerations</h3>

<p>Firefox doesn't yet support "clip</p>
Revert to this revision