Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
block copy/paste etc on web page
# __ __ __ __ __ __ ___ ___ __ ___ __
# / ` / \ |__) \ / |__) |__) / \ | |__ / ` | | / \ |\ |
# \__, \__/ | | | | \ \__/ | |___ \__, | | \__/ | \|
#
# https://stackoverflow.com/a/32804545/444955
$ ->
$.fn.blockCopy = (options) ->
settings = $.extend({ blockPasteClass: null }, options)
style_appender = (rule) ->
$('html > head').append $('<style>' + rule + '</style>')
html_appender = (html) ->
$('body').append html
clearClipboard = ->
$temp = $('#bypasser')
$temp.val('You can\'t cheat !').select()
document.execCommand 'copy'
add_absolute_div = (id) ->
html_appender '<div id=\'noClick' + id + '\' onclick=\'return false;\' oncontextmenu=\'return false;\'>&nbsp;</div>'
return
absorbEvent_ = (event) ->
e = event or window.event
e.preventDefault and e.preventDefault()
e.stopPropagation and e.stopPropagation()
e.cancelBubble = true
e.returnValue = false
preventLongPressMenu = (node) ->
node.ontouchstart = absorbEvent_
node.ontouchmove = absorbEvent_
node.ontouchend = absorbEvent_
node.ontouchcancel = absorbEvent_
set_absolute_div = (element, id) ->
position = element.position()
noclick = '#noClick' + id
$(noclick).css
height: element.height()
width: element.width()
position: 'absolute'
top: position.top
left: position.left
'z-index': 100
if settings.blockPasteClass
$('.' + settings.blockPasteClass).bind 'copy paste cut drag drop', (e) ->
e.preventDefault()
false
$('body').bind 'contextmenu', (e) ->
e.preventDefault()
#Append needed rules to CSS
style_appender '* {-moz-user-select: none !important; -khtml-user-select: none !important; -webkit-user-select: none !important; -ms-user-select: none !important; user-select: none !important; }' + '.content {position: relative !important; }' + '.content .mask {position: absolute !important ; z-index: 1 !important; width: 100% !important; height: 100%!important;}' + '.content a {position: relative !important; z-index: 3 !important;}' + '.content, .content .mask{ pointer-events: none;}'
#Append an input to clear the clipboard
html_appender '<input id=\'bypasser\' value=\'nothing\' type=\'hidden\'>'
#Clearing clipboard Intervali
setInterval clearClipboard, 1000
id = 1
@each ->
#Preventing using touch events
preventLongPressMenu $(this)
#Add CSS preventer rules to selected DOM & append mask to class
$(this).addClass('content').append '<div class=\'mask\'></div>'
#Append an absolute div to body
add_absolute_div id
#Set position of the div to selected DOM
set_absolute_div $(this), id
id++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.