Firefox Xul Clipboad

為{幸葍}努か 提交于 2019-12-13 05:05:25

问题


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.


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/5700519/firefox-xul-clipboad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!