“Prevent” users to steal data from your webpage
Programming

"Prevent" users to steal data from your webpage

October 22, 2012

OK, I know the title is kind of funny, but funny titles are increasing your curiosity, and the chance that you will read this article 🙂 However, every geek, that has a little knowledge about the web, know the we can not prevent that a user will copy our text or images from our website. We can only make the way of “stealing” a little bit difficult.

Ok, now from the beginning. Recently I got a project, exactly a webpage to develop. And there was a strange request. I should develop the page in this way, that the user can not easily copy the text and than used it for different purposes. I accepted the project, so the feature had to be implemented. And there is the solution I choose.

The folowing JavaScript code prevent to use the right-click and event to trigger the action of button combination: CTRL+A, CTRL+C, CTRL+V, CTRL+X.

//DISABLE COMBINATIONS LIKE CTRL+A, CTRL+C, CTRL+V
function disableCtrlKeyCombination(e){

    //list all CTRL + key combinations you want to disable
    var forbiddenKeys = new Array('a', 'c', 'x', 'v');
    var key;
    var isCtrl;

    if(window.event){
        key = window.event.keyCode;     //IE
        if(window.event.ctrlKey)
        isCtrl = true;
        else
        isCtrl = false;
    }else{
        key = e.which;     //firefox
        if(e.ctrlKey)
        isCtrl = true;
        else
        isCtrl = false;
    }
    //if ctrl is pressed check if other key is in forbidenKeys array
    if(isCtrl){
        for(i=0; i < forbiddenKeys.length; i++){
            //case-insensitive comparation
            if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase()){
                return false;
            }
        }
    }
    return true;
}
document.onkeypress=function(e){return disableCtrlKeyCombination(e);}
document.onkeydown=function(e){return disableCtrlKeyCombination(e);}
//DISABLE RIGHT CLICK
document.oncontextmenu = function(){return false}
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown = function(e){
    if(e.target==document)return false;
}

										
										
										
										
										
										
0

Follow deanpodgornik on

Comments (4)
  • You don’t need to prevent various key combinations… you just have to disable any text selection.

    What about absolute positioning a transparent div in front of the text container?

    Matjaž
  • You are right. Disabling CTRL+A is enough.

    The transparent div is a good solution (also disabled-javascript proof), but in my case is not so optimal 🙂 I needed to prevent “text stealing” from every page of the website. Slicing those transparent div would take me at least 2 hour (If I want to let the links clickable).

    Thank you for your advise 🙂 I will reduce the key checking only to CTRL+A 🙂

    deanpodgornik
  • Ctrl+A is not enough, someone could select the text by mouse.
    You have to check window.selection or document.selection and disable it imediately when it appears.

    Matjaž
  • Of cource…. how I didn’t think of this before 🙂 Maybe it is the late hour 🙂

    Thanks for the solution… I will update this post with this optimal solution 😉

    deanpodgornik
  • Leave a Reply

    Your email address will not be published. Required fields are marked *