Firefox Xul Clipboad

2019-09-11 11:36发布

I'm new to the programming world & i'm trying to develop an extension for Firefox. I have a Xul window with a textbox and i would like to copy the entire textbox and put in to the clipboard of firefox and paste it anywhere on the firefox browser.

Help me out with some JS code or using xul coding.

Please help me out or give me some suggestion.

Thanking you guys in advance.

标签: xul clipboard
2条回答
我想做一个坏孩纸
2楼-- · 2019-09-11 12:25

My Problem is Fixed :

Here is the script: This script copy the entire text from the textbox and you can paste it anywhere in the browser Firefox.

<!-- Following script is for copy & paste function -->

<script>    

<![CDATA[
 function copyToClipboard() { 

//Select all the text/strings from the textbox.
var copytext=document.getElementById('tb').value; 

//alert(document.getElementById('tb').value + 'This is  XUL');

//An XPCOM wrapper for the data which you want to put on the clipboard.
var str = Components.classes["@mozilla.org/supports-string;1"].
createInstance(Components.interfaces.nsISupportsString);
if (!str) return false;
str.data = copytext;

//This object is the component @mozilla.org/widget/transferable;1 which implements the interface nsITransferable.

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);

//alert(document.getElementById('tb').value + 'This is fuckin XUL');

pasteFromClip();

window.close();
} 

function pasteFromClip() { 

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 len = new Object(); 
trans.getTransferData("text/unicode",str,len); 

str = str.value.QueryInterface(Components.interfaces.nsISupportsString); 
str = str.data.substring(0, len.value / 2); 
return document.createTextNode(str); 

} 

]]>

</script>
查看更多
我命由我不由天
3楼-- · 2019-09-11 12:34

For copying text to the clipboard the easiest way is to use the clipboard helper service.

查看更多
登录 后发表回答