firefox window.clipboardData.getData

时间:2011.4.14 编辑: 叶建勇 阅读量:14464 全屏

<html>
<head>
<title>onpaste event example</title>
</head>

<body>
<h3>Play with this editor!</h3>
<div>
    <span style="color:red">Copy this text.</span>
</div>
<textarea id="editor" rows="3" cols="80">
Try pasting text into this area!
</textarea>
<div id="editdiv" contenteditable="true">
..
</div>

<script type="text/javascript">
//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;
}
    function log(txt) {
        document.getElementById("log").appendChild(document.createTextNode(txt + "\n"));
    }

    function pasteIntercept(evt) {
        var txt = getClipboard();
        setClipboard(txt);
        log("Pasting!");
    }

    document.getElementById("editor").addEventListener("paste", pasteIntercept, false);
    document.getElementById("editdiv").addEventListener("paste", pasteIntercept, false);
</script>

<h3>Log</h3>
<textarea rows="15" cols="80" id="log" readonly="true"></textarea>
</body>
</html>