当前位置: 代码迷 >> JavaScript >> javascript可以轻巧操作客户端剪贴板内容
  详细解决方案

javascript可以轻巧操作客户端剪贴板内容

热度:184   发布时间:2012-11-08 08:48:11.0
javascript可以轻松操作客户端剪贴板内容
写道
<html>
<head>
<script type="text/javascript">
<script language="javascript">
//IE、Firefox下Clipboard的读写js脚本
/**************************************************
将字符串maintext复制到剪贴板
**************************************************/
function setClipboard(maintext) {
if (window.clipboardData) {
return (window.clipboardData.setData("Text", maintext));
}
else if (window.netscape) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
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);
var copytext=maintext;
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;
}
return false;
}
/**************************************************
返回剪贴板的内容
**************************************************/
function getClipboard() {
if (window.clipboardData) {
return(window.clipboardData.getData('Text'));
}
else if (window.netscape) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
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');
clip.getData(trans,clip.kGlobalClipboard);
var str = new Object();
var len = new Object();
try {
trans.getTransferData('text/unicode',str,len);
}
catch(error) {
return null;
}
if (str) {
if (Components.interfaces.nsISupportsWString) str=str.value.QueryInterface(Components.interfaces.nsISupportsWString);
else if (Components.interfaces.nsISupportsString) str=str.value.QueryInterface(Components.interfaces.nsISupportsString);
else str = null;
}
if (str) {
return(str.data.substring(0,len.value / 2));
}
}
return null;
}
</script>
</script>
</head>
<body onmouseup="test()">
<span class="red b" id="PageBreak">[--page--]</span><br />

<input type="button" value="复制代码" onClick='javascript:if(setClipboard(document.getElementById("PageBreak").innerHTML)) {alert("已复制到剪切板,请在文章内容分页处粘贴")};' class="button" />
<br><br>
</body>
</html>

写道
下面都是我的猜测,ff的鼠标捕获相当于能自动设置和释放的document.body.setCapture(false)。
因为我测试下面的程序,发现ie和ff效果是差不多的:
ie:

<html>
<body>
<div id="aa" onmouseover="alert(1)"> </div>
<script>
document.body.onmousedown=function(){this.setCapture(false)}
document.body.onmouseup=function(){this.releaseCapture()}
document.onmousemove=function(){aa.innerHTML+=1}
</script>
</body>
</html>

ff:

<html>
<body>
<div id="aa" onmouseover="alert(1)"> </div>
<script>
document.onmousemove=function(){aa.innerHTML+=1}
</script>
</body>
</html>
  • ie:  document.selection.empty()
  • ff:  window.getSelection().removeAllRanges()

为了防止在拖放过程中选择内容,所以把它放到Move程序中,下面是兼容的写法:

window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();

?
  相关解决方案