aboutsummaryrefslogtreecommitdiffstats
path: root/lib/clipboard.js
blob: 1870e9fddab278af0d9c48ca3089e4ea0186796d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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
 * 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;
};

var flavorsMethod = exports.getCurrentFlavors = function flavorsMethod(test) {
    // 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 flavorArray = [];
    var clip = getClipboard();

    for (var flavor in possibleTypes) {
        var presentFlavor = clip.hasDataMatchingFlavors(
            [flavor],
            1,
            clip.kGlobalClipboard
        );
        if (presentFlavor) {
            flavorArray.push(possibleTypes[flavor]);
        }
    }
    return flavorArray;
};