UNPKG

protect-scr

Version:

Comprehensive client-side security protection for React applications against screenshots, printing, and unauthorized access

195 lines 7.7 kB
export class KeyboardProtector { constructor(config) { this.isActive = false; this.eventListeners = []; this.config = config; } start() { if (this.isActive) return; this.isActive = true; this.setupKeyboardInterception(); } stop() { if (!this.isActive) return; this.isActive = false; this.eventListeners.forEach(removeListener => removeListener()); this.eventListeners = []; } setupKeyboardInterception() { const keyHandler = (e) => { var _a, _b; const key = e.key.toLowerCase(); const keyCode = e.keyCode || e.which; let blocked = false; let action = ''; // F12 - Developer Tools if (key === 'f12' || keyCode === 123) { blocked = true; action = 'F12 Developer Tools'; } // Ctrl+Shift+I - Developer Tools if (e.ctrlKey && e.shiftKey && (key === 'i' || keyCode === 73)) { blocked = true; action = 'Ctrl+Shift+I Developer Tools'; } // Ctrl+Shift+J - Console if (e.ctrlKey && e.shiftKey && (key === 'j' || keyCode === 74)) { blocked = true; action = 'Ctrl+Shift+J Console'; } // Ctrl+Shift+C - Element Inspector if (e.ctrlKey && e.shiftKey && (key === 'c' || keyCode === 67)) { blocked = true; action = 'Ctrl+Shift+C Element Inspector'; } // Ctrl+U - View Source if (e.ctrlKey && (key === 'u' || keyCode === 85)) { blocked = true; action = 'Ctrl+U View Source'; } // Ctrl+S - Save Page if (e.ctrlKey && (key === 's' || keyCode === 83)) { blocked = true; action = 'Ctrl+S Save Page'; } // Ctrl+P - Print if (e.ctrlKey && (key === 'p' || keyCode === 80)) { blocked = true; action = 'Ctrl+P Print'; } // Ctrl+A - Select All if (e.ctrlKey && (key === 'a' || keyCode === 65)) { blocked = true; action = 'Ctrl+A Select All'; } // F5, Ctrl+R, Ctrl+F5 - Refresh if (key === 'f5' || keyCode === 116 || (e.ctrlKey && (key === 'r' || keyCode === 82)) || (e.ctrlKey && key === 'f5')) { blocked = true; action = 'Page Refresh'; } // Ctrl+Shift+Delete - Clear browsing data if (e.ctrlKey && e.shiftKey && (key === 'delete' || keyCode === 46)) { blocked = true; action = 'Ctrl+Shift+Delete Clear Data'; } // Alt+F4 - Close window if (e.altKey && (key === 'f4' || keyCode === 115)) { blocked = true; action = 'Alt+F4 Close Window'; } // Ctrl+W - Close tab if (e.ctrlKey && (key === 'w' || keyCode === 87)) { blocked = true; action = 'Ctrl+W Close Tab'; } // Ctrl+T - New tab if (e.ctrlKey && (key === 't' || keyCode === 84)) { blocked = true; action = 'Ctrl+T New Tab'; } // Ctrl+N - New window if (e.ctrlKey && (key === 'n' || keyCode === 78)) { blocked = true; action = 'Ctrl+N New Window'; } // Ctrl+Shift+N - New incognito window if (e.ctrlKey && e.shiftKey && (key === 'n' || keyCode === 78)) { blocked = true; action = 'Ctrl+Shift+N Incognito Window'; } // Ctrl+H - History if (e.ctrlKey && (key === 'h' || keyCode === 72)) { blocked = true; action = 'Ctrl+H History'; } // Ctrl+J - Downloads if (e.ctrlKey && (key === 'j' || keyCode === 74) && !e.shiftKey) { blocked = true; action = 'Ctrl+J Downloads'; } // Ctrl+Shift+O - Bookmarks if (e.ctrlKey && e.shiftKey && (key === 'o' || keyCode === 79)) { blocked = true; action = 'Ctrl+Shift+O Bookmarks'; } // Ctrl+D - Bookmark page if (e.ctrlKey && (key === 'd' || keyCode === 68)) { blocked = true; action = 'Ctrl+D Bookmark Page'; } // Ctrl+F - Find if (e.ctrlKey && (key === 'f' || keyCode === 70)) { blocked = true; action = 'Ctrl+F Find'; } // Ctrl+G - Find next if (e.ctrlKey && (key === 'g' || keyCode === 71)) { blocked = true; action = 'Ctrl+G Find Next'; } // Ctrl+Shift+G - Find previous if (e.ctrlKey && e.shiftKey && (key === 'g' || keyCode === 71)) { blocked = true; action = 'Ctrl+Shift+G Find Previous'; } // Ctrl+Plus/Minus - Zoom if (e.ctrlKey && (key === '+' || key === '-' || key === '=' || keyCode === 187 || keyCode === 189)) { blocked = true; action = 'Ctrl+Plus/Minus Zoom'; } // Ctrl+0 - Reset zoom if (e.ctrlKey && (key === '0' || keyCode === 48)) { blocked = true; action = 'Ctrl+0 Reset Zoom'; } // Alt+Tab - Switch windows/apps if (e.altKey && (key === 'tab' || keyCode === 9)) { blocked = true; action = 'Alt+Tab Switch Windows'; } // Windows key combinations if (e.metaKey) { blocked = true; action = 'Windows Key Combination'; } // Escape key in certain contexts if (key === 'escape' || keyCode === 27) { // Allow escape in some contexts, but block in others // You can customize this based on your needs } if (blocked) { e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); (_b = (_a = this.config).onAttempt) === null || _b === void 0 ? void 0 : _b.call(_a, action); if (this.config.showWarning) { this.showWarning(action); } return false; } }; // Add event listeners for keydown and keypress document.addEventListener('keydown', keyHandler, true); document.addEventListener('keypress', keyHandler, true); this.eventListeners.push(() => { document.removeEventListener('keydown', keyHandler, true); document.removeEventListener('keypress', keyHandler, true); }); // Block specific key combinations on window level window.addEventListener('keydown', keyHandler, true); this.eventListeners.push(() => { window.removeEventListener('keydown', keyHandler, true); }); } showWarning(action) { const message = `${action} is disabled for security reasons.\n${this.config.warningMessage}`; if (typeof window !== 'undefined' && typeof window.alert === 'function') { alert(message); } } } //# sourceMappingURL=KeyboardProtector.js.map