2011-04-18 71 views
0

我是新來的編程世界&我正在嘗試開發Firefox的擴展。我有一個帶有文本框的Xul窗口,我想複製整個文本框並放入Firefox的剪貼板並粘貼到Firefox瀏覽器的任何位置。Firefox Xul Clipboad

幫我解決一些JS代碼或使用xul編碼。

請幫我出來或給我一些建議。

感謝你們提前。

回答

0

我的問題是固定的:

下面是腳本:此腳本從文本框複製整個文本,你可以在瀏覽器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>