-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
238 lines (156 loc) · 6.33 KB
/
main.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
class Device {
static get isMobile(){
document.write(navigator.userAgent)
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
}
static permission(type){
navigator.permissions.query({name:type}).then( result => {
return result.state
})
}}
class WindowManager{
constructor() {
this.setup()
this.tab_change_num = 0
this.pointer_conter = null
}
setup(){
this.visibilityChangedDetector()
this.pointerDetector()
this.windowSizeTracker()
}
windowSizeTracker(){
window.addEventListener('resize', (e) => {
widthRation = window.outerWidth/window.screen.width;
heightRation = window.outerHeight/window.screen.height;
})}
pointerDetector(){
document.addEventListener('pointerleave', (e) => {
if(widthRation<0.9){
this.pointer_conter++;
pointerHandler(this.pointer_conter, this.tab_change_num, true);
}
});
document.addEventListener('pointerover', (event) => {
pointerHandler(this.pointer_conter, this.tab_change_num, false);
})}
visibilityChangedDetector() {
// this event fires when ever the user changes the tab*
document.addEventListener("visibilitychange", event => {
if (document.visibilityState === "visible") {
this.tab_change_num++
pointerHandler(this.pointer_conter, this.tab_change_num, true)
}
})
}
}
class EventBlocker{ // this class block some of the browser features
constructor() {
this.setup();
}
setup(){
// this.googleTranslateBlocker();
// this.clipboardEventCapture();
this.textOperationDisable()
this.pagePrintBlocker();
this.rightClickEventCapture();
}
googleTranslateBlocker(mode=true) {
// blocking Google translator
let value="yes";
if(!mode){
value="no";
document.getElementById("disable-translation-btn").classList.add("hide")
document.getElementById("enable-translation-btn").classList.remove("hide")
}else {
document.getElementById("disable-translation-btn").classList.remove("hide")
document.getElementById("enable-translation-btn").classList.add("hide")
}
let htmlTag = document.getElementsByTagName("html")[0];
htmlTag.setAttribute("translate", value);
}
rightClickEventCapture(target="right-click-block"){
// blocking right click for any element that has the class name *right-click-img-block*
let elements = document.getElementsByClassName(target);
for (const elm of elements) {
elm.addEventListener('contextmenu', e =>{
notificationAlert("Right Click")
e.preventDefault()}, false);
}
}
textOperationDisable(mode){
if(mode){
}else{
}
}
clipboardEventCapture(mode){
let copy_elm = document.getElementsByClassName("no_copy");
// blocking copy and drag features for any elements that has the class name *no_copy*
if(mode){
document.getElementById("btn-copy-off").classList.remove("hide")
document.getElementById("btn-copy-on").classList.add("hide")
for (const ele of copy_elm) {
ele.addEventListener('copy', disable, true);
ele.addEventListener('dragstart', disable, true);
}
}else {
document.getElementById("btn-copy-off").classList.add("hide")
document.getElementById("btn-copy-on").classList.remove("hide")
for (const ele of copy_elm) {
ele.removeEventListener('copy', disable, true);
ele.removeEventListener('dragstart', disable, true);
}}
}
pagePrintBlocker(){
function hideContent(){
bodyElem.setAttribute("class", "hide");
}
function restoreContent(){
bodyElem.removeAttribute("class")
}
let bodyElem = document.getElementsByTagName("body")[0]
window.addEventListener('beforeprint', hideContent);
window.addEventListener('afterprint', restoreContent);
}
}
function notificationAlert(operation) {
// temp function to handle and receive the events and show an alert
if (operation=="Copy" || operation=="Drag"){
document.getElementById("no_copy").classList.add("copy-warning")
setTimeout(()=>{document.getElementById("no_copy").classList.remove("copy-warning")}, 300)
}
else if(operation=="Right Click"){
document.getElementById("img--").classList.add("copy-warning")
setTimeout(()=>{document.getElementById("img--").classList.remove("copy-warning")}, 300)
}
}
function pointerHandler(pointerNum, tabNum, warning) {
// temp function for slides
const card_warning = document.getElementById("card-5");
const output = document.getElementById('output');
if(warning){
output.classList.add("warning");
}else if(!warning){
output.classList.remove("warning");
}
output.innerText = `
Pointer Out: ${pointerNum} times
page was hidden: ${tabNum} times
`
}
function disable(e) {
e.preventDefault()
notificationAlert("Copy")
}
let widthRation ;
let heightRation;
let blocker = new EventBlocker()
new WindowManager()
let disable_trans_btn = document.getElementById("disable-translation-btn");
let enable_trans_btn=document.getElementById("enable-translation-btn");
disable_trans_btn.addEventListener('click',()=>{blocker.googleTranslateBlocker(false)});
enable_trans_btn.addEventListener('click', ()=>{blocker.googleTranslateBlocker(true)});
let btn_copy_on = document.getElementById("btn-copy-on");
let btn_copy_off=document.getElementById("btn-copy-off");
btn_copy_on.addEventListener('click',()=>{blocker.clipboardEventCapture(true)});
btn_copy_off.addEventListener('click', ()=>{blocker.clipboardEventCapture(false)});