Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
Add : Disable dangerous functions
Add : Detect Suspect Actions
  • Loading branch information
Netrvin committed May 11, 2018
1 parent 56d58ae commit 88f5730
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 31 deletions.
52 changes: 38 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
# PageGuard.js
No copying, printing as well as opening developers tools.

Only **1.6KB** after gzipped.

It can prevents user from :
* Select
* Print
* Right click
* Crtl / Shift / Alt / F12
* Open Developers Tools (Including opening in a separate window)
* Run some scripts
* Move mouse out of the page

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.

## Uasge

Download or
```
<script type="text/javascript" src="https://netrvin.github.io/PageGuard.js/pageguard.min.js"></script>
Download and install `pageguard.min.js`
```
### AntiCopy
```
var anticopy_id = PageGuard.antiCopy();
var anticopy_key = PageGuard.antiCopy();
```
You can use the following codes to allow user to copy again (Cannot clear the CSS):
```
PageGuard.allowCopy(anticopy_id);
PageGuard.allowCopy(anticopy_key);
```
### Detect Developers Tools
Expand All @@ -39,18 +37,47 @@ Supported:
It can only run one at the same time.
```
var detect_key = PageGuard.detectDevTools(function () {
var detectDevTools_key = PageGuard.detectDevTools(function () {
// Your codes will run when developers tools is opening
});
```
You can also use the following codes to stop detecting:
```
PageGuard.stopDetecting(detect_key);
PageGuard.stopDetectingDevTools(detectDevTools_key);
```
### Detect Suspect Actions
Detect:
* Focus and blur
* Mouse leave and enter
```
var detectSuspect_key = PageGuard.detectSuspectActions(function(){
// Run when it begins
},function(){
// Run when it ends
});

```
Stop it:
```
PageGuard.stopDetectingSuspectActions(detectSuspect_key);
```
### Disable dangerous functions
This will disable follwing functions to keep users from running some scripts:
* window.open (Open a new window with copy-able contents)
* URL.createObjectURL (Generate files to download)
```
PageGuard.disableFunctions();
```
**Warning:** With disabling these functions, your codes may not work well
## Safe Tips
Don't let user get the anticopy_id and the detect_key.
Don't let user get the key.
You can write your codes like this:
```
(function () {
Expand All @@ -60,10 +87,7 @@ You can write your codes like this:
## Addons
### Anti Copy & Print (CSS)
Download or
```
<link href="https://netrvin.github.io/PageGuard.js/anticopy.min.css" rel="stylesheet">
```
Download and install `anticopy.min.css`
## Examples
https://netrvin.github.io/PageGuard.js/example.html
Expand Down
30 changes: 21 additions & 9 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@

<script>
(function () {
var anticopy_id = PageGuard.antiCopy();
setTimeout(function () {
PageGuard.allowCopy(anticopy_id);
alert('You can copy now');
}, 10000);

function ready(func) {
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', function () {
Expand All @@ -44,15 +38,33 @@
});
}
}

var detectDevTools_key = 0;
ready(function () {
var detect_key = PageGuard.detectDevTools(function () {
detect_key = PageGuard.detectDevTools(function () {
window.document.body.innerHTML = 'No copying or analyzing.';
PageGuard.stopDetecting(detect_key);
// console.clear(); // Sometimes it may makes this page crash
});
});

var HTML;

var detectSuspect_key = PageGuard.detectSuspectActions(function () {
HTML = document.body.innerHTML;
document.body.innerHTML = 'Move your mouse back and click';
}, function () {
document.body.innerHTML = HTML;
});

PageGuard.disableFunctions();

var anticopy_key = PageGuard.antiCopy();
setTimeout(function () {
PageGuard.allowCopy(anticopy_key);
PageGuard.stopDetecting(detectDevTools_key);
PageGuard.stopDetectingSuspectActions(detectSuspect_key);
alert('You can copy now');
}, 20000);

})();
</script>
</body>
Expand Down
153 changes: 147 additions & 6 deletions pageguard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* PageGuard.js v1.1.1 (https://github.com/Netrvin/PageGuard.js)
* PageGuard.js v1.2.0 (https://github.com/Netrvin/PageGuard.js)
* Licensed under the MIT license
* Included some codes from https://github.com/sindresorhus/devtools-detect
* Used some codes from https://stackoverflow.com/questions/7798748/find-out-whether-chrome-console-is-open
Expand All @@ -13,11 +13,17 @@
var allow_copy = true;
var copy_key = 0;
var copy_old_id = 0;
var protect_function = false;
//var protect_function_key = 0;
var ele = 9;

var is_firefox = navigator.userAgent.indexOf('Firefox') != -1;
var is_edge = navigator.userAgent.indexOf('Edge') != -1;
var is_ie = navigator.userAgent.indexOf("MSIE") != -1 || (!!window.ActiveXObject || "ActiveXObject" in window) ;
var is_ie = navigator.userAgent.indexOf("MSIE") != -1; // <= IE 10

var returnFalse = function (e) {
return false;
};

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

function antiCopy_old() {
return setInterval(function () {
var returnFalse = function (e) {
return false;
};
document.oncontextmenu = returnFalse;
document.oncopy = returnFalse;
document.onselectstart = returnFalse;
Expand Down Expand Up @@ -283,7 +286,7 @@
}
};

PageGuard.stopDetecting = function (key) {
PageGuard.stopDetectingDevTools = function (key) {
if (is_detecting) {
if (key == stop_key) {
is_detecting = false;
Expand All @@ -298,5 +301,143 @@
}
};

PageGuard.stopDetecting = PageGuard.stopDetectingDevTools;

PageGuard.disableFunctions = function () {
if (!protect_function) {
protect_function = true;
window.open = function () {
return false;
};
if (!is_ie) {
URL.createObjectURL = function () {
return false;
};
}
return true;
} else {
return false;
}
};

var is_detecting_suspect = false;
var detect_suspect_key = 0;
var ds_status = false;
var focus_status = true;
var mouse_status = true;
var on_ds_begin = returnFalse;
var on_ds_end = returnFalse;
var ds_interval_id = 0;

var withoutChildFunction = function (func) {
return function (e) {
var parent = e.relatedTarget;
while (parent != this && parent) {
try {
parent = parent.parentNode;
} catch (e) {
break;
}
}
if (parent != this)
func(e);
}
}

PageGuard.detectSuspectActions = function (func1, func2) {
if (!is_detecting_suspect) {
is_detecting_suspect = true;
detect_suspect_key = Math.random();
on_ds_begin = withoutChildFunction(function (e) {
if (!ds_status) {
ds_status = true;
func1();
} 
mouse_status = false;
});
on_ds_end = withoutChildFunction(function (e) {
if (ds_status && focus_status) {
ds_status = false;
func2();
}
mouse_status = true;
});
if (!is_ie) {
document.addEventListener('mouseover', on_ds_end, true);
document.addEventListener('mouseout', on_ds_begin, true);
}
ds_interval_id = setInterval(function () {
window.onblur = function () {
if (!ds_status) {
ds_status = true;
func1();
}
focus_status = false;
};
window.onfocus = function () {
if (ds_status && mouse_status) {
ds_status = false;
func2();
}
focus_status = true;
};
}, 100);
return detect_suspect_key;
} else {
return false;
}
};

PageGuard.stopDetectingSuspectActions = function (key) {
if (is_detecting_suspect) {
if (key == detect_suspect_key) {
is_detecting_suspect = false;
if (!is_ie) {
document.removeEventListener('mouseover', on_ds_end, true);
document.removeEventListener('mouseout', on_ds_begin, true);
}
clearInterval(ds_interval_id);
window.onblur = null;
window.onfocus = null;
return true;
} else {
return false;
}
} else {
return false;
}
};

// Need to be fixed
/*
function randomText(len) {
var s = '';
var chars = 'ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';  
var char_len = chars.length;
for (var i = 0; i < len; i++) {
s += chars.charAt(Math.floor(Math.random() * char_len));
}
return s;
}
function confuseContent() {
var ele_list = ['p', 'a', 'span', 'textarea'];
for (var i=0;i<ele_list.length;i++) {
var ele_clt=document.getElementsByTagName(ele_list[i]);
for (var j=0;j<ele_clt.length;j++) {
var new_span = document.createElement('span');
new_span.innerText = randomText(100);
new_span.style.position = 'fixed !important';
new_span.style['z-index'] = '-99999 !important';
new_span.style.right = '-1000% !important';
new_span.style.display = 'inline !important';
ele_clt[j].parentNode.insertBefore(new_span, ele_clt[j]);
}
}
}
PageGuard.confuseContent = confuseContent;
*/

window.PageGuard = PageGuard;
})();
4 changes: 2 additions & 2 deletions pageguard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88f5730

Please sign in to comment.