Document Information

Last modified:
2008/02/14 00:50 by c.boulanger

Communicating with the Operating System Clipboard

Here is a drop-in method for your classes to copy data to the Clipboard of your operating system.

There are many things that are missing:

  • This should be made part of a real clipboard manager class
  • So far, only Mozilla-based browsers and Internet Explorer is supported, I have no idea if and how this works in other browsers
  • qooxdoo variants should be used to deal with browser differences
  • We need a method which gets data from the system clipboard
  • Which are the flavors that can be used cross-browser - I have found that only “text/unicode” seems to work so far.
  • ...
    /**
     * tries to copy text to the clipboard of the underlying operating system
     * 
     * sources: http://www.krikkit.net/howto_javascript_copy_clipboard.html
     *          http://www.xulplanet.com/tutorials/xultu/clipboard.html
     *          http://www.codebase.nl/index.php/command/viewcode/id/174
     *          
     * works only in Mozilla and Internet Explorer
     * In Mozilla, add this line to your prefs.js file in your Mozilla user profile directory
     *    user_pref("signed.applets.codebase_principal_support", true);
     * or change the setting from within the browser with calling the "about:config" page
     **/
    copyToSystemClipboard : function ( text, flavor )
    {
      if ( ! flavor )
      {
        // default
        flavor = "text/unicode";
      }
      
      if (window.clipboardData) 
      {
    	   // IE
    	   window.clipboardData.setData("Text", text );
      } 
      else if (window.netscape) 
      { 
    	 	// Mozilla, Firefox etc.
        try 
        {
    		  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
    		} 
        catch(e) 
        {
    			alert(
    		   	"Because of tight security settings in Mozilla / Firefox you cannot copy "+
    				"to the system clipboard at the moment. Please open the 'about:config' page "+
    				"in your browser and change the preference 'signed.applets.codebase_principal_support' to 'true'."
    				);
    			return false;
    		}
         // we could successfully enable the privilege
    	   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(flavor);
    	   var str = new Object();
    	   var len = new Object();
    	   var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    	   var copytext=text;
    	   str.data=copytext;
    	   trans.setTransferData(flavor,str,copytext.length*2);
    	   var clipid=Components.interfaces.nsIClipboard;
    	   if (!clip) return false;
    	   clip.setData(trans,null,clipid.kGlobalClipboard);
    	   return true;
       } 
       else 
       {
    		alert("Your browser does not support copying to the clipboard!");
       }
    }
 
  }

Information

Last modified:
2008/02/14 00:50 by c.boulanger

Account

Not logged in

 
 

Job Offers

To further improve qooxdoo we are seeking javascript developers. Read more...

Rich Ajax Platform (RAP)

RAP uses qooxdoo, Java and the Eclipse development model to build rich web applications. Read more...

qooxdoo Web Toolkit (QWT)

Similar to GWT this framework allows to create impressive qooxdoo applications just using Java. Read more...

Pustefix

Pustefix is a MVC-based web application framework using Java and XML/XSLT. Read more...

 
SourceForge.net Logo

Bad Behavior has blocked 0 potential spam attempts in the last 7 days.