Exception
Exception

Reputation: 8389

Assign Clipboard data to a local variable with Firefox API

How can I assign Clipboard text to a local variable using XPCOM. I need only to be worked in Firefox. Please suggest me any plugin or events to use. I have googled, but unable to find the article which matches my requirement. I heard something about

nsIClipboardCommands

but need assistance on how to use it.. And can I use this method in a web page?

Upvotes: 1

Views: 1329

Answers (2)

Steel.Liao
Steel.Liao

Reputation: 196

Maybe you can use zeroclipboard to achieve this.Or you can use these code:

function copyToClipboard(txt) {
    if (window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if (navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert("please goto ’about:config’\n and set ’signed.applets.codebase_principal_support’ to ’true’");
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip)
            return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans)
            return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode", str, copytext.length * 2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip)
            return false;
        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
    return true;
}

Upvotes: 2

pradeek
pradeek

Reputation: 22115

See this : Using the Clipboard

Upvotes: 2

Related Questions