Simple Anti-Copy Protection Javascript For Any Website

Simple Anti-Copy Protection Javascript For Any Website

Sometimes, you just don’t want people copying your site content. Here’s a quick and simple script to block copying, while still letting you allow it for specific areas if needed.

The Script

<script>const e=['PRE','CODE','TEXTAREA','BLOCKQUOTE'],t=['.allow-copy','#copy-area'],m="Copying is not allowed.";document.addEventListener("copy",o=>{const c=o.target;(e.includes(c.tagName)||t.some(e=>c.closest(e)))||(m&&o.clipboardData.setData("text/plain",m),o.preventDefault())},!1);</script>

How It Works

  1. Blocks copying everywhere by default.
  2. Copy Message: The default message that appears when a user tries to copy content on your site is "Copying is not allowed". You can modify the message or leave it empty as m=" ";
  3. Allows copying for:
    • Tags like <pre>, <code><textarea>, and <blockquote> - because those are usually meant for sharing.
    • Anything with a class like allow-copy or an ID like copy-area can be copied. You can change the class to your preferred exemption class or id.

Examples

<!-- Copying blocked -->
<p>This text can't be copied.</p>

<!-- Copying allowed -->
<p class="allow-copy">This text can be copied.</p>
<div id="copy-area">This area is also copyable.</div>

Need to Allow Copying Everywhere?

If you change your mind to allow copying contents on a post or a page, just add this in the post through the HTML view or Code editor interface:

<script>document.removeEventListener("copy",()=>{});</script>

Simple as it is

This script is simple, light, and flexible. It protects your content but still lets you make exceptions when needed. Copy, paste, and you’re done!

Please write your comments or send a webmention
Webmention Notice

This site accepts Webmentions via webmention.io/www.sdavidprince.space/webmention. You can also reply to syndicated posts, and your responses may be backfed (posted back here) via brid.gy.

What does all these mean? Read more about it. Respect this and keep mentions relevant.

Dark/Light
Font Size
A-
A+
Best Personal Blogs About Life - OnToplist.com
Translate

Explore: Navigation