lovehuayud 2010-05-31
代码如下:
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; }
代码如下:
if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (ex) { alert("If you want to do paste, please input 'about:config' in address bar, then input Enter.\n Set \"signed.applets.codebase_principal_support\" to \"true\""); } }
代码如下:
function getSel(w) { return w.getSelection ? w.getSelection() : w.document.selection; } function setRange(sel,r) { sel.removeAllRanges(); sel.addRange(r); } function filterPasteData(originalText) { var newText=originalText; //do something to filter unnecessary data return newText; } function block(e) { e.preventDefault(); } var w,or,divTemp,originText; var newData; function pasteClipboardData(editorId,e) { var objEditor = document.getElementById(editorId); var edDoc=objEditor.contentWindow.document; if(isIE) { var orRange=objEditor.contentWindow.document.selection.createRange(); var ifmTemp=document.getElementById("ifmTemp"); if(!ifmTemp) { ifmTemp=document.createElement("IFRAME"); ifmTemp.id="ifmTemp"; ifmTemp.style.width="1px"; ifmTemp.style.height="1px"; ifmTemp.style.position="absolute"; ifmTemp.style.border="none"; ifmTemp.style.left="-10000px"; ifmTemp.src="iframeblankpage.html"; document.body.appendChild(ifmTemp); ifmTemp.contentWindow.document.designMode = "On"; ifmTemp.contentWindow.document.open(); ifmTemp.contentWindow.document.write("<body></body>"); ifmTemp.contentWindow.document.close(); }else { ifmTemp.contentWindow.document.body.innerHTML=""; } originText=objEditor.contentWindow.document.body.innerText; ifmTemp.contentWindow.focus(); ifmTemp.contentWindow.document.execCommand("Paste",false,null); objEditor.contentWindow.focus(); newData=ifmTemp.contentWindow.document.body.innerHTML; //filter the pasted data newData=filterPasteData(newData); ifmTemp.contentWindow.document.body.innerHTML=newData; //paste the data into the editor orRange.pasteHTML(newData); //block default paste if(e) { e.returnValue = false; if(e.preventDefault) e.preventDefault(); } return false; }else { enableKeyDown=false; //create the temporary html editor var divTemp=edDoc.createElement("DIV"); divTemp.id='htmleditor_tempdiv'; divTemp.innerHTML='\uFEFF'; divTemp.style.left="-10000px"; //hide the div divTemp.style.height="1px"; divTemp.style.width="1px"; divTemp.style.position="absolute"; divTemp.style.overflow="hidden"; edDoc.body.appendChild(divTemp); //disable keyup,keypress, mousedown and keydown objEditor.contentWindow.document.addEventListener("mousedown",block,false); objEditor.contentWindow.document.addEventListener("keydown",block,false); enableKeyDown=false; //get current selection; w=objEditor.contentWindow; or=getSel(w).getRangeAt(0); //move the cursor to into the div var docBody=divTemp.firstChild; rng = edDoc.createRange(); rng.setStart(docBody, 0); rng.setEnd(docBody, 1); setRange(getSel(w),rng); originText=objEditor.contentWindow.document.body.textContent; if(originText==='\uFEFF') { originText=""; } window.setTimeout(function() { //get and filter the data after onpaste is done if(divTemp.innerHTML==='\uFEFF') { newData=""; edDoc.body.removeChild(divTemp); return; } newData=divTemp.innerHTML; // Restore the old selection if (or) { setRange(getSel(w),or); } newData=filterPasteData(newData); divTemp.innerHTML=newData; //paste the new data to the editor objEditor.contentWindow.document.execCommand('inserthtml', false, newData ); edDoc.body.removeChild(divTemp); },0); //enable keydown,keyup,keypress, mousedown; enableKeyDown=true; objEditor.contentWindow.document.removeEventListener("mousedown",block,false); objEditor.contentWindow.document.removeEventListener("keydown",block,false); return true; } }
代码如下:
var ifrm=document.getElementById("editor") if(isIE) { ifrm.contentWindow.document.documentElement.attachEvent("onpaste", function(e){return pasteClipboardData(ifrm.id,e);}); } else { ifrm.contentWindow.document.addEventListener("paste", function(e){return pasteClipboardData(ifrm.id,e);},false); }