From 67bdce8805063717bfdbc449e5d8974f32f288a2 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Mon, 7 Jun 2010 15:04:02 +0200 Subject: The first attempt to make JEP-10/Clipboard working. --- lib/clipboard.js | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/rhbzpage.js | 5 ++- 2 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 lib/clipboard.js (limited to 'lib') diff --git a/lib/clipboard.js b/lib/clipboard.js new file mode 100644 index 0000000..b87c477 --- /dev/null +++ b/lib/clipboard.js @@ -0,0 +1,129 @@ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php + +/** + * returns content of the system clipboard + * @return string with the content of the clipboard or "" if empty. + * originally from + * https://developer.mozilla.org/en/Using_the_Clipboard + * + */ +// TODO to-be-replaced by +// var contents = jetpack.clipboard.get(); +// http://hsivonen.iki.fi/kesakoodi/clipboard/ +// https://wiki.mozilla.org/Labs/Jetpack/JEP/10 + +function getClipboard() { + var clip = Cc["@mozilla.org/widget/clipboard;1"]. + getService(Ci.nsIClipboard); + if (!clip) { + throw new Error("No access to the clipboard!"); + } + return clip; +} + +function createTransferable() { + var trans = Cc["@mozilla.org/widget/transferable;1"]. + createInstance(Ci.nsITransferable); + if (!trans) { + throw new Error("No access to the transfer object during the set of clipboard!"); + } + return trans; +} + +var getMethod = exports.get = function getMethod( flavor ) { + var pastetext = "", mimeType = "", stuff = {}; + var len = 0, clipId = 0, clip = {}, trans = {}; + + // flavor argument is optional + if (flavor === undefined) { + flavor = "plain"; + } + + if (flavor === "plain") { + mimeType = "text/unicode"; + } else if (favor === "html") { + mimeType = "text/html"; + } else { + throw new Error("Unsupported flavor '" + flavor + "'!"); + } + + clip = getClipboard(); + + trans = createTransferable(); + + trans.addDataFlavor(mimeType); + clip.getData(trans, clip.kGlobalClipboard); + + var str = {}; + var strLength = {}; + + trans.getTransferData(mimeType, str, strLength); + + if (str) { + str = str.value.QueryInterface(Ci.nsISupportsString); + pastetext = str.data.substring(0, strLength.value / 2); + } + return pastetext; +}; + +var setMethod = exports.set = function setMethod(content, flavor) { + var mimeType = "", stuff = {}; + var len = 0, clipId = 0, clip = {}, trans = {}; + + // flavor argument is optional + if (flavor === undefined) { + flavor = "plain"; + } + + if (flavor === "plain") { + mimeType = "text/unicode"; + } else if (favor === "html") { + mimeType = "text/html"; + } else { + throw new Error("Unsupported flavor '" + flavor + "'!"); + } + + stuff = Cc["@mozilla.org/supports-string;1"]. + createInstance(Ci.nsISupportsString); + if (!stuff) { + return false; + } + stuff.data = content; + len = content.length * 2; + + clip = getClipboard(); + + trans = createTransferable(); + + trans.addDataFlavor(mimeType); + trans.setTransferData(mimeType, stuff, content.length * 2); + + clip.setData(trans, null, clip.kGlobalClipboard); + return true; +}; + +function createSupportsWString() { + return Cc["@mozilla.org/supports-wstring;1"]. + createInstance(Ci.nsISupportsWString); +} + +var flavorsMethod = exports.getCurrentFlavors = function flavorsMethod() { + // currently the only possible flavors in Jetpack-prototype are "plain" and + // "html", i.e., "text/plain" (or text/unicode?) and "text/html" (or + // application/xml+xhtml?) + var possibleTypes = { + "text/unicode": "plain", + "text/plain": "plain", + "text/html": "html" + }; + var flavourArray = []; + + for (mime in possibleTypes) { + var kSuppString = createSupportsWString(); + kSuppString.data = mime; + // FIXME Missing some kind of if (mime in flavors) ... + flavourArray.add(possibleTypes[mime]); + } + return flavourArray; +}; diff --git a/lib/rhbzpage.js b/lib/rhbzpage.js index c885699..c9d9b9f 100644 --- a/lib/rhbzpage.js +++ b/lib/rhbzpage.js @@ -6,6 +6,7 @@ var util = require("util"); var xrpc = require("xmlrpc"); var xhr = require("xhr"); +var clip = require("clipboard"); // var TriagedDistro = 13; // var NumberOfFrames = 7; // var XMLRPCurl = "https://bugzilla.redhat.com/xmlrpc.cgi"; @@ -589,7 +590,7 @@ RHBugzillaPage.prototype.queryForSelection = function() { let text = jetpack.selection.text; console.log("selection = " + text); if (!text) { - text = jetpack.clipboard.get(); + text = clip.get(); console.log("clipboard = " + text); } console.log("text = " + text); @@ -604,7 +605,7 @@ RHBugzillaPage.prototype.queryForSelection = function() { RHBugzillaPage.prototype.queryUpstream = function() { let text = jetpack.selection.text; if (!text) { - text = jetpack.clipboard.get(); + text = clip.get(); } if (text) { let text = encodeURIComponent(text.trim()); -- cgit