如何使用 javascript 禁用打印屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26296882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:47:16  来源:igfitidea点击:

How to disable printscreen with javascript?

javascriptjqueryzeroclipboard

提问by GoobyPrs

I want to make function in javascript which change value of clipboard after the printscreen was used. Is that possible?

我想在使用打印屏幕后更改剪贴板值的 javascript 中创建函数。那可能吗?

$(document).keyup(function(e){ if(e.keyCode == 44) //change clipboard value code });

$(document).keyup(function(e){ if(e.keyCode == 44) //change clipboard value code });

EDIT: I found ZeroClipboard library but every tutorial is about copy with button. I want just change the value of clipboard.

编辑:我找到了 ZeroClipboard 库,但每个教程都是关于使用按钮复制的。我只想更改剪贴板的值。

回答by Abhay Singh

You can do it with javascript and jquery. Just copying another thing in clipboard place of screen capture.

你可以用 javascript 和 jquery 来做到这一点。只需在屏幕截图的剪贴板位置复制另一件事。

function copyToClipboard() {

  var aux = document.createElement("input");
  aux.setAttribute("value", "print screen disabled!");      
  document.body.appendChild(aux);
  aux.select();
  document.execCommand("copy");
  // Remove it from the body
  document.body.removeChild(aux);
  alert("Print screen disabled!");
}

$(window).keyup(function(e){
  if(e.keyCode == 44){
    copyToClipboard();
  }
});

回答by Rune

There is another way to disable Print Screen in your website (it worked for my website). Click hereto go to my Pen (Codepen.io). Here is also a snippet:

还有另一种方法可以在您的网站中禁用 Print Screen(它适用于我的网站)。单击此处转到我的 Pen (Codepen.io)。这里还有一个片段:

document.addEventListener("keyup", function (e) {
    var keyCode = e.keyCode ? e.keyCode : e.which;
            if (keyCode == 44) {
                stopPrntScr();
            }
        });
function stopPrntScr() {

            var inpFld = document.createElement("input");
            inpFld.setAttribute("value", ".");
            inpFld.setAttribute("width", "0");
            inpFld.style.height = "0px";
            inpFld.style.width = "0px";
            inpFld.style.border = "0px";
            document.body.appendChild(inpFld);
            inpFld.select();
            document.execCommand("copy");
            inpFld.remove(inpFld);
        }
       function AccessClipboardData() {
            try {
                window.clipboardData.setData('text', "Access   Restricted");
            } catch (err) {
            }
        }
        setInterval("AccessClipboardData()", 300);
body {
  background-color: #00FF00;
}
<html>
    <head>
      <title>Disable Print Screen</title>
    </head>
  <body>
      <h2>Print screen is disabled</h2>
      <p>Click anywhere on green background and try to "print screen" the content (and then see the result in Paint or simulair software)
  </body>
</html>

Click herefor original code

单击此处获取原始代码

回答by Sumate Mephokkij

Try this code to Disable PrtScr or Alt+PrntScr in All Browsers using JavaScript.

尝试使用此代码在所有使用 JavaScript 的浏览器中禁用 PrtScr 或 Alt+PrntScr。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Disable Print Screen</title>
<script>
  window.console = window.console || function(t) {};
</script>
<script>
  if (document.location.search.match(/type=embed/gi)) {
    window.parent.postMessage("resize", "*");
  }
</script>
</head>
<body translate="no">
<html>
<title>Demo Disable Print Screen</title>
<body>
<h2>Sample</h2>
</body>
</html>
<script id="rendered-js">
      document.addEventListener("keyup", function (e) {
  var keyCode = e.keyCode ? e.keyCode : e.which;
  if (keyCode == 44) {
    stopPrntScr();
  }
});
function stopPrntScr() {

  var inpFld = document.createElement("input");
  inpFld.setAttribute("value", ".");
  inpFld.setAttribute("width", "0");
  inpFld.style.height = "0px";
  inpFld.style.width = "0px";
  inpFld.style.border = "0px";
  document.body.appendChild(inpFld);
  inpFld.select();
  document.execCommand("copy");
  inpFld.remove(inpFld);
}
function AccessClipboardData() {
  try {
    window.clipboardData.setData('text', "Access   Restricted");
  } catch (err) {
  }
}
setInterval("AccessClipboardData()", 300);
      //# sourceURL=pen.js
    </script>
</body>
</html>

Inspire from original link.

灵感来自原始链接

回答by skidadon

Uses arrow functions and navigator. Clean and will work with modern browsers.

使用箭头功能和导航器。干净,可与现代浏览器一起使用。

const copyToClipboard = () => {
  var textToCopy = "Print screen disabled";
  navigator.clipboard.writeText(textToCopy);
}

$(window).keyup((e) => {
  if (e.keyCode == 44) {
    setTimeout(
      copyToClipboard(), 
      1000
    );
  }
});

回答by gokul

You can't. It's beyond your control, because print screen (unlike the in-browser print icon/Ctrl-P) is not a browser feature but a system feature.

你不能。这超出了您的控制,因为打印屏幕(与浏览器中的打印图标/Ctrl-P 不同)不是浏览器功能而是系统功能。

回答by JagKum

U can't do it from Javascript. If you really need to do it pls check Stop User from using "Print Scrn" / "Printscreen" key of the Keyboard for any Web Page

你不能从 Javascript 中做到这一点。如果您真的需要这样做,请检查 停止用户使用任何网页的键盘的“Print Scrn”/“Printscreen”键

回答by Harshal Patil

You cannot. The user can capture the screen no matter what you do with your scripts. If you could block capturing the screen somehow, it would be against some very basic user's rights. Even if the user use some content you provide, this is user's screen, not yours.

你不能。无论您使用脚本做什么,用户都可以捕获屏幕。如果您可以以某种方式阻止捕获屏幕,那将违反一些非常基本的用户权利。即使用户使用您提供的某些内容,这是用户的屏幕,而不是您的。