Skip to content
This repository was archived by the owner on Sep 28, 2024. It is now read-only.

Commit 88f5730

Browse files
authored
v1.2.0
Add : Disable dangerous functions Add : Detect Suspect Actions
1 parent 56d58ae commit 88f5730

File tree

4 files changed

+208
-31
lines changed

4 files changed

+208
-31
lines changed

README.md

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
# PageGuard.js
22
No copying, printing as well as opening developers tools.
33

4-
Only **1.6KB** after gzipped.
5-
64
It can prevents user from :
75
* Select
86
* Print
97
* Right click
108
* Crtl / Shift / Alt / F12
119
* Open Developers Tools (Including opening in a separate window)
10+
* Run some scripts
11+
* Move mouse out of the page
1212

1313
You can use CSS if you don't run Javascript. But for safety, you should use this javascript and make your page only show when the Javscript is on.
1414

1515
## Uasge
1616

17-
Download or
18-
```
19-
<script type="text/javascript" src="https://netrvin.github.io/PageGuard.js/pageguard.min.js"></script>
17+
Download and install `pageguard.min.js`
2018
```
2119
### AntiCopy
2220
```
23-
var anticopy_id = PageGuard.antiCopy();
21+
var anticopy_key = PageGuard.antiCopy();
2422
```
2523
2624
You can use the following codes to allow user to copy again (Cannot clear the CSS):
2725
```
28-
PageGuard.allowCopy(anticopy_id);
26+
PageGuard.allowCopy(anticopy_key);
2927
```
3028
3129
### Detect Developers Tools
@@ -39,18 +37,47 @@ Supported:
3937
4038
It can only run one at the same time.
4139
```
42-
var detect_key = PageGuard.detectDevTools(function () {
40+
var detectDevTools_key = PageGuard.detectDevTools(function () {
4341
// Your codes will run when developers tools is opening
4442
});
4543
```
4644
4745
You can also use the following codes to stop detecting:
4846
```
49-
PageGuard.stopDetecting(detect_key);
47+
PageGuard.stopDetectingDevTools(detectDevTools_key);
5048
```
5149
50+
### Detect Suspect Actions
51+
Detect:
52+
* Focus and blur
53+
* Mouse leave and enter
54+
55+
```
56+
var detectSuspect_key = PageGuard.detectSuspectActions(function(){
57+
// Run when it begins
58+
},function(){
59+
// Run when it ends
60+
});
61+
62+
```
63+
64+
Stop it:
65+
```
66+
PageGuard.stopDetectingSuspectActions(detectSuspect_key);
67+
```
68+
69+
### Disable dangerous functions
70+
This will disable follwing functions to keep users from running some scripts:
71+
* window.open (Open a new window with copy-able contents)
72+
* URL.createObjectURL (Generate files to download)
73+
```
74+
PageGuard.disableFunctions();
75+
```
76+
**Warning:** With disabling these functions, your codes may not work well
77+
78+
5279
## Safe Tips
53-
Don't let user get the anticopy_id and the detect_key.
80+
Don't let user get the key.
5481
You can write your codes like this:
5582
```
5683
(function () {
@@ -60,10 +87,7 @@ You can write your codes like this:
6087
6188
## Addons
6289
### Anti Copy & Print (CSS)
63-
Download or
64-
```
65-
<link href="https://netrvin.github.io/PageGuard.js/anticopy.min.css" rel="stylesheet">
66-
```
90+
Download and install `anticopy.min.css`
6791
6892
## Examples
6993
https://netrvin.github.io/PageGuard.js/example.html

example.html

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323

2424
<script>
2525
(function () {
26-
var anticopy_id = PageGuard.antiCopy();
27-
setTimeout(function () {
28-
PageGuard.allowCopy(anticopy_id);
29-
alert('You can copy now');
30-
}, 10000);
31-
3226
function ready(func) {
3327
if (document.addEventListener) {
3428
document.addEventListener('DOMContentLoaded', function () {
@@ -44,15 +38,33 @@
4438
});
4539
}
4640
}
47-
41+
var detectDevTools_key = 0;
4842
ready(function () {
49-
var detect_key = PageGuard.detectDevTools(function () {
43+
detect_key = PageGuard.detectDevTools(function () {
5044
window.document.body.innerHTML = 'No copying or analyzing.';
51-
PageGuard.stopDetecting(detect_key);
5245
// console.clear(); // Sometimes it may makes this page crash
5346
});
5447
});
5548

49+
var HTML;
50+
51+
var detectSuspect_key = PageGuard.detectSuspectActions(function () {
52+
HTML = document.body.innerHTML;
53+
document.body.innerHTML = 'Move your mouse back and click';
54+
}, function () {
55+
document.body.innerHTML = HTML;
56+
});
57+
58+
PageGuard.disableFunctions();
59+
60+
var anticopy_key = PageGuard.antiCopy();
61+
setTimeout(function () {
62+
PageGuard.allowCopy(anticopy_key);
63+
PageGuard.stopDetecting(detectDevTools_key);
64+
PageGuard.stopDetectingSuspectActions(detectSuspect_key);
65+
alert('You can copy now');
66+
}, 20000);
67+
5668
})();
5769
</script>
5870
</body>

pageguard.js

Lines changed: 147 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* PageGuard.js v1.1.1 (https://github.com/Netrvin/PageGuard.js)
2+
* PageGuard.js v1.2.0 (https://github.com/Netrvin/PageGuard.js)
33
* Licensed under the MIT license
44
* Included some codes from https://github.com/sindresorhus/devtools-detect
55
* Used some codes from https://stackoverflow.com/questions/7798748/find-out-whether-chrome-console-is-open
@@ -13,11 +13,17 @@
1313
var allow_copy = true;
1414
var copy_key = 0;
1515
var copy_old_id = 0;
16+
var protect_function = false;
17+
//var protect_function_key = 0;
1618
var ele = 9;
1719

1820
var is_firefox = navigator.userAgent.indexOf('Firefox') != -1;
1921
var is_edge = navigator.userAgent.indexOf('Edge') != -1;
20-
var is_ie = navigator.userAgent.indexOf("MSIE") != -1 || (!!window.ActiveXObject || "ActiveXObject" in window) ;
22+
var is_ie = navigator.userAgent.indexOf("MSIE") != -1; // <= IE 10
23+
24+
var returnFalse = function (e) {
25+
return false;
26+
};
2127

2228
if (!is_ie) {
2329
var can_preventDefault = typeof Event.prototype.preventDefault === "function";
@@ -66,9 +72,6 @@
6672

6773
function antiCopy_old() {
6874
return setInterval(function () {
69-
var returnFalse = function (e) {
70-
return false;
71-
};
7275
document.oncontextmenu = returnFalse;
7376
document.oncopy = returnFalse;
7477
document.onselectstart = returnFalse;
@@ -283,7 +286,7 @@
283286
}
284287
};
285288

286-
PageGuard.stopDetecting = function (key) {
289+
PageGuard.stopDetectingDevTools = function (key) {
287290
if (is_detecting) {
288291
if (key == stop_key) {
289292
is_detecting = false;
@@ -298,5 +301,143 @@
298301
}
299302
};
300303

304+
PageGuard.stopDetecting = PageGuard.stopDetectingDevTools;
305+
306+
PageGuard.disableFunctions = function () {
307+
if (!protect_function) {
308+
protect_function = true;
309+
window.open = function () {
310+
return false;
311+
};
312+
if (!is_ie) {
313+
URL.createObjectURL = function () {
314+
return false;
315+
};
316+
}
317+
return true;
318+
} else {
319+
return false;
320+
}
321+
};
322+
323+
var is_detecting_suspect = false;
324+
var detect_suspect_key = 0;
325+
var ds_status = false;
326+
var focus_status = true;
327+
var mouse_status = true;
328+
var on_ds_begin = returnFalse;
329+
var on_ds_end = returnFalse;
330+
var ds_interval_id = 0;
331+
332+
var withoutChildFunction = function (func) {
333+
return function (e) {
334+
var parent = e.relatedTarget;
335+
while (parent != this && parent) {
336+
try {
337+
parent = parent.parentNode;
338+
} catch (e) {
339+
break;
340+
}
341+
}
342+
if (parent != this)
343+
func(e);
344+
}
345+
}
346+
347+
PageGuard.detectSuspectActions = function (func1, func2) {
348+
if (!is_detecting_suspect) {
349+
is_detecting_suspect = true;
350+
detect_suspect_key = Math.random();
351+
on_ds_begin = withoutChildFunction(function (e) {
352+
if (!ds_status) {
353+
ds_status = true;
354+
func1();
355+
} 
356+
mouse_status = false;
357+
});
358+
on_ds_end = withoutChildFunction(function (e) {
359+
if (ds_status && focus_status) {
360+
ds_status = false;
361+
func2();
362+
}
363+
mouse_status = true;
364+
});
365+
if (!is_ie) {
366+
document.addEventListener('mouseover', on_ds_end, true);
367+
document.addEventListener('mouseout', on_ds_begin, true);
368+
}
369+
ds_interval_id = setInterval(function () {
370+
window.onblur = function () {
371+
if (!ds_status) {
372+
ds_status = true;
373+
func1();
374+
}
375+
focus_status = false;
376+
};
377+
window.onfocus = function () {
378+
if (ds_status && mouse_status) {
379+
ds_status = false;
380+
func2();
381+
}
382+
focus_status = true;
383+
};
384+
}, 100);
385+
return detect_suspect_key;
386+
} else {
387+
return false;
388+
}
389+
};
390+
391+
PageGuard.stopDetectingSuspectActions = function (key) {
392+
if (is_detecting_suspect) {
393+
if (key == detect_suspect_key) {
394+
is_detecting_suspect = false;
395+
if (!is_ie) {
396+
document.removeEventListener('mouseover', on_ds_end, true);
397+
document.removeEventListener('mouseout', on_ds_begin, true);
398+
}
399+
clearInterval(ds_interval_id);
400+
window.onblur = null;
401+
window.onfocus = null;
402+
return true;
403+
} else {
404+
return false;
405+
}
406+
} else {
407+
return false;
408+
}
409+
};
410+
411+
// Need to be fixed
412+
/*
413+
function randomText(len) {
414+
var s = '';
415+
var chars = 'ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';  
416+
var char_len = chars.length;
417+
for (var i = 0; i < len; i++) {
418+
s += chars.charAt(Math.floor(Math.random() * char_len));
419+
}
420+
return s;
421+
}
422+
423+
function confuseContent() {
424+
var ele_list = ['p', 'a', 'span', 'textarea'];
425+
for (var i=0;i<ele_list.length;i++) {
426+
var ele_clt=document.getElementsByTagName(ele_list[i]);
427+
for (var j=0;j<ele_clt.length;j++) {
428+
var new_span = document.createElement('span');
429+
new_span.innerText = randomText(100);
430+
new_span.style.position = 'fixed !important';
431+
new_span.style['z-index'] = '-99999 !important';
432+
new_span.style.right = '-1000% !important';
433+
new_span.style.display = 'inline !important';
434+
ele_clt[j].parentNode.insertBefore(new_span, ele_clt[j]);
435+
}
436+
}
437+
}
438+
439+
PageGuard.confuseContent = confuseContent;
440+
*/
441+
301442
window.PageGuard = PageGuard;
302443
})();

pageguard.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)