1- function noScreenshot ( options , overlayId ) {
1+ const isNode = typeof document === 'undefined' && typeof window === 'undefined' ;
2+
3+ function noScreenshot ( options , overlayId ) {
4+ if ( isNode ) {
5+ console . warn ( 'noScreenshot: DOM functions and screenshot prevention not supported in Node.js' ) ;
6+ return ;
7+ }
28 options = options || { } ;
39
410 const {
@@ -104,15 +110,15 @@ function noScreenshot(options , overlayId) {
104110 event . preventDefault ( ) ;
105111 }
106112 } ) ;
107- document . addEventListener ( 'keyup' , ( event ) => {
108- if ( event . key === 'PrintScreen' ) {
109- navigator . clipboard . writeText ( '' )
110- overlayScreen ( )
111- }
112- } )
113+ document . addEventListener ( 'keyup' , ( event ) => {
114+ if ( event . key === 'PrintScreen' ) {
115+ navigator . clipboard . writeText ( '' )
116+ overlayScreen ( )
117+ }
118+ } )
113119
114120 }
115- if ( disableFunctionKeys ) {
121+ if ( disableFunctionKeys ) {
116122 document . addEventListener ( 'keydown' , event => {
117123 if ( event . key === 'F1' || event . key === 'F2' || event . key === 'F3' || event . key === 'F5' || event . key === 'F6' || event . key === 'F7' || event . key === 'F8' || event . key === 'F9' || event . key === 'F10' || event . key === 'F11' || event . key === 'F12' ) {
118124 event . preventDefault ( ) ;
@@ -134,11 +140,11 @@ function noScreenshot(options , overlayId) {
134140 } ) ;
135141 }
136142
137- if ( mouseEnterAutoHide ) {
138- document . addEventListener ( 'mouseenter' , ( ) => {
139- HideOverlayScreen ( overlayId ) ;
140- } ) ;
141- }
143+ if ( mouseEnterAutoHide ) {
144+ document . addEventListener ( 'mouseenter' , ( ) => {
145+ HideOverlayScreen ( overlayId ) ;
146+ } ) ;
147+ }
142148
143149 if ( ctrlOverlay ) {
144150 document . addEventListener ( 'keydown' , event => {
@@ -153,16 +159,32 @@ if (mouseEnterAutoHide) {
153159 if ( event . altKey || event . optionsKey ) {
154160 overlayScreen ( overlayId ) ;
155161 }
156- } ) ;
157- }
162+ } ) ;
163+ }
164+
165+ if ( shiftOverlay ) {
166+ document . addEventListener ( 'keydown' , event => {
167+ if ( event . shiftKey ) {
168+ overlayScreen ( overlayId ) ;
169+ }
170+ } ) ;
171+ }
172+
173+ // Disable pointer events on body while the overlay is active
174+
175+ document . body . style . pointerEvents = 'none' ;
176+
177+ document . addEventListener ( 'keydown' , escListener ) ;
158178
159- if ( shiftOverlay ) {
160- document . addEventListener ( 'keydown' , event => {
161- if ( event . shiftKey ) {
162- overlayScreen ( overlayId ) ;
179+ function escListener ( event ) {
180+ if ( event . key === 'Escape' ) {
181+ HideOverlayScreen ( overlayId ) ;
182+ // document.body.removeChild(overlay);
183+ // document.body.style.pointerEvents = 'auto'; // Re-enable pointer events on body
184+ // document.removeEventListener('keydown', escListener);
163185 }
164- } ) ;
165- }
186+ }
187+
166188}
167189
168190function overlayScreen ( overlayId ) {
@@ -196,58 +218,51 @@ function overlayScreen(overlayId) {
196218 }
197219 }
198220
199- const overlay = document . createElement ( 'div' ) ;
200- overlay . id = 'no-screenshot-overlay' ;
201- overlay . style . position = 'fixed' ;
202- overlay . style . top = '0' ;
203- overlay . style . left = '0' ;
204- overlay . style . width = '100%' ;
205- overlay . style . height = '100%' ;
206- overlay . style . background = 'rgba(255, 255, 255, 1)' ; // semi-transparent white background
207- overlay . style . zIndex = '9999' ;
208- overlay . style . display = 'flex' ;
209- overlay . style . alignItems = 'center' ;
210- overlay . style . justifyContent = 'center' ;
211-
212- const message = document . createElement ( 'div' ) ;
213- message . textContent = 'Press Esc to close. Screenshots are disabled.' ;
214- message . style . fontSize = '24px' ;
215- message . style . color = 'black' ; // You can adjust the color as needed
216- message . style . padding = '20px' ; // Add padding to the message
217- message . style . background = 'rgba(255, 255, 255, 0.9)' ; // semi-transparent white background for message
218- message . style . borderRadius = '10px' ; // Rounded corners for the message box
219- message . style . boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.5)' ; // Drop shadow for the message box
220-
221- overlay . appendChild ( message ) ;
222- document . body . appendChild ( overlay ) ;
223- }
221+ const overlay = document . createElement ( 'div' ) ;
222+ overlay . id = 'no-screenshot-overlay' ;
223+ overlay . style . position = 'fixed' ;
224+ overlay . style . top = '0' ;
225+ overlay . style . left = '0' ;
226+ overlay . style . width = '100%' ;
227+ overlay . style . height = '100%' ;
228+ overlay . style . background = 'rgba(255, 255, 255, 1)' ; // semi-transparent white background
229+ overlay . style . zIndex = '9999' ;
230+ overlay . style . display = 'flex' ;
231+ overlay . style . alignItems = 'center' ;
232+ overlay . style . justifyContent = 'center' ;
224233
225- // Disable pointer events on body while the overlay is active
226- document . body . style . pointerEvents = 'none' ;
234+ const message = document . createElement ( 'div' ) ;
235+ message . textContent = 'Press Esc to close. Screenshots are disabled.' ;
236+ message . style . fontSize = '24px' ;
237+ message . style . color = 'black' ; // You can adjust the color as needed
238+ message . style . padding = '20px' ; // Add padding to the message
239+ message . style . background = 'rgba(255, 255, 255, 0.9)' ; // semi-transparent white background for message
240+ message . style . borderRadius = '10px' ; // Rounded corners for the message box
241+ message . style . boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.5)' ; // Drop shadow for the message box
227242
228- document . addEventListener ( 'keydown' , escListener ) ;
243+ overlay . appendChild ( message ) ;
244+ document . body . appendChild ( overlay ) ;
245+ }
229246
230- function escListener ( event ) {
231- if ( event . key === 'Escape' ) {
232- document . body . removeChild ( overlay ) ;
247+
248+ function HideOverlayScreen ( overlayId ) {
249+ if ( overlayId ) {
250+ const customOverlay = document . getElementById ( overlayId ) ;
251+ if ( customOverlay ) {
252+ customOverlay . style . display = 'none' ; // Hide the custom overlay
233253 document . body . style . pointerEvents = 'auto' ; // Re-enable pointer events on body
234- document . removeEventListener ( 'keydown' , escListener ) ;
254+ return ;
235255 }
256+ }
257+ var overlay = document . getElementById ( 'no-screenshot-overlay' ) ;
258+ document . body . removeChild ( overlay ) ;
259+ document . body . style . pointerEvents = 'auto' ; // Re-enable pointer events on body
260+ //document.removeEventListener('keydown', escListener);
236261}
237262
238- function HideOverlayScreen ( overlayId ) {
239- if ( overlayId ) {
240- const customOverlay = document . getElementById ( overlayId ) ;
241- if ( customOverlay ) {
242- customOverlay . style . display = 'none' ; // Hide the custom overlay
243- document . body . style . pointerEvents = 'auto' ; // Re-enable pointer events on body
244- return ;
245- }
246- }
247- var overlay = document . getElementById ( 'no-screenshot-overlay' ) ;
248- document . body . removeChild ( overlay ) ;
249- document . body . style . pointerEvents = 'auto' ; // Re-enable pointer events on body
250- document . removeEventListener ( 'keydown' , escListener ) ;
263+ if ( isNode ) {
264+ module . exports = noScreenshot ;
265+ } else {
266+ window . noScreenshot = noScreenshot ;
251267}
252268
253- module . exports = noScreenshot ;
0 commit comments