Undoing oncut/oncopy/onpaste Falsities

Published 3 years, 5 months ago

Inspired by Ryan Joy’s excellent and deservedly popular tweet, I wrote a small, not-terribly-smart Javascript function to undo cut/copy/paste blocking in HTML.

function fixCCP() {
   var elems = document.getElementsByTagName('*');
   var attrs = ['onpaste','oncopy','oncut'];
   for (i = 0; i < elems.length; i++) {
      for (j = 0; j < attrs.length; j++) {
         if (elems[i].getAttribute(attrs[j])) {
            elems[i].setAttribute(attrs[j],elems[i]
            .getAttribute(attrs[j])
            .replace("return false","return true"));
         }
      }
   }
}

Here it is as a bookmarklet, if you still roll that way (as I do): fixCCP.  Thanks to the Bookmarklet Maker at bookmarklets.org for helping me out with that!

If there are obvious improvements to be made to its functionality, let me know and I’ll throw it up on Github.


  1. Nice, but I don’t think it will work if the CCP blocking is implemented as something like this:

    <script>
    $(document).ready(function(){
    $('input').on('cut',function(e){e.preventDefault();});
    $('input').on('copy',function(e){e.preventDefault();});
    $('input').on('paste',function(e){e.preventDefault();});
    });
    </script>

  2. Yeah, it skips event listeners entirely (thus “blocking in HTML”). I went for fast and cheap. If there’s a good universal way to deal with listeners too, I’m happy to add it!

  3. Maybe listen to the event in the “capture” phase and override
    e.preventDefault to an empty function?


    ['cut', 'copy', 'paste'].forEach(function(name) {
    window.addEventListener(name, function(event) {
    event.preventDefault = function() {};
    }, true);
    });

    I didn’t try on a website that _really_ was doing this. Also this won’t work if they do it in a iframe of course.

  4. […] Undoing oncut/oncopy/onpaste Falsities […]

Leave a Comment

Management reserves the right to edit or remove any comment, especially when abusive or irrelevant to the topic at hand. HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <em> <i> <q cite=""> <s> <strong> <pre class=""> <kbd>


Comment Preview

If you're satisfied with what you've written, then go ahead...