Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Darren Bowen on May 23, 2007 12:54
open dhtmlx forum
Firefox Copying To Clipboard Protection

Hello I was trying to get copying and pasting working in Firefox 2 but couldn't get it to work, I get the error

Clipboard is not defined dhtmlXGrid_nxml.js (line 196)


I tried the usual tricks to enable clipboard, including setting, in about:config
signed.applets.codebase_principal_support = true

And the following in user.js:
user_pref("capability.policy.policynames", "allowclipboard");
user_pref("capability.policy.allowclipboard.sites", "scbr.com");
user_pref("capability.policy.allowclipboard.Clipboard.cutcopy", "allAccess");
user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess");

But the error still occurs.


I have been looking around this area and I found an alternative solution that works well in Firefox, there are some details here:

http://developer.mozilla.org/en/docs/Using_the_Clipboard


I modified the copy and paste methods in dhtmlXGrid_nxml.js as follows and now copying/pasting works in Firefox and better still it pops up a security dialogue asking whether or not you want to allow the site access to the clipboard, so no need to fiddle with all the config options.

------------------------------------------------------------------------------------------------------------------------------------------------

dhtmlXGridObject.prototype.toClipBoard=function(val){

    if (window.clipboardData) {
        window.clipboardData.setData("Text",val);
        
    }
    else {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
        
        var copytext = val;
        
        var str = Components.classes["@mozilla.org/supports-string;1"].
                             createInstance(Components.interfaces.nsISupportsString);
        if (!str) return false;
        
        str.data = copytext;
        
        var trans = Components.classes["@mozilla.org/widget/transferable;1"].
                             createInstance(Components.interfaces.nsITransferable);
        if (!trans) return false;
        
        trans.addDataFlavor("text/unicode");
        trans.setTransferData("text/unicode", str, copytext.length * 2);
        
        var clipid = Components.interfaces.nsIClipboard;
        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
        if (!clip) return false;
        
        clip.setData(trans, null, clipid.kGlobalClipboard);
        
    }


};

//------------------------------------------------------------------------------------------------------------------------------------------------

dhtmlXGridObject.prototype.fromClipBoard=function(){
    if (window.clipboardData) {
        return window.clipboardData.getData("Text")
    
    }
    else{
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
        if (!clip) return false;
        
        var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return false;
        trans.addDataFlavor("text/unicode");
        
        clip.getData(trans, clip.kGlobalClipboard);
        
        var str = new Object();
        var strLength = new Object();
        
        trans.getTransferData("text/unicode", str, strLength);
            
            
        if (str) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
        if (str) pastetext = str.data.substring(0, strLength.value / 2);
        
        return pastetext;

    }

};
Answer posted on May 24, 2007 19:27
Thanks for your code propositions, I incorporated this logic in main code branch.
Updated file for dhtmlxGrid 1.3 attached.

Attachments (1)
Answer posted by Stanislav (Support) on Nov 29, 2014 23:47

I hope this information will be enough for you. But you also can have a look at calendar date picker javascript and mso-list.