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 711303 of clipboard

  • Revision slug: Mozilla/Add-ons/SDK/High-Level_APIs/clipboard
  • Revision title: clipboard
  • Revision id: 711303
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment
Tags: 

Revision Content

Stable

Interact with the system clipboard, setting and getting its contents.

Usage

You can optionally specify the type of the data to set and retrieve. The following types are supported:

  • text (plain text)
  • html (a string of HTML)
  • image (a base-64 encoded png)

If no data type is provided, then the module will detect it for you.

Currently the image type doesn't support transparency on Windows.

Examples

Set and get the contents of the clipboard.

var clipboard = require("sdk/clipboard");
clipboard.set("Lorem ipsum dolor sit amet");
var contents = clipboard.get();

Set the clipboard contents to some HTML.

var clipboard = require("sdk/clipboard");
clipboard.set("<blink>Lorem ipsum dolor sit amet</blink>", "html");

If the clipboard contains HTML content, open it in a new tab.

var clipboard = require("sdk/clipboard");
if (clipboard.currentFlavors.indexOf("html") != -1)
  require("sdk/tabs").open("data:text/html;charset=utf-8," + clipboard.get("html"));

Set the clipboard contents to an image.

var clipboard = require("sdk/clipboard");
clipboard.set("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYA" +
              "AABzenr0AAAASUlEQVRYhe3O0QkAIAwD0eyqe3Q993AQ3cBSUKpygfsNTy" +
              "N5ugbQpK0BAADgP0BRDWXWlwEAAAAAgPsA3rzDaAAAAHgPcGrpgAnzQ2FG" +
              "bWRR9AAAAABJRU5ErkJggg%3D%3D");

If the clipboard contains an image, open it in a new tab.

var clipboard = require("sdk/clipboard");
if (clipboard.currentFlavors.indexOf("image") != -1)
  require("sdk/tabs").open(clipboard.get());

As noted before, data type can be easily omitted for images. If the intention is set the clipboard to a data URL as string and not as image, it can be done by specifying a different flavor, like text.

var clipboard = require("sdk/clipboard");
 
clipboard.set("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYA" +
              "AABzenr0AAAASUlEQVRYhe3O0QkAIAwD0eyqe3Q993AQ3cBSUKpygfsNTy" +
              "N5ugbQpK0BAADgP0BRDWXWlwEAAAAAgPsA3rzDaAAAAHgPcGrpgAnzQ2FG" +
              "bWRR9AAAAABJRU5ErkJggg%3D%3D", "text");

Globals

Functions

set(data, datatype)

Replace the contents of the user's clipboard with the provided data.

Parameters

data : string
The data to put on the clipboard.

datatype : string
The type of the data, , one of "text", "html", "image". Optional.

get(datatype)

Get the contents of the user's clipboard.

Parameters

datatype : string
Retrieve the clipboard contents only if matching this type (optional). The function will return null if the contents of the clipboard do not match the supplied type.

Properties

currentFlavors

Data on the clipboard is sometimes available in multiple types. For example, HTML data might be available as both a string of HTML (the html type) and a string of plain text (the text type). This property is an array contains all types in which the data currently on the clipboard is available.

Revision Source

<div class="note">
 <p>Stable</p>
</div>
<p><span class="seoSummary">Interact with the system clipboard, setting and getting its contents.</span></p>
<h2 id="Usage">Usage</h2>
<p>You can optionally specify the type of the data to set and retrieve. The following types are supported:</p>
<ul>
 <li><code>text</code> (plain text)</li>
 <li><code>html</code> (a string of HTML)</li>
 <li><code>image</code> (a base-64 encoded png)</li>
</ul>
<p>If no data type is provided, then the module will detect it for you.</p>
<p>Currently the <code>image</code> type doesn't support transparency on Windows.</p>
<h3 id="Examples">Examples</h3>
<p>Set and get the contents of the clipboard.</p>
<pre class="brush: js">
var clipboard = require("sdk/clipboard");
clipboard.set("Lorem ipsum dolor sit amet");
var contents = clipboard.get();</pre>
<p>Set the clipboard contents to some HTML.</p>
<pre class="brush: js">
var clipboard = require("sdk/clipboard");
clipboard.set("&lt;blink&gt;Lorem ipsum dolor sit amet&lt;/blink&gt;", "html");</pre>
<p>If the clipboard contains HTML content, open it in a new tab.</p>
<pre class="brush: js">
var clipboard = require("sdk/clipboard");
if (clipboard.currentFlavors.indexOf("html") != -1)
  require("sdk/tabs").open("data:text/html;charset=utf-8," + clipboard.get("html"));</pre>
<p>Set the clipboard contents to an image.</p>
<pre class="brush: js">
var clipboard = require("sdk/clipboard");
clipboard.set("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYA" +
              "AABzenr0AAAASUlEQVRYhe3O0QkAIAwD0eyqe3Q993AQ3cBSUKpygfsNTy" +
              "N5ugbQpK0BAADgP0BRDWXWlwEAAAAAgPsA3rzDaAAAAHgPcGrpgAnzQ2FG" +
              "bWRR9AAAAABJRU5ErkJggg%3D%3D");</pre>
<p>If the clipboard contains an image, open it in a new tab.</p>
<pre class="brush: js">
var clipboard = require("sdk/clipboard");
if (clipboard.currentFlavors.indexOf("image") != -1)
  require("sdk/tabs").open(clipboard.get());</pre>
<p>As noted before, data type can be easily omitted for images. If the intention is set the clipboard to a data URL as string and not as image, it can be done by specifying a different flavor, like <code>text</code>.</p>
<pre class="brush: js">
var clipboard = require("sdk/clipboard");
 
clipboard.set("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYA" +
              "AABzenr0AAAASUlEQVRYhe3O0QkAIAwD0eyqe3Q993AQ3cBSUKpygfsNTy" +
              "N5ugbQpK0BAADgP0BRDWXWlwEAAAAAgPsA3rzDaAAAAHgPcGrpgAnzQ2FG" +
              "bWRR9AAAAABJRU5ErkJggg%3D%3D", "text");</pre>
<h2 id="Globals">Globals</h2>
<h3 id="Functions">Functions</h3>
<h4 class="addon-sdk-api-name" id="set(data.2C_datatype)"><code>set(data, datatype)</code></h4>
<p>Replace the contents of the user's clipboard with the provided data.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>data : string</strong><br />
 The data to put on the clipboard.</p>
<p><strong>datatype : string</strong><br />
 The type of the data, , one of "text", "html", "image". Optional.</p>
<h4 class="addon-sdk-api-name" id="get(datatype)"><code>get(datatype)</code></h4>
<p>Get the contents of the user's clipboard.</p>
<h5 id="Parameters_2">Parameters</h5>
<p><strong>datatype : string</strong><br />
 Retrieve the clipboard contents only if matching this type (optional). The function will return null if the contents of the clipboard do not match the supplied type.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="currentFlavors"><code>currentFlavors</code></h4>
<p>Data on the clipboard is sometimes available in multiple types. For example, HTML data might be available as both a string of HTML (the <code>html</code> type) and a string of plain text (the <code>text</code> type). This property is an array contains all types in which the data currently on the clipboard is available.</p>
Revert to this revision