nsClipboard.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * nsClipboard - wrapper around nsIClipboard and nsITransferable
  7. * that simplifies access to the clipboard.
  8. **/
  9. var nsClipboard = {
  10. _CB: null,
  11. get mClipboard()
  12. {
  13. if (!this._CB)
  14. {
  15. const kCBContractID = "@mozilla.org/widget/clipboard;1";
  16. const kCBIID = Components.interfaces.nsIClipboard;
  17. this._CB = Components.classes[kCBContractID].getService(kCBIID);
  18. }
  19. return this._CB;
  20. },
  21. currentClipboard: null,
  22. /**
  23. * Array/Object read (Object aFlavourList, long aClipboard, Bool aAnyFlag) ;
  24. *
  25. * returns the data in the clipboard
  26. *
  27. * @param FlavourSet aFlavourSet
  28. * formatted list of desired flavours
  29. * @param long aClipboard
  30. * the clipboard to read data from (kSelectionClipboard/kGlobalClipboard)
  31. * @param Bool aAnyFlag
  32. * should be false.
  33. **/
  34. read: function (aFlavourList, aClipboard, aAnyFlag)
  35. {
  36. this.currentClipboard = aClipboard;
  37. var data = nsTransferable.get(aFlavourList, this.getClipboardTransferable, aAnyFlag);
  38. return data.first.first; // only support one item
  39. },
  40. /**
  41. * nsISupportsArray getClipboardTransferable (Object aFlavourList) ;
  42. *
  43. * returns a nsISupportsArray of the item on the clipboard
  44. *
  45. * @param Object aFlavourList
  46. * formatted list of desired flavours.
  47. **/
  48. getClipboardTransferable: function (aFlavourList)
  49. {
  50. const supportsContractID = "@mozilla.org/supports-array;1";
  51. const supportsIID = Components.interfaces.nsISupportsArray;
  52. var supportsArray = Components.classes[supportsContractID].createInstance(supportsIID);
  53. var trans = nsTransferable.createTransferable();
  54. for (var flavour in aFlavourList)
  55. trans.addDataFlavor(flavour);
  56. nsClipboard.mClipboard.getData(trans, nsClipboard.currentClipboard)
  57. supportsArray.AppendElement(trans);
  58. return supportsArray;
  59. }
  60. };