-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-toggle.js
More file actions
441 lines (386 loc) Β· 15.8 KB
/
dev-toggle.js
File metadata and controls
441 lines (386 loc) Β· 15.8 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* Developer Toggle System for Browser UI Restrictions
* Ctrl + Shift + D to toggle between development and production modes
*/
class DevToggleManager {
constructor() {
this.restrictionsEnabled = true; // Default: restrictions ON (production mode)
this.originalStyles = new Map(); // Store original CSS styles
this.eventListeners = new Map(); // Store event listeners for removal
this.notification = null; // Reference to notification element
this.init();
}
init() {
// Set up keyboard shortcut listener
document.addEventListener('keydown', (e) => this.handleKeyDown(e));
// Apply initial restrictions
this.applyRestrictions();
console.log('π DevToggle: Restrictions enabled by default (Production mode)');
}
handleKeyDown(e) {
// Check for Ctrl + Shift + D
if (e.ctrlKey && e.shiftKey && e.key === 'D') {
e.preventDefault();
this.toggleRestrictions();
return false;
}
}
toggleRestrictions() {
this.restrictionsEnabled = !this.restrictionsEnabled;
if (this.restrictionsEnabled) {
this.applyRestrictions();
this.showNotification('π Restrictions Enabled: Dev mode OFF', 'enabled');
} else {
this.removeRestrictions();
this.showNotification('π Restrictions Disabled: Dev mode ON', 'disabled');
}
console.log(`π DevToggle: Restrictions ${this.restrictionsEnabled ? 'ENABLED' : 'DISABLED'}`);
}
applyRestrictions() {
this.applyCSSRestrictions();
this.applyJavaScriptRestrictions();
}
removeRestrictions() {
this.removeCSSRestrictions();
this.removeJavaScriptRestrictions();
}
applyCSSRestrictions() {
// Apply CSS restrictions to all elements
const style = document.createElement('style');
style.id = 'dev-restrictions-style';
style.textContent = `
* {
/* Prevent text selection and dragging on all elements */
-webkit-user-select: none !important;
-moz-user-select: none !important;
-ms-user-select: none !important;
user-select: none !important;
-webkit-user-drag: none !important;
-khtml-user-drag: none !important;
-moz-user-drag: none !important;
-o-user-drag: none !important;
user-drag: none !important;
/* Disable touch callouts on mobile */
-webkit-touch-callout: none !important;
/* Prevent zooming on mobile */
touch-action: manipulation !important;
}
html {
overflow: hidden !important;
}
body {
overflow: hidden !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
width: 100vw !important;
height: 100vh !important;
}
`;
// Remove existing restrictions style if present
const existing = document.getElementById('dev-restrictions-style');
if (existing) existing.remove();
document.head.appendChild(style);
}
removeCSSRestrictions() {
// Remove the restrictions style sheet
const restrictionsStyle = document.getElementById('dev-restrictions-style');
if (restrictionsStyle) {
restrictionsStyle.remove();
}
// Allow developers to scroll, select text, etc.
const devStyle = document.createElement('style');
devStyle.id = 'dev-override-style';
devStyle.textContent = `
* {
-webkit-user-select: auto !important;
-moz-user-select: auto !important;
-ms-user-select: auto !important;
user-select: auto !important;
-webkit-user-drag: auto !important;
-khtml-user-drag: auto !important;
-moz-user-drag: auto !important;
-o-user-drag: auto !important;
user-drag: auto !important;
-webkit-touch-callout: default !important;
touch-action: auto !important;
}
html {
overflow: auto !important;
}
body {
overflow: auto !important;
position: relative !important;
}
`;
document.head.appendChild(devStyle);
}
applyJavaScriptRestrictions() {
// Store and apply event listeners for restrictions
this.eventListeners.clear();
// Prevent right-click context menu
const contextMenuHandler = (e) => {
e.preventDefault();
return false;
};
document.addEventListener('contextmenu', contextMenuHandler);
this.eventListeners.set('contextmenu', contextMenuHandler);
// Prevent text selection
const selectStartHandler = (e) => {
e.preventDefault();
return false;
};
document.addEventListener('selectstart', selectStartHandler);
this.eventListeners.set('selectstart', selectStartHandler);
// Prevent dragging
const dragStartHandler = (e) => {
e.preventDefault();
return false;
};
document.addEventListener('dragstart', dragStartHandler);
this.eventListeners.set('dragstart', dragStartHandler);
// Prevent long-press on mobile
const touchStartHandler = (e) => {
if (e.touches.length > 1) {
e.preventDefault();
}
};
document.addEventListener('touchstart', touchStartHandler);
this.eventListeners.set('touchstart', touchStartHandler);
// Prevent mobile zooming gestures
const touchMoveHandler = (e) => {
if (e.touches.length > 1) {
e.preventDefault();
}
};
document.addEventListener('touchmove', touchMoveHandler, { passive: false });
this.eventListeners.set('touchmove', touchMoveHandler);
// Prevent double-tap zoom on iOS
let lastTouchEnd = 0;
const touchEndHandler = (e) => {
const now = Date.now();
if (now - lastTouchEnd <= 300) {
e.preventDefault();
}
lastTouchEnd = now;
};
document.addEventListener('touchend', touchEndHandler, false);
this.eventListeners.set('touchend', touchEndHandler);
// Block developer keyboard shortcuts (except our toggle)
const keyDownHandler = (e) => {
// Allow our toggle shortcut
if (e.ctrlKey && e.shiftKey && e.key === 'D') {
return true;
}
// Block F12, Ctrl+Shift+I, Ctrl+U, Ctrl+S, F5, Ctrl+R
if (
e.key === 'F12' ||
(e.ctrlKey && e.shiftKey && e.key === 'I') ||
(e.ctrlKey && e.key === 'u') ||
(e.ctrlKey && e.key === 's') ||
e.key === 'F5' ||
(e.ctrlKey && e.key === 'r')
) {
e.preventDefault();
return false;
}
};
document.addEventListener('keydown', keyDownHandler);
this.eventListeners.set('keydown', keyDownHandler);
}
removeJavaScriptRestrictions() {
// Remove all restriction event listeners
this.eventListeners.forEach((handler, event) => {
document.removeEventListener(event, handler);
});
this.eventListeners.clear();
// Remove the dev override style
const devStyle = document.getElementById('dev-override-style');
if (devStyle) {
devStyle.remove();
}
}
showNotification(message, type) {
// Remove existing notification
if (this.notification) {
this.notification.remove();
}
// Create notification element
this.notification = document.createElement('div');
this.notification.className = `dev-notification ${type}`;
this.notification.textContent = message;
// Style the notification
Object.assign(this.notification.style, {
position: 'fixed',
bottom: '20px',
right: '20px',
padding: '12px 20px',
borderRadius: '8px',
fontSize: '14px',
fontWeight: 'bold',
color: 'white',
zIndex: '999999',
fontFamily: 'Arial, sans-serif',
boxShadow: '0 4px 12px rgba(0, 0, 0, 0.3)',
transition: 'all 0.3s ease',
transform: 'translateY(100px)',
opacity: '0'
});
// Set background color based on type
if (type === 'enabled') {
this.notification.style.background = 'linear-gradient(135deg, #e74c3c, #c0392b)';
} else {
this.notification.style.background = 'linear-gradient(135deg, #27ae60, #229954)';
}
document.body.appendChild(this.notification);
// Animate in
requestAnimationFrame(() => {
this.notification.style.transform = 'translateY(0)';
this.notification.style.opacity = '1';
});
// Auto-hide after 3 seconds
setTimeout(() => {
if (this.notification) {
this.notification.style.transform = 'translateY(100px)';
this.notification.style.opacity = '0';
setTimeout(() => {
if (this.notification) {
this.notification.remove();
this.notification = null;
}
}, 300);
}
}, 3000);
}
createDevPanel() {
// Create developer panel
const devPanel = document.createElement('div');
devPanel.id = 'dev-panel';
devPanel.className = 'dev-panel hidden';
devPanel.innerHTML = `
<div class="dev-panel-header">
<h3>π Developer Panel</h3>
<button class="dev-panel-close">Γ</button>
</div>
<div class="dev-panel-content">
<div class="dev-section">
<h4>Service Worker & Cache Management</h4>
<div class="dev-buttons">
<button id="cleanSwBtn" class="dev-btn primary">π§Ή Clean SW & Caches</button>
<button id="cleanAllBtn" class="dev-btn warning">π Clean Everything</button>
<button id="swStatusBtn" class="dev-btn info">π SW Status</button>
</div>
</div>
<div class="dev-section">
<h4>Quick Actions</h4>
<div class="dev-buttons">
<button id="refreshBtn" class="dev-btn">π Hard Refresh</button>
<button id="clearLocalStorageBtn" class="dev-btn">πΎ Clear localStorage</button>
</div>
</div>
<div class="dev-section">
<h4>Debug Info</h4>
<div id="debugInfo" class="debug-info">
<p>Press Ctrl+Shift+D to toggle restrictions</p>
<p>Check console for detailed logs</p>
</div>
</div>
</div>
`;
document.body.appendChild(devPanel);
this.setupDevPanelEvents(devPanel);
return devPanel;
}
setupDevPanelEvents(panel) {
// Close button
panel.querySelector('.dev-panel-close').addEventListener('click', () => {
panel.classList.add('hidden');
});
// Clean SW & Caches
panel.querySelector('#cleanSwBtn').addEventListener('click', async () => {
if (window.swCleanup) {
console.log('π§Ή Starting Service Worker cleanup...');
await window.swCleanup.performFullCleanup();
} else {
console.error('SW Cleanup utility not available');
}
});
// Clean Everything
panel.querySelector('#cleanAllBtn').addEventListener('click', async () => {
if (confirm('This will clear all caches, service workers, and localStorage. Continue?')) {
if (window.swCleanup) {
console.log('π Starting complete cleanup...');
await window.swCleanup.performCompleteCleanup();
alert('Complete cleanup finished. Consider refreshing the page.');
} else {
console.error('SW Cleanup utility not available');
}
}
});
// SW Status
panel.querySelector('#swStatusBtn').addEventListener('click', async () => {
if (window.swCleanup) {
const status = await window.swCleanup.getStatus();
console.log('π SW Status:', status);
const debugInfo = panel.querySelector('#debugInfo');
debugInfo.innerHTML = `
<p><strong>Service Workers:</strong> ${status.serviceWorkers}</p>
<p><strong>Caches:</strong> ${status.caches}</p>
<p><strong>SW Supported:</strong> ${status.isSupported}</p>
<p><em>Check console for detailed logs</em></p>
`;
}
});
// Hard Refresh
panel.querySelector('#refreshBtn').addEventListener('click', () => {
window.location.reload(true);
});
// Clear localStorage
panel.querySelector('#clearLocalStorageBtn').addEventListener('click', () => {
if (confirm('Clear all localStorage data?')) {
localStorage.clear();
console.log('πΎ localStorage cleared');
alert('localStorage cleared');
}
});
}
toggleDevPanel() {
let panel = document.getElementById('dev-panel');
if (!panel) {
panel = this.createDevPanel();
}
panel.classList.toggle('hidden');
}
// Enhanced toggle to include dev panel
toggleRestrictions() {
this.restrictionsEnabled = !this.restrictionsEnabled;
if (this.restrictionsEnabled) {
this.applyRestrictions();
this.showNotification('π Restrictions Enabled: Dev mode OFF', 'enabled');
// Hide dev panel when restrictions are enabled
const panel = document.getElementById('dev-panel');
if (panel) panel.classList.add('hidden');
} else {
this.removeRestrictions();
this.showNotification('π Restrictions Disabled: Dev mode ON', 'disabled');
// Show dev panel when restrictions are disabled
setTimeout(() => this.toggleDevPanel(), 500);
}
console.log(`π DevToggle: Restrictions ${this.restrictionsEnabled ? 'ENABLED' : 'DISABLED'}`);
}
// Public method to get current state
getState() {
return {
restrictionsEnabled: this.restrictionsEnabled,
mode: this.restrictionsEnabled ? 'production' : 'development'
};
}
}
// Initialize the dev toggle manager when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
window.devToggle = new DevToggleManager();
});
} else {
window.devToggle = new DevToggleManager();
}