protect-scr
Version:
Comprehensive client-side security protection for React applications against screenshots, printing, and unauthorized access
114 lines • 4.04 kB
JavaScript
export class DevToolsDetector {
constructor(config) {
this.isDetecting = false;
this.thresholds = {
low: 200,
medium: 160,
high: 120
};
this.config = config;
}
start() {
if (this.isDetecting)
return;
this.isDetecting = true;
// Method 1: Window size detection
this.startSizeDetection();
// Method 2: Console detection
this.startConsoleDetection();
// Method 3: Debugger detection
this.startDebuggerDetection();
// Method 4: Performance timing detection
this.startPerformanceDetection();
}
stop() {
if (!this.isDetecting)
return;
this.isDetecting = false;
if (this.checkInterval) {
clearInterval(this.checkInterval);
this.checkInterval = undefined;
}
}
startSizeDetection() {
const threshold = this.thresholds[this.config.sensitivity];
this.checkInterval = setInterval(() => {
const widthDiff = window.outerWidth - window.innerWidth;
const heightDiff = window.outerHeight - window.innerHeight;
if (widthDiff > threshold || heightDiff > threshold) {
this.onDevToolsDetected();
}
}, 500);
}
startConsoleDetection() {
// Create a custom console object that detects access
const consoleDetector = {
log: () => this.onDevToolsDetected(),
warn: () => this.onDevToolsDetected(),
error: () => this.onDevToolsDetected(),
info: () => this.onDevToolsDetected(),
debug: () => this.onDevToolsDetected(),
clear: () => this.onDevToolsDetected(),
dir: () => this.onDevToolsDetected(),
table: () => this.onDevToolsDetected()
};
// Detect console usage
let consoleUsageDetected = false;
const originalConsole = window.console;
Object.keys(consoleDetector).forEach(method => {
const originalMethod = originalConsole[method];
originalConsole[method] = function (...args) {
if (!consoleUsageDetected) {
consoleUsageDetected = true;
// Small delay to avoid false positives
setTimeout(() => {
consoleUsageDetected = false;
}, 100);
}
return originalMethod.apply(this, args);
};
});
}
startDebuggerDetection() {
// Detect debugger statements and breakpoints
setInterval(() => {
const start = performance.now();
// This will pause if debugger is open
// eslint-disable-next-line no-debugger
debugger;
const end = performance.now();
// If execution took too long, debugger was likely open
if (end - start > 100) {
this.onDevToolsDetected();
}
}, 1000);
}
startPerformanceDetection() {
// Detect performance analysis tools
setInterval(() => {
const start = performance.now();
// Create a function that would be analyzed by performance tools
for (let i = 0; i < 100; i++) {
Math.random();
}
const end = performance.now();
// If this simple operation takes too long, tools might be running
if (end - start > 50) {
this.onDevToolsDetected();
}
}, 2000);
}
onDevToolsDetected() {
if (this.config.onDetected) {
this.config.onDetected();
}
if (this.config.redirectUrl) {
window.location.href = this.config.redirectUrl;
}
else {
// Default action: redirect to blank page
window.location.href = 'about:blank';
}
}
}
//# sourceMappingURL=DevToolsDetector.js.map