简体   繁体   English

JavaScript,打印屏幕以及复制和粘贴

[英]JavaScript, Print Screen and copy and paste

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. 该代码在Internet Explorer中可以很好地工作,但在其他浏览器中则无法工作。 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;
}

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. 请注意,要阻止用户使用javascript复制或截屏您的网站是完全不可能的,要了解他们如何能够简单地禁用js或您的功能,尤其是已经在评论中提到的。

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 如果您只是想尽可能地劝阻其他人,您仍然可以使用代码,但是window.clipboardData.setData仅在IE中有效,因此在其他浏览器中收到错误消息并不奇怪,因为您必须使用execCommand以设置的间隔将设置的消息复制到剪贴板

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 ) (有关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 这应该可以在Firefox,Safari和Chrome浏览器中使用,我无法在Opera中执行此操作,因为这两个命令都无法在该浏览器中使用

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. 我想指出的是,我提供此功能仅是为了向您显示代码的问题,因为如果不让人们在他们的计算机上安装第三方权限管理软件,您将永远无法完全完成您想做的事情。

I find the following code at Stackoverflow here, by iDhavalVaja and it worked fine. 我在iDhavalVaja的Stackoverflow上找到了以下代码,并且工作正常。

<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>

If you just want to get this working in other browsers, maybe use jQuery (something like this): 如果您只想在其他浏览器中使用此功能,则可以使用jQuery(如下所示):

       $(document).keydown(function (e) {
            alert("PLEASE DON'T USE KEYBORD");
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM