New Answer
You want to automatically omit certain parts of the text when users select and copy it.
I know two solutions for this:
- Put the text in different parent elements
- Hide the unwanted elements when the user starts selecting
Solution #1 is usually used for source code with line numbers. By clever applying CSS, two DIV's are aligned vertically so that each line number appears to be on the left of the correct line of source code (which is actually in a different DIV).
That way, you can drag your selection in the source code without getting any of the line numbers. This works well for information that can be displayed in boxes that are vertically/horizontally aligned or, to use a different picture, if you could put things into different table cells.
Solution #2 responds to the first mouse click and applies display: none
to elements with a certain class like omit-during-copy
.
The advantage of this approach is that the user can see what they are copying (i.e. the unwanted information is vanishing).
The disadvantage is how/when to revert the state.
A variant of this is to use absolute positioning to make elements appear in a certain place. This becomes tedious very quickly if your have to apply the technique to things like aHIDEb
- you will have to put enough empty space between a
and b
to display HIDE
between them, then place HIDE
pixel perfect in the gap. Not impossible but I'd try to rearrange the information first.
Old Answer
There are two ways to prevent a user from copying text from your HTML page:
- Don't display it
- Use a password field
Let me explain: There is a small amount of things that you can try with JavaScript in the page itself. If you swallow Ctrl+C, then people will use the mouse. If you swallow the mouse button in the page, they will use the menu bar. If you disable the menu bar of the browser, people will deactivate the option "Allow JavaScript to hide the menu bar".
If you use a password field, people will use the JavaScript console or the "Show Source" or they'll use tools like Tampermonkey to get rid of your pesky intrusion into their lives.
You can try to display an image with the text instead. People will then use OCR. So even if you used a Flash-based HTML viewer to replace the whole page, people could still copy the text. But it would make them pretty mad.
Personal note: I tend to visit pages which try things like that once and briefly at that. I tend to remember such sites for a long time and with very negative emotions. So if I were you, I try this only to drive people away from my site.