meyerweb.com

Skip to: site navigation/presentation
Skip to: Thoughts From Eric

Undoing oncut/oncopy/onpaste Falsities

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.

Four Responses»

    • #1
    • Comment
    • Wed 8 Jul 2015
    • 1254
    Kevin H wrote in to say...

    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
    • Comment
    • Wed 8 Jul 2015
    • 1331
    Eric Meyer wrote in to say...

    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
    • Comment
    • Thu 9 Jul 2015
    • 0401
    JulienW wrote in to say...

    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
    • Pingback
    • Fri 10 Jul 2015
    • 1728
    Received from Article Roundup - Hoverboard Studios

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

Leave a Comment

Line and paragraph breaks automatic, e-mail address required but never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



Remember to encode character entities if you're posting markup examples! Management reserves the right to edit or remove any comment—especially those that are abusive, irrelevant to the topic at hand, or made by anonymous posters—although honestly, most edits are a matter of fixing mangled markup. Thus the note about encoding your entities. If you're satisfied with what you've written, then go ahead...


July 2015
SMTWTFS
June August
 1234
567891011
12131415161718
19202122232425
262728293031  

Sidestep

Feeds

Extras