Question

Some form validators block CTRL+C/CTRL+V on some input fields. Is there a quick bookmarklet or something that could allow me to use my keyboard properly. Or, perhaps, browsers allow my to override that kind of rude behavior of some pages?

EDIT: What I'm looking for is similar solution like the one that can be used to enable right-click on a page by executing a javascript snippet withing context of the page:

javascript:void(document.oncontextmenu=null) 

So far, I think that there is no similar solution with javascript snippet without involvment with 3rd party tools like Greasemonkey

Was it helpful?

Solution

That depends on how it is done. Also, removing/disabling event listeners is always risky, you might harm the functionality of the site seriously - and you never know whether a handler does evil or good things, least you can know whether there is a handler at all.

So, I've skimmed the first few Google results for "javascript prevent copy paste" and found mostly foolish scripts: Disable pasting text into HTML form, Prevent Copy, Cut and Paste into HTML Input Text Fields, not working example. So what a script could do now:

  • remove all on-copy, -paste, -input, -cut, -drag, etc properties of input elements
  • capture these event types on the document and stop their propagation, so (bubble) handlers will never get called and can't prevent the default action

Yet, as already said, this can't hinder everything. The input event, a very useful one, is cleverly used by this solution to prevent inserting more than one character at once. So you would need to decide whether you want to stop that event and possible break the application.

var evts = ["copy", "paste", "cut", "drag", "selectionstart?", "input?", …?];
var els = [document, document.body].concat(
  [].slice.call(document.getElementsByTagName("input")),
  [].slice.call(document.getElementsByTagName("textarea"))
);
function trap(e) { e.stopPropagation(); }
for (var i=0; i<evts.length; i++) {
    var evt = "on"+evts[i].charAt(0).toUpperCase()+evts[i].substr(1);
    for (var j=0; j<els.length; j++)
        els[j][evt] = null; // remove handler
    document.addEventListener(evts[i], trap, true); // capture phase
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top