How to prevent copying text other than phone and email?

  • 0
    How to modify the script so that it forbids copying everything on the site except for links a [href * = 'mailto:'] and a [href * = 'tel:'] ???

    // PREVENT CLIPBOARD COPYING
    document.querySelector('body').addEventListener("copy", function(evt){
        // Change the copied text if you want
        evt.clipboardData.setData("text/plain", "Copying is not allowed on this webpage");
    
        // Prevent the default copy action
        evt.preventDefault();
    }, false);
    JavaScript Anonymous, Jul 20, 2020

  • 1 Answers
  • 0
    // PREVENT CLIPBOARD COPYING
    document.querySelector('body').addEventListener("copy", function(evt){
    if(evt.target.nodeName === "A" && (evt.target.href.indexOf("mailto:") === 0 || evt.target.href.indexOf("tel:") === 0)) return;

    // Change the copied text if you want
    evt.clipboardData.setData("text/plain", "Copying is not allowed on this webpage");

    // Prevent the default copy action
    evt.preventDefault();
    }, false);
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!