首页 > Linux操作系统 > Linux操作系统 > 转载 Using the Clipboard

转载 Using the Clipboard

原创 Linux操作系统 作者:aben_xml 时间:2009-06-24 12:57:45 0 删除 编辑

Using the Clipboard

This section provides information about cutting, copying and pasting to and from the clipboard.

The Clipboard

Mozilla provides a number of interfaces for accessing the clipboard. The component;1 can be used to copy text to the clipboard. This component implements the interface nsIClipboardHelper, which has a function copyString which can be used to copy a string.

  1. const gClipboardHelper = Components.classes[";1"].  
  2. getService(Components.interfaces.nsIClipboardHelper);  
  3. gClipboardHelper.copyString("Put me on the clipboard, please.");  

This example will first create a clipboard helper and then copy a short string to the clipboard. This method only works to put strings on the clipboard. For other types of data, such as URLs or images, you will need to use a more complex method.

A component;1 and an interface nsIClipboard provide general access to the system clipboard. You can use it to copy and paste any type of data from your application to the clipboard. Three XPCOM objects are needed to handle clipboard operations. The first is an object that holds the data to put on the clipboard. The second is the clipboard object. The third is an object which is used to transfer the data from the first object to the clipboard. The clipboard model in Mozilla requires you to perform. the following steps to copy data:

  1. Create an XPCOM wrapper for the data which you want to put on the clipboard. This is needed because you can put anything on the clipboard from text to images.
  2. Create a transferring object. This object can be the component;1 which implements the interface nsITransferable.
  3. Tell the transferring object about the type of data being copied.
  4. Tell the transferring object about the data to copy.
  5. Create a clipboard object which refers to the system clipboard.
  6. Tell the clipboard object to copy the data using the transferring object.

You might wonder why a transferring object is needed instead of just putting the object directly on the clipboard. One reason is that some systems do not copy the data right away. Instead, they wait until the data is pasted. Another reason is that the transferable can hold multiple representations of the same data. For example, a piece of HTML can be represented in both its original HTML form. and in plain text. If an application wants to get the data from the clipboard and doesn't understand HTML, it can use the plain text version. If it does understand HTML, it can grab that version. The transferring object will hold the clipboard contents until the application has decided what it needs. This allows the clipboard to be used by another application right away.

Let's break down the steps needed to copy data to the clipboard. First, we need to create an XPCOM object to wrap what we want to copy. We'll assume that we want to copy some text. We will use the interface nsISupportsString which can be used to represent strings (specifically, Unicode strings).

  1. var copytext = "Text to copy";  
  2. var str      = Components.classes[";1"].  
  3.                           createInstance(Components.interfaces.nsISupportsString);  
  4.     = copytext;  

The first line holds the text that we want to copy. Next, the variable str is assigned to a component that can be used to hold a string. The third line assigns the string to the component using the data property. Here, the string "Text to copy" will be copied but you can replace this with the text string that you want to copy. Now that we have the object to copy, a transferring object needs to be created:


  1. var trans = Components.classes[";1"].  
  2.                        createInstance(Components.interfaces.nsITransferable);  
  3. trans.addDataFlavor("text/unicode");  
  4. trans.setTransferData("text/unicode", str, copytext.length * 2);  


The first line gets the transferring component which implements nsITransferable. Next, we need to tell the transferable what type of data we want to use. The type of data is referred to as a data flavor. The function addDataFlavor is used to tell the transferable that it needs to transfer data of a certain flavour. In this case, we are transferring the flavor "text/unicode" which is a Unicode string. Then, the function setTransferData is called which copies the data from the string into the transferable. This function takes three parameters. The first is the flavor we are setting, the second is the object holding the string and the third is the length of the data, in bytes. Here, the length is multiplied by two because we are using a Unicode string which requires two bytes per character.

You can repeat the last two lines and call addDataFlavor and setTransferData for multiple flavors. That way, you could have a text version and an HTML version of the content. The Transferable object will hold its own copy of the data. When you've added all the flavors you want, you can put it all on the clipboard at once. The transferable object will hold all of the data that you want until you're ready to put it on the clipboard.

Next, we need to create a clipboard object that refers to the system clipboard.

  1. var clipid = Components.interfaces.nsIClipboard;  
  2. var clip   = Components.classes[";1"].getService(clipid);  
  3. clip.setData(trans, null, clipid.kGlobalClipboard);  

We get the system clipboard object and store it in the clip variable. We can copy the data to the clipboard by calling the function setData. The first parameter of this function is the transferable. The second parameter can usually be set to null but you could set it to a nsIClipboardOwner so that you can tell when the data you've copied is overwritten by another copy operation. Call setData only when you're ready to copy to the system clipboard.

The third parameter to setData (and the parameter to emptyClipboard) indicates which clipboard buffer to use. The above code uses the constant kGlobalClipboard for this, which refers to the global clipboard. This would be the same one that cut and paste operations from the Edit menu typically use. If you use kSelectionClipboard instead, you will copy into the selection buffer, which is generally only available on Unix systems.

This multi-step process has resulted in text being copied on the clipboard. We can cut to the clipboard instead of copying by doing a copy and then deleting the original data. Normally, the text would be in a document or textbox. The code is put together below, with additional error checking:

  1. var copytext = "Text to copy";  
  3. var str = Components.classes[";1"].  
  4. createInstance(Components.interfaces.nsISupportsString);  
  5. if (!str) return false;  
  7. = copytext;  
  9. var trans = Components.classes[";1"].  
  10. createInstance(Components.interfaces.nsITransferable);  
  11. if (!trans) return false;  
  13. trans.addDataFlavor("text/unicode");  
  14. trans.setTransferData("text/unicode", str, copytext.length * 2);  
  16. var clipid = Components.interfaces.nsIClipboard;  
  17. var clip = Components.classes[";1"].getService(clipid);  
  18. if (!clip) return false;  
  20. clip.setData(trans, null, clipid.kGlobalClipboard);  

The complete function shown below copies some text to the clipboard as HTML, as well as making it a clickable Hyperlink. So we've added a text flavor and an HTML flavor of the content. The Transferable object will hold both flavors.

  1. copyLinkToClipboard : function(copyURL,copyLabel) {  
  3. // generate the Unicode and HTML versions of the Link  
  5. var textUnicode = copyURL;  
  6. var textHtml = (" + copyURL + "\">" + copyLabel + "");  
  9. // make a copy of the Unicode  
  11. var str = Components.classes[";1"].  
  12.                        createInstance(Components.interfaces.nsISupportsString);  
  13. if (!str) return false// couldn't get string obj  
  14. = textUnicode; // unicode string?  
  17. // make a copy of the HTML  
  19. var htmlstring = Components.classes[";1"].  
  20.                        createInstance(Components.interfaces.nsISupportsString);  
  21. if (!htmlstring) return false// couldn't get string obj  
  22. = textHtml;           
  25. // add Unicode & HTML flavors to the transferable widget  
  27. var trans = Components.classes[";1"].  
  28.                        createInstance(Components.interfaces.nsITransferable);  
  29. if (!trans) return false//no transferable widget found  
  31. trans.addDataFlavor("text/unicode");  
  32. trans.setTransferData("text/unicode", str, textUnicode.length * 2); // *2 because it's unicode  
  34. trans.addDataFlavor("text/html");  
  35. trans.setTransferData("text/html", htmlstring, textHtml.length * 2); // *2 because it's unicode   
  38. // copy the transferable widget!  
  40. var clipboard = Components.classes[";1"].  
  41.                        getService(Components.interfaces.nsIClipboard);  
  42. if (!clipboard) return false// couldn't get the clipboard  
  44. clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);  
  45. return true;  
  47. },  

Pasting Clipboard Contents

To paste data from the clipboard we can use a similar process, except we use getData instead of setData and getTransferData instead of setTransferData. Here are the steps to pasting:

  1. Create a clipboard object which refers to the system clipboard.
  2. Create a transferring object which implements the nsITransferable interface.
  3. Tell the transferring object about the flavor of data you want to get.
  4. Retrieve the data from the clipboard and put it in the transferable.
  5. Get the data from the transferring object.

The first steps are similar to that used for copying:

  1. var clip = Components.classes[";1"].getService(Components.interfaces.nsIClipboard);  
  2. if (!clip) return false;  
  4. var trans = Components.classes[";1"].createInstance(Components.interfaces.nsITransferable);  
  5. if (!trans) return false;  
  6. trans.addDataFlavor("text/unicode");  

This code gets the system clipboard object and a transferable object. The flavor is added to the transferable. Next, we need to get the data from the clipboard:

  1. clip.getData(trans, clip.kGlobalClipboard);  
  3. var str       = new Object();  
  4. var strLength = new Object();  
  6. trans.getTransferData("text/unicode", str, strLength);  

The first line performs the opposite of getData. The data currently on the system clipboard is placed into the transferable. Next we create two JavaScript. objects which will hold the data and the length of the data. Note that we have no idea what type of data is currently on the clipboard. It may have been placed there by another application. This is why we use generic Objects for str and strLength.

Then we use getTransferData to retrieve the data from the transferable. We specify the flavor we would like to get. The data will be converted if it is not of the desired flavor and a conversion between the actual and desired flavor is possible. If you originally copied data of multiple flavors onto the clipboard, you can retrieve the data in the best format necessary. For example, a textbox would accept "text/unicode" (or "text/plain") while a Composer window might accept HTML and image data.

The variable str now holds the data from the clipboard. We need to convert the data back into a JavaScript. string from an XPCOM object. The code below can be used for this purpose:


  1. if (str) str       = str.value.QueryInterface(Components.interfaces.nsISupportsString);  
  2. if (str) pastetext =, strLength.value / 2);  

Because the data from the transferable is a nsISupportsString, we need to convert it into a JavaScript. string. The property value of the object filled in by getTransferData, which is str in this case, provides the actual value of the object.

We assign the string to the variable pastetext. We can then put that into a textbox or other location as necessary.

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录


  • 博文量
  • 访问量