Undoing oncut/oncopy/onpaste Falsities
Published 9 years, 3 months pastInspired 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.
Comments (4)
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>
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!
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.
Pingback ::
Article Roundup - Hoverboard Studios
[…] Undoing oncut/oncopy/onpaste Falsities […]