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
- Blocks copying everywhere by default.
- 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=" ";
- 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 likecopy-area
can be copied. You can change the class to your preferred exemption class or id.
- Tags like
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!