-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (107 loc) · 3.04 KB
/
index.js
File metadata and controls
115 lines (107 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Protector {
constructor(options) {
this.options = options || {
selectAll: true,
copy: true,
paste: true,
cut: true,
save: true,
viewSource: true,
print: true,
disableRightClick: true,
disableTextSelection: true,
disableImageDragging: true,
};
this.keys = {
selectAll: 65, // CTRL + A
copy: 67, // CTRL + C
paste: 86, // CTRL + V
cut: 88, // CTRL + X
save: 83, // CTRL + S
viewSource: 85, // CTRL + U
print: 80, // CTRL + P
};
}
init() {
const {
disableRightClick,
disableTextSelection,
disableImageDragging,
} = this.options;
for (let key in this.options) {
if (this.options[key]) {
this.disableKey(this.keys[key]);
}
}
if (disableRightClick) this.disableRightClick();
if (disableTextSelection) this.disableTextSelection();
if (disableImageDragging) this.disableImageDragging();
}
disableSelectorCSS() {
const css = document.createElement("style");
css.type = "text/css";
css.innerText =
"* {-webkit-user-select: none !important; -moz-user-select: none !important; -ms-user-select: none !important; user-select: none !important; }";
document.head.appendChild(css);
}
disableRightClickDocumentStyles() {
document.body.style.cursor = "default";
if (
typeof document.documentElement.style.webkitTouchCallout !== "undefined"
) {
document.documentElement.style.webkitTouchCallout = "none";
}
if (
typeof document.documentElement.style.webkitUserSelect !== "undefined"
) {
document.documentElement.style.webkitUserSelect = "none";
}
}
disableKey(keyCode) {
window.addEventListener("keydown", e => {
if (
(e.ctrlKey && e.which === keyCode) ||
(e.metaKey && e.which === keyCode)
)
e.preventDefault();
});
document.keypress = e => {
if (
(e.ctrlKey && e.which === keyCode) ||
(e.metaKey && e.which === keyCode)
)
return false;
};
}
disableRightClick() {
document.oncontextmenu = e => {
const t = e || window.event;
const n = t.target || t.srcElement;
if (n.nodeName !== "A") return false;
};
document.body.oncontextmenu = () => false;
document.onmousedown = e => {
if (e.button === 2) return false;
};
}
disableTextSelection() {
if (typeof document.body.onselectstart !== "undefined") {
document.body.onselectstart = () => false;
}
if (typeof document.body.style.MozUserSelect !== "undefined") {
document.body.style.MozUserSelect = "none";
}
if (typeof document.body.style.webkitUserSelect !== "undefined") {
document.body.style.webkitUserSelect = "none";
}
if (typeof document.body.onmousedown !== "undefined") {
document.body.onmousedown = () => false;
}
this.disableRightClickDocumentStyles();
this.disableSelectorCSS();
}
disableImageDragging() {
document.ondragstart = () => false;
}
}
export default Protector;