UNPKG

anti-screenshot-lib

Version:

A JavaScript library to prevent screenshots, screen recording, and unauthorized actions.

77 lines (67 loc) 2.23 kB
// AntiScreenshot Library class AntiScreenshot { constructor(options = {}) { this.options = Object.assign({ blockShortcuts: true, detectRecording: true, blockDevTools: true, disableRightClick: true, detectDOMManipulation: true, targetElements: [], systemConfigs: { windows: {}, mac: {}, linux: {} } }, options); this.init(); } init() { this.detectOS(); if (this.options.blockShortcuts) this.blockShortcuts(); if (this.options.detectRecording) this.detectScreenRecording(); if (this.options.blockDevTools) this.detectDevTools(); if (this.options.disableRightClick) this.disableRightClick(); if (this.options.detectDOMManipulation) this.detectDOMManipulation(); } detectOS() { const ua = navigator.userAgent.toLowerCase(); this.currentOS = ua.includes("win") ? "windows" : ua.includes("mac") ? "mac" : "linux"; } blockShortcuts() { document.addEventListener("keydown", (event) => { const config = this.options.systemConfigs[this.currentOS] || {}; if (config.blockAllKeys || (config.blockNavigationKeys && ["ArrowUp", "ArrowDown"].includes(event.key))) { event.preventDefault(); } if (event.key === "PrintScreen") { event.preventDefault(); alert("Screenshots are disabled!"); } }); } detectScreenRecording() { const origGetDisplayMedia = navigator.mediaDevices.getDisplayMedia; navigator.mediaDevices.getDisplayMedia = async function (...args) { alert("Screen recording detected!"); location.reload(); return origGetDisplayMedia.apply(this, args); }; } detectDevTools() { setInterval(() => { if (window.outerWidth - window.innerWidth > 200) { alert("DevTools detected!"); } }, 1000); } disableRightClick() { document.addEventListener("contextmenu", (event) => { event.preventDefault(); alert("Right-click is disabled!"); }); } detectDOMManipulation() { const observer = new MutationObserver(() => { alert("DOM Manipulation detected!"); }); observer.observe(document.body, { childList: true, subtree: true }); } } export default AntiScreenshot;