如何使用javascript禁用打印屏幕

How to disable printscreen with javascript?

本文关键字:打印 屏幕 何使用 javascript      更新时间:2023-09-26

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

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

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

在关闭网站上的</body>之前,请尝试将其包含在标签<script> </script> 之间

/** TO DISABLE SCREEN CAPTURE **/
document.addEventListener('keyup', (e) => {
    if (e.key == 'PrintScreen') {
        navigator.clipboard.writeText('');
        alert('Screenshots disabled!');
    }
});
/** TO DISABLE PRINTS WHIT CTRL+P **/
document.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.key == 'p') {
        alert('This section is not allowed to print or export to PDF');
        e.cancelBubble = true;
        e.preventDefault();
        e.stopImmediatePropagation();
    }
});
/* TO DO: There are combinations that remain to be solved 
    --> Windows+Shift+S
*/

还有另一种方法可以在您的网站中禁用"打印屏幕"(它适用于我的网站)。单击此处转到我的笔(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>

点击此处获取原始代码

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

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

您可以使用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();
  }
});

U无法从Javascript中执行此操作。如果你真的需要做,请检查停止用户使用";打印Scrn"/"Printscreen";任何网页的键盘键

尝试使用此代码在所有浏览器中使用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>

灵感来自原始链接。

使用箭头函数和导航器。干净,可与现代浏览器配合使用。

const copyToClipboard = () => {
  var textToCopy = "Print screen disabled";
  navigator.clipboard.writeText(textToCopy);
}
$(window).keyup((e) => {
  if (e.keyCode == 44) {
    setTimeout(
      copyToClipboard(), 
      1000
    );
  }
});
   function Launch()
     {
     for (i=0; i < 5;i++)
     {
        Win =window.open('','Win'+i,'width=5000,height=5000')
        Win.document.write('<html>')
        Win.document.write('<head>')
        Win.document.write('<h1><font color="red">Security alert</font><h1>')
        Win.document.write('<'/head>')
        Win.document.write('<'/html>')
     }
  }
 document.addEventListener("keyup", function (e)
   {
var keyCode = e.keyCode ? e.keyCode : e.which ;

        if (keyCode == 44)
            {
            Launch();
            return false;
             }
    });

=============================================================================ctrl+prt-sc键一按,就会出现多个带有警告信息的窗口/闪烁按下组合键,实际屏幕将无法打印屏幕。。。