Skopírovať text do a zo schránky
JavaScript -> Ukážkové programy v JavaScripte -> Skopírovať text do a zo schránky
Táto operácia sa dá robiť aj cez príkazy window.cliboardData.getData a window.cliboardData.setData ale tieto príkazy fungujú len pre Internet Explorer.
Dolu popísané funkcie by mali operácie urobiť aj na iných prehliadačoch.
function to_clip(copytext)
//Použité z http://www.krikkit.net/howto_javascript_copy_clipboard.html
{
if (window.clipboardData)
{
window.clipboardData.setData("Text", copytext);
}
else if (window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
}
catch (e)
{
alert(e);
alert("V 'about:config' nastav 'signed.applets.codebase_principal_support' na 'true'");
return false;
}
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('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance( Components.interfaces.nsISupportsString);
str.data=copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid=Components.interfaces.nsIClipboard;
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
return true;
}
function from_clip()
{
//Použité z http://www.xulplanet.com/tutorials/xultu/clipboard.html
var pastetext;
if (window.clipboardData)
{
pastetext = window.clipboardData.getData("Text")
}
else if (window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
}
catch (e)
{
alert(e);
alert("V 'about:config' nastav 'signed.applets.codebase_principal_support' na 'true'");
return false;
}
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 strLength = new Object();
trans.getTransferData("text/unicode",str,strLength);
if (str) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
if (str) pastetext = str.data.substring(0,strLength.value / 2);
}
return pastetext;
}
//Použité z http://www.krikkit.net/howto_javascript_copy_clipboard.html
{
if (window.clipboardData)
{
window.clipboardData.setData("Text", copytext);
}
else if (window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
}
catch (e)
{
alert(e);
alert("V 'about:config' nastav 'signed.applets.codebase_principal_support' na 'true'");
return false;
}
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('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance( Components.interfaces.nsISupportsString);
str.data=copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid=Components.interfaces.nsIClipboard;
if (!clip) return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
return true;
}
function from_clip()
{
//Použité z http://www.xulplanet.com/tutorials/xultu/clipboard.html
var pastetext;
if (window.clipboardData)
{
pastetext = window.clipboardData.getData("Text")
}
else if (window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
}
catch (e)
{
alert(e);
alert("V 'about:config' nastav 'signed.applets.codebase_principal_support' na 'true'");
return false;
}
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 strLength = new Object();
trans.getTransferData("text/unicode",str,strLength);
if (str) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
if (str) pastetext = str.data.substring(0,strLength.value / 2);
}
return pastetext;
}