0

I got some code from the internet, below, and used it in a mock exam application I am doing. This is suppose to prevent people from Printing Screen, copying or cutting from the exam page. The code works perfectly well in Internet Explorer but does not work in the other browsers. I need help to make the code below work in the other browsers to avoid cheating at the site during mock exam. Below is the code:

<script type="text/javascript">
function AccessClipboardData() {
    try {
        window.clipboardData.setData('text', "No print data");
    } catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.description + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);
    }
}

setInterval("AccessClipboardData()", 300);

document.onkeydown = function (ev) {
    var a;
    ev = window.event;
    if (typeof ev == "undefined") {
        alert("PLEASE DON'T USE KEYBORD");
    }
    a = ev.keyCode;
    alert("PLEASE DON'T USE KEYBORD");
    return false;
}
document.onkeyup = function (ev) {
    var charCode;
    if (typeof ev == "undefined") {
        ev = window.event;
        alert("PLEASE DON'T USE KEYBORD");
    } else {
        alert("PLEASE DON'T USE KEYBORD");
    }
    return false;
}

Bainn
  • 37
  • 12
  • You're never going to be able to prevent people from printing screen on computers you don't control. Someone can easily disable JS to get around your code. – mason Mar 12 '15 at 17:02
  • I think it is an illusion to think that there would be no solution to copy your page. It is always possible to disable JavaScript for example, or just this function of JS, and as soon as some information is on some screen, it can be copied. – Lithy Mar 12 '15 at 17:03
  • But I believe this will at least frustrate the little children or those not that advanced in computing. At least. – Bainn Mar 12 '15 at 17:16

3 Answers3

0

Please know that it is entirely impossible to prevent users from copying or screencapping your site from javascript, seeing how they could simply disable js or your function in particular as has been mentioned in the comments already.

If you simply want to discourage people as much as possible you can still use your code, however window.clipboardData.setData only works in IE so it is not strange you would get an error message in other browsers, for thos you would have to use execCommand to copy a set message to the clipboard at you set interval

documnet.execCommand(delete, false, null)

to delete the current selection and then

documnet.execCommand(copy, false, null)

to copy the currently selected text(which you just made sure was nothing)

(for more info on execCommand https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand)

this should work in Firefox, Safari and Chrome, I know of no way to do this in Opera, as neither command will work in that browser

Note however that this will keep overwritting your clipboard as long as the site is open in the browser, so even if someone tried to copy something else entirely they would be unable.

I would like to point out that I provide this function only to show you what the problem with your code, as you will never be able to do what you want to completely without getting people to install third party rights management software on their computer.

Serates
  • 106
  • 4
0

I find the following code at Stackoverflow here, by iDhavalVaja and it worked fine.

<script type="text/javascript">

$(function () {

    $(this).bind("contextmenu", function (e) {

        e.preventDefault();

    });

});
</script>
<script type="text/JavaScript">
    function killCopy(e) { return false }
    function reEnable() { return true }
    document.onselectstart = new Function("return false");
    if (window.sidebar) {
        document.onmousedown = killCopy;
        document.onclick = reEnable;
    }
</script>
Bainn
  • 37
  • 12
-1

If you just want to get this working in other browsers, maybe use jQuery (something like this):

       $(document).keydown(function (e) {
            alert("PLEASE DON'T USE KEYBORD");
        });
Steve
  • 531
  • 5
  • 16
  • And will this work if the user disables JavaScript? No. Will it prevent the user from making another window active, then taking a screenshot of the browser? No. – mason Mar 12 '15 at 18:07
  • Hi all! Thank you very much indeed. Serates, I will warn the candidates about this problem before they start the exam. Serates, please, the delete and copy, are they in quotation marks? and where in the JavaScript do I have to put them? Steve the jQuery too is working, but does not prevent highlighting and copying to the clipboard and pasting. – Bainn Mar 13 '15 at 09:11
  • @Mason, do some reading. He read your comment but indicated it could still be a deterrent and he still wanted to get it working in multiple browsers. Vent your anger elsewhere. – Steve Mar 13 '15 at 13:53
  • @Steve I have no anger for this. It doesn't affect me. But your script is not effective at all at accomplishing what the OP wants to do (mostly because it's impossible to do). – mason Mar 13 '15 at 14:01
  • @Mason, I get it. So did he. But as I read the comments, he basically said "hey, it could still deter them" and it seemed to indicate that the main problem was that it only worked in IE. So I figured I'd try the simple cross browser jQuery solution, see if that's what he wanted. Might not be the answer, but doesn't seem unreasonable. – Steve Mar 13 '15 at 14:11
  • Folks, cease fire. Stackoverflow is a great resource. The jQuery submitted by Steve? works fine but I find the following code here too and that solved the whole problem. – Bainn Mar 13 '15 at 15:05
  • – Bainn Mar 13 '15 at 15:06