【问题标题】:How to disable printscreen with javascript?如何使用 javascript 禁用打印屏幕?
【发布时间】:2014-10-10 10:06:20
【问题描述】:

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

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

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

【问题讨论】:

  • 我严重怀疑 - 在 JS 中可靠地修改剪贴板本身就是一个挑战。我相信通常会使用 Flash hack 来影响剪贴板。

标签: javascript jquery zeroclipboard


【解决方案1】:

还有另一种方法可以在您的网站中禁用 Print Screen(它适用于我的网站)。 单击here 转到我的 Pen (Codepen.io)。 这里也是一个sn-p:

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>

点击here获取原码

【讨论】:

  • 它适用于 PrintScreen,但是当用户按下 Win+PrintScreen(用于直接将图像写入 C:\Users\user\Pictures\Screenshots 中的文件)时,它不再起作用了。
【解决方案2】:

尝试在关闭您网站上的标签&lt;script&gt; &lt;/script&gt;之间的&lt;/body&gt;之前包含此内容

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

【讨论】:

  • 请考虑添加您的代码的简要说明,并将 cmets 翻译成英文,或者直接取消它们
  • 完成!有些组合我几乎设法解决了,等我准备好了再回来
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      你不能从 Javascript 中做到这一点。如果你真的需要这样做,请检查 Stop User from using "Print Scrn" / "Printscreen" key of the Keyboard for any Web Page

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

          灵感来自original link

          【讨论】:

            【解决方案8】:

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

            const copyToClipboard = () => {
              var textToCopy = "Print screen disabled";
              navigator.clipboard.writeText(textToCopy);
            }
            
            $(window).keyup((e) => {
              if (e.keyCode == 44) {
                setTimeout(
                  copyToClipboard(), 
                  1000
                );
              }
            });
            

            【讨论】:

              【解决方案9】:
                 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 键时出现/闪烁 按下组合,实际屏幕将无法打印屏幕......

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-03-20
                • 2011-03-09
                • 1970-01-01
                • 1970-01-01
                • 2010-10-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多