How to Disable Copy Paste (Browser)

后端 未结 10 703
旧时难觅i
旧时难觅i 2020-11-27 19:36

I am trying 2 alternatives:

  • Ignore right-click
  • Ignore ctrl + C, ctrl + A

This is my code:

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 19:51

    If you do not want to block highlighting or want to allow user to only copy a limited number of characters :

      function anticopy(event: ClipboardEvent) {    
        // @ts-ignore
        const clipboardData = event.originalEvent.clipboardData || window.clipboardData || event.originalEvent.clipboardData;
        const txt = window.getSelection().toString() || editor.getWin().getSelection().toString();
        if (txt.length > 200) {
          const no = 'You cannot copy more than 200 characters.';
          clipboardData.setData('text', no);
          clipboardData.setData('text/html', `

    ${no}

    `); } else { const html = `

    ${txt}

    `; clipboardData.setData('text', txt); clipboardData.setData('text/html', html); } event.preventDefault(); }

提交回复
热议问题