tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

clipboard.js (1495B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // Helpers for clipboard handling.
      6 
      7 "use strict";
      8 
      9 const clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
     10  Ci.nsIClipboardHelper
     11 );
     12 
     13 function copyString(string) {
     14  clipboardHelper.copyString(string);
     15 }
     16 
     17 /**
     18 * Retrieve the current clipboard data matching the flavor "text/plain".
     19 *
     20 * @return {string} Clipboard text content, null if no text clipboard data is available.
     21 */
     22 function getText() {
     23  const flavor = "text/plain";
     24 
     25  const xferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
     26    Ci.nsITransferable
     27  );
     28 
     29  if (!xferable) {
     30    throw new Error(
     31      "Couldn't get the clipboard data due to an internal error " +
     32        "(couldn't create a Transferable object)."
     33    );
     34  }
     35 
     36  xferable.init(null);
     37  xferable.addDataFlavor(flavor);
     38 
     39  // Get the data into our transferable.
     40  Services.clipboard.getData(xferable, Services.clipboard.kGlobalClipboard);
     41 
     42  const data = {};
     43  try {
     44    xferable.getTransferData(flavor, data);
     45  } catch (e) {
     46    // Clipboard doesn't contain data in flavor, return null.
     47    return null;
     48  }
     49 
     50  // There's no data available, return.
     51  if (!data.value) {
     52    return null;
     53  }
     54 
     55  return data.value.QueryInterface(Ci.nsISupportsString).data;
     56 }
     57 
     58 exports.copyString = copyString;
     59 exports.getText = getText;