How to disable printscreen with javascript?

余生长醉 提交于 2021-01-04 03:18:34

问题


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

$(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.


回答1:


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

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 here for original code




回答2:


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

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();
  }
});



回答3:


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.




回答4:


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




回答5:


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.




回答6:


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

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




回答7:


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
    );
  }
});


来源:https://stackoverflow.com/questions/26296882/how-to-disable-printscreen-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!