Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from copying text on mobile browsers

I'm trying to develop a typing speed competition using JavaScript. People should write all the words they see from a div to a textarea.

To prevent cheating (like copying the words from div) one way is check the written words only when a keyboard key is down, but I was wondering if there is a way to prevent the user from copying the text in a browser?

What I have tried so far:

  1. Disable right click (didn't work on mobile browsers)
  2. Show an alert using the onmousedown event in all the page (it didn't work either)

Using any libraries is OK.

like image 682
Jafar Akhondali Avatar asked Sep 26 '15 10:09

Jafar Akhondali


People also ask

How do I stop people from copying text from a website?

If you are a website owner and would like to prevent your content from being copied, we recommend disabling copy and right-click buttons from posts and pages. Another way of copying a website content is by using hotkeys like Ctrl+V, Ctrl+C, Ctrl+A, etc.

How do I turn off text copying?

You can allow text selection, but prevent copy and cut functions using the oncopy, oncut and onpaste event attributes. By adding these attributes into a textbox's <input> tag, you can disable cut, copy and paste features. The user is left with the option to enter the field manually with these attributes set.

How do websites block copy and paste?

If a browser will not allow you to copy and paste, the website you're visiting disabled text selection. You can easily fix the text selection problem if you switch to a different browser. Disabling Javascript by following our method will also get rid of the text selection issue.


1 Answers

You can simply make the text into an image.

<style type="text/css">
div.image {
    width: 100px;
    height: 100px;
    background-image: url-to-your-image;
}
</style>

To generate the images you can use a server side script as in the aswers of this question

or something like this:

<?php
header("Content-type: image/png");
$im = @imagecreate(210, 30)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 4, 5, 5,  "This is a test", $text_color);
imagepng($im);
imagedestroy($im);
?> 

Test here

like image 160
Max Avatar answered Oct 18 '22 21:10

Max