PDA

View Full Version : Detecting Pasted Content


herronial
03-26-2006, 01:04 PM
Hi,

I'm trying to customize a wysiwyg editor (the widgEditor). It's setup to automatically clean any html that is pasted in the textarea. However it only detects the pasted content if you use 'Ctrl-V'. If you 'right-click paste' or click 'Edit-Paste' the content won't be automaticaaly cleaned.

Here is the code that detects the paste:

/* Check for pasted content */
widgEditor.prototype.detectPaste = function(e)
{
var keyPressed = null;
var theEvent = null;

if (e)
{
theEvent = e;
}
else
{
theEvent = event;
}

if (theEvent.ctrlKey && theEvent.keyCode == 86 && this.wysiwyg)
{
var self = this;

this.pasteCache = this.theIframe.contentWindow.document.getElementsByTagName("body")[0].innerHTML;

/* Because Mozilla can't access the clipboard directly, must rely on timeout to check pasted differences in main content */
setTimeout(function(){self.cleanSource(); return true;}, 100);
}

return true;
}

Anyone have any ideas how I could get the pasted content automatically cleaned no matter what method the user opts for?

cheers

felgall
03-26-2006, 08:49 PM
There is no way to distinguish how text is added into a text area unless it is done via the keyboard. The best time to validate the content would be using an onchange event.

Mozilla isn't the only browser that has no access to the clipboard. There are hundreds of different browsers that can't access the clipboard and only one that sometimes can access it (Internet Explorer with security set lower than most security experts advise setting it).