content-security-toolkit 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -41
- package/package.json +12 -5
- package/dist/core/mediator/handlers/baseEventHandler.d.ts +0 -65
- package/dist/core/mediator/handlers/baseEventHandler.js +0 -99
- package/dist/core/mediator/handlers/index.d.ts +0 -9
- package/dist/core/mediator/handlers/index.js +0 -34
- package/dist/strategies/AbstractStrategy.mediator.d.ts +0 -162
- package/dist/strategies/AbstractStrategy.mediator.js +0 -349
- package/dist/strategies/DevToolsStrategy copy.d.ts +0 -85
- package/dist/strategies/DevToolsStrategy copy.js +0 -362
- package/dist/strategies/DevToolsStrategy-detectorManager.d.ts +0 -70
- package/dist/strategies/DevToolsStrategy-detectorManager.js +0 -309
- package/dist/strategies/DevToolsStrategy-simple.d.ts +0 -75
- package/dist/strategies/DevToolsStrategy-simple.js +0 -366
- package/dist/strategies/StrategyRegistry.d.ts +0 -133
- package/dist/strategies/StrategyRegistry.js +0 -379
- package/dist/utils/base/LoggableComponent.d.ts +0 -62
- package/dist/utils/base/LoggableComponent.js +0 -95
- package/dist/utils/debuggerDetector/debuggerDetectionWorker.d.ts +0 -6
- package/dist/utils/debuggerDetector/debuggerDetectionWorker.js +0 -24
- package/dist/utils/debuggerDetector/debuggerDetector.d.ts +0 -55
- package/dist/utils/debuggerDetector/debuggerDetector.js +0 -158
- package/dist/utils/debuggerDetector/firefoxDetector.d.ts +0 -8
- package/dist/utils/debuggerDetector/firefoxDetector.js +0 -64
- package/dist/utils/detection.d.ts +0 -29
- package/dist/utils/detection.js +0 -267
- package/dist/utils/detectors/debuggerDetectionWorker.d.ts +0 -6
- package/dist/utils/detectors/debuggerDetectionWorker.js +0 -24
- package/dist/utils/detectors/firefoxDetector.d.ts +0 -8
- package/dist/utils/detectors/firefoxDetector.js +0 -64
- package/dist/utils/logging/LogLevel.d.ts +0 -21
- package/dist/utils/logging/LogLevel.js +0 -46
- package/dist/utils/logging/LoggingConfig.d.ts +0 -68
- package/dist/utils/logging/LoggingConfig.js +0 -64
- package/dist/utils/logging/LoggingFactory.d.ts +0 -22
- package/dist/utils/logging/LoggingFactory.js +0 -61
- package/dist/utils/logging/LoggingService.d.ts +0 -235
- package/dist/utils/logging/LoggingService.js +0 -385
- package/dist/utils/logging/SimpleLoggingService.d.ts +0 -39
- package/dist/utils/logging/SimpleLoggingService.js +0 -58
- package/dist/utils/logging/advanced/LogLevel.d.ts +0 -21
- package/dist/utils/logging/advanced/LogLevel.js +0 -46
- package/dist/utils/logging/advanced/LoggingConfig.d.ts +0 -68
- package/dist/utils/logging/advanced/LoggingConfig.js +0 -64
- package/dist/utils/logging/advanced/LoggingFactory.d.ts +0 -22
- package/dist/utils/logging/advanced/LoggingFactory.js +0 -61
- package/dist/utils/logging/advanced/LoggingService.d.ts +0 -235
- package/dist/utils/logging/advanced/LoggingService.js +0 -385
- package/dist/utils/protectedContentManager-simple.d.ts +0 -86
- package/dist/utils/protectedContentManager-simple.js +0 -180
- package/dist/utils/securityOverlayManager-observer-pause.d.ts +0 -283
- package/dist/utils/securityOverlayManager-observer-pause.js +0 -878
- package/dist/utils/securityOverlayManager-simple.d.ts +0 -197
- package/dist/utils/securityOverlayManager-simple.js +0 -552
package/README.md
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
# Content Security Toolkit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/content-security-toolkit) [](https://github.com/Isonimus/content-security-toolkit/actions) [](LICENSE)
|
|
4
|
+
|
|
5
|
+
A comprehensive toolkit for implementing content security measures in web applications — lightweight, modular, and TypeScript-friendly.
|
|
4
6
|
|
|
5
7
|
## Features
|
|
6
8
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
9
|
+
- Developer tools detection and response (DevTools detection)
|
|
10
|
+
- Screenshot detection and response (blur, warnings, callbacks)
|
|
11
|
+
- Watermarking with automatic regeneration (MutationObserver-protected)
|
|
12
|
+
- Keyboard shortcut prevention (copy/print/inspect shortcuts)
|
|
13
|
+
- Context menu blocking
|
|
14
|
+
- Print prevention and watermark-on-print support
|
|
15
|
+
- Selection / copy prevention
|
|
16
|
+
- Extension detection (detect suspicious browser extensions)
|
|
17
|
+
- Frame embedding protection (prevent unauthorized iframe embedding)
|
|
18
|
+
- Modular, strategy-based architecture — enable/disable strategies independently
|
|
19
|
+
- TypeScript typings, tests, and CI-ready workflow
|
|
20
|
+
- Lightweight and easy to integrate into web apps
|
|
13
21
|
|
|
14
22
|
## Installation
|
|
15
23
|
|
|
@@ -17,7 +25,102 @@ A comprehensive toolkit for implementing content security measures in web applic
|
|
|
17
25
|
npm install content-security-toolkit
|
|
18
26
|
# or
|
|
19
27
|
yarn add content-security-toolkit
|
|
20
|
-
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Simple usage (quick start)
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import { ContentProtector } from 'content-security-toolkit'
|
|
36
|
+
|
|
37
|
+
// Protect the whole document with sensible defaults
|
|
38
|
+
const protector = new ContentProtector()
|
|
39
|
+
protector.protect()
|
|
40
|
+
|
|
41
|
+
// Later...
|
|
42
|
+
protector.unprotect()
|
|
43
|
+
protector.dispose()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Custom usage (advanced configuration)
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
import { ContentProtector } from 'content-security-toolkit'
|
|
50
|
+
|
|
51
|
+
const protector = new ContentProtector({
|
|
52
|
+
targetElement: '#viewer', // CSS selector or HTMLElement
|
|
53
|
+
enableWatermark: true,
|
|
54
|
+
watermarkOptions: {
|
|
55
|
+
text: 'CONFIDENTIAL',
|
|
56
|
+
opacity: 0.12,
|
|
57
|
+
density: 3,
|
|
58
|
+
},
|
|
59
|
+
preventDevTools: true,
|
|
60
|
+
preventScreenshots: true,
|
|
61
|
+
preventEmbedding: true,
|
|
62
|
+
debugMode: false,
|
|
63
|
+
// Optional callbacks
|
|
64
|
+
customHandlers: {
|
|
65
|
+
onDevToolsDetected: () => console.warn('DevTools opened'),
|
|
66
|
+
onScreenshotDetected: () => alert('Screenshot detected'),
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
protector.protect()
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Framework quick examples
|
|
74
|
+
|
|
75
|
+
React (hooks):
|
|
76
|
+
|
|
77
|
+
```jsx
|
|
78
|
+
import React, { useEffect } from 'react'
|
|
79
|
+
import { ContentProtector } from 'content-security-toolkit'
|
|
80
|
+
|
|
81
|
+
function Viewer() {
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const protector = new ContentProtector({ enableWatermark: true })
|
|
84
|
+
protector.protect()
|
|
85
|
+
return () => protector.dispose()
|
|
86
|
+
}, [])
|
|
87
|
+
|
|
88
|
+
return <div id="protected-content">Protected content</div>
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Vue 3 (Composition API):
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
import { onMounted, onUnmounted } from 'vue'
|
|
96
|
+
import { ContentProtector } from 'content-security-toolkit'
|
|
97
|
+
|
|
98
|
+
export default {
|
|
99
|
+
setup() {
|
|
100
|
+
let protector
|
|
101
|
+
onMounted(() => {
|
|
102
|
+
protector = new ContentProtector({ targetElement: '#protected-content' })
|
|
103
|
+
protector.protect()
|
|
104
|
+
})
|
|
105
|
+
onUnmounted(() => protector?.dispose())
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### API highlights
|
|
111
|
+
|
|
112
|
+
- `ContentProtector.protect()` — enable protection
|
|
113
|
+
- `ContentProtector.unprotect()` — disable protection
|
|
114
|
+
- `ContentProtector.dispose()` — cleanup resources
|
|
115
|
+
- `ContentProtector.getStrategy(name)` — access a strategy instance for advanced control
|
|
116
|
+
- Options include: `targetElement`, `enableWatermark`, `watermarkOptions`, `preventDevTools`, `preventScreenshots`, `preventEmbedding`, `debugMode`, `customHandlers`
|
|
117
|
+
|
|
118
|
+
_For full examples, see `examples/` and `examples/advanced/advanced_example.ts`._
|
|
119
|
+
### Where to start
|
|
120
|
+
|
|
121
|
+
- Use the simple example to get protection running quickly.
|
|
122
|
+
- Add `customHandlers` and `watermarkOptions` to tailor behaviour.
|
|
123
|
+
- See `examples/` for runnable demos and the `src/` folder for strategy implementations.
|
|
21
124
|
|
|
22
125
|
Content Security Toolkit is a comprehensive JavaScript/TypeScript library designed to protect sensitive web content from unauthorized copying, extraction, and distribution. It implements multiple layers of protection strategies that work together to safeguard digital content while maintaining a good user experience for legitimate users.
|
|
23
126
|
|
|
@@ -42,46 +145,56 @@ This architecture allows for flexible configuration and easy extension with new
|
|
|
42
145
|
|
|
43
146
|
**Implementation Details**:
|
|
44
147
|
|
|
45
|
-
- Intercepts key combinations like Ctrl+C, Ctrl+P, Ctrl+S, Ctrl+Shift+I
|
|
148
|
+
- Intercepts key combinations like Ctrl/Cmd+C, Ctrl/Cmd+P, Ctrl/Cmd+S, Ctrl+Shift+I
|
|
46
149
|
- Configurable to block specific shortcuts
|
|
47
150
|
- Provides custom event handlers for blocked shortcuts
|
|
48
151
|
|
|
49
152
|
|
|
50
|
-
### 2.
|
|
153
|
+
### 2. Clipboard Protection (ClipboardStrategy)
|
|
154
|
+
|
|
155
|
+
**Purpose**: Intercepts clipboard operations to prevent programmatic copying or tampering via clipboard events.
|
|
156
|
+
|
|
157
|
+
**Implementation Details**:
|
|
158
|
+
|
|
159
|
+
- Listens to `copy`, `cut` and `paste` events
|
|
160
|
+
- Can clear or override clipboard data or present a custom handler
|
|
161
|
+
- Useful in combination with selection and context menu protections
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
### 3. Context Menu Protection (ContextMenuStrategy)
|
|
51
165
|
|
|
52
|
-
**Purpose**: Prevents users from accessing the browser's context menu (right-click menu) to copy content or
|
|
166
|
+
**Purpose**: Prevents users from accessing the browser's context menu (right-click menu) to copy content, save assets, or inspect elements.
|
|
53
167
|
|
|
54
168
|
**Implementation Details**:
|
|
55
169
|
|
|
56
|
-
- Intercepts the contextmenu event on specified elements
|
|
170
|
+
- Intercepts the `contextmenu` event on specified elements
|
|
57
171
|
- Can be applied to the entire document or specific elements
|
|
58
|
-
- Supports custom handler for
|
|
172
|
+
- Supports custom handler for right-click attempts
|
|
59
173
|
|
|
60
174
|
|
|
61
|
-
###
|
|
175
|
+
### 4. Print Protection (PrintStrategy)
|
|
62
176
|
|
|
63
|
-
**Purpose**: Prevents
|
|
177
|
+
**Purpose**: Prevents or modifies the browser's print behavior to protect content (optionally watermark on print).
|
|
64
178
|
|
|
65
179
|
**Implementation Details**:
|
|
66
180
|
|
|
67
|
-
- Intercepts print
|
|
68
|
-
- Optionally
|
|
69
|
-
-
|
|
181
|
+
- Intercepts `window.print` and print keyboard shortcuts
|
|
182
|
+
- Optionally adds a print-only watermark or blocks printing
|
|
183
|
+
- Provides hooks for custom messaging when print is attempted
|
|
70
184
|
|
|
71
185
|
|
|
72
|
-
###
|
|
186
|
+
### 5. Selection Protection (SelectionStrategy)
|
|
73
187
|
|
|
74
188
|
**Purpose**: Prevents users from selecting and copying text content.
|
|
75
189
|
|
|
76
190
|
**Implementation Details**:
|
|
77
191
|
|
|
78
192
|
- Disables text selection via CSS and JavaScript
|
|
79
|
-
- Intercepts selection events
|
|
80
|
-
- Can be applied to
|
|
81
|
-
- Supports custom handler for selection attempts
|
|
193
|
+
- Intercepts selection events and clears selection ranges
|
|
194
|
+
- Can be applied selectively to elements or site-wide
|
|
82
195
|
|
|
83
196
|
|
|
84
|
-
###
|
|
197
|
+
### 6. Watermark Protection (WatermarkStrategy)
|
|
85
198
|
|
|
86
199
|
**Purpose**: Adds visible watermarks to the content to discourage unauthorized sharing and identify the source.
|
|
87
200
|
|
|
@@ -91,30 +204,51 @@ This architecture allows for flexible configuration and easy extension with new
|
|
|
91
204
|
- Supports customization of text, opacity, density, and positioning
|
|
92
205
|
- Includes user identification (userId) to trace leaked content
|
|
93
206
|
- **Observer Mechanism**: Implements a MutationObserver to detect when watermarks are removed from the DOM and automatically regenerates them
|
|
94
|
-
- Watermarks are positioned in a way that makes them difficult to remove without affecting the content
|
|
95
207
|
|
|
96
208
|
|
|
97
|
-
###
|
|
209
|
+
### 7. DevTools Protection (DevToolsStrategy)
|
|
98
210
|
|
|
99
211
|
**Purpose**: Detects and responds to attempts to open browser developer tools, which could be used to inspect and modify the page.
|
|
100
212
|
|
|
101
213
|
**Implementation Details**:
|
|
102
214
|
|
|
103
|
-
- Uses multiple detection techniques (
|
|
215
|
+
- Uses multiple detection techniques (console timing, resize detection, feature checks)
|
|
104
216
|
- Provides callbacks when DevTools are opened or closed
|
|
105
|
-
- Can be configured to take specific actions when DevTools are detected
|
|
217
|
+
- Can be configured to take specific actions (log, blur, overlay) when DevTools are detected
|
|
106
218
|
|
|
107
219
|
|
|
108
|
-
###
|
|
220
|
+
### 8. Screenshot Protection (ScreenshotStrategy)
|
|
109
221
|
|
|
110
222
|
**Purpose**: Detects and responds to screenshot attempts.
|
|
111
223
|
|
|
112
224
|
**Implementation Details**:
|
|
113
225
|
|
|
114
|
-
- Monitors clipboard events and screen capture
|
|
115
|
-
- Blurs content or displays warning message during screenshot attempts
|
|
226
|
+
- Monitors clipboard events and screen capture API usage
|
|
227
|
+
- Blurs content or displays a warning message during screenshot attempts
|
|
116
228
|
- Provides callbacks when screenshot attempts are detected
|
|
117
|
-
- Uses visual
|
|
229
|
+
- Uses visual techniques to make screenshots less useful (temporary content blurring)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
### 9. Extension Detection (BrowserExtensionDetectionStrategy)
|
|
233
|
+
|
|
234
|
+
**Purpose**: Detects suspicious or known browser extensions that may bypass protections.
|
|
235
|
+
|
|
236
|
+
**Implementation Details**:
|
|
237
|
+
|
|
238
|
+
- Heuristics and detection strategies to identify extensions
|
|
239
|
+
- Callbacks for detected extensions
|
|
240
|
+
- Optionally disable protections or report detections to a backend
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
### 10. Frame Embedding Protection (FrameEmbeddingProtectionStrategy)
|
|
244
|
+
|
|
245
|
+
**Purpose**: Detects and prevents the page from being embedded into unauthorized frames.
|
|
246
|
+
|
|
247
|
+
**Implementation Details**:
|
|
248
|
+
|
|
249
|
+
- Checks `top`/`window` relationships and hostnames
|
|
250
|
+
- Optionally break out of frames or display a blocking overlay
|
|
251
|
+
- Configurable whitelist of allowed origins
|
|
118
252
|
|
|
119
253
|
|
|
120
254
|
## Technical Deep Dive
|
|
@@ -145,16 +279,27 @@ The screenshot protection uses several techniques to detect and respond to scree
|
|
|
145
279
|
|
|
146
280
|
## Known Limitations and Issues
|
|
147
281
|
|
|
148
|
-
While Content Security Toolkit provides robust protection, it's important to understand its limitations
|
|
282
|
+
While Content Security Toolkit provides robust protection, it's important to understand its limitations and where protections are best applied.
|
|
283
|
+
|
|
284
|
+
### Browser compatibility & limitations
|
|
285
|
+
|
|
286
|
+
| Feature | Desktop | Mobile | Notes |
|
|
287
|
+
|---|---:|---:|---|
|
|
288
|
+
| DevTools detection | Good on modern Chromium & Firefox | Limited | Mobile devices have limited toolsets; detection techniques may be unreliable.
|
|
289
|
+
| Screenshot detection | Partial | Partial | Can detect some screen-capture APIs and clipboard activity; cannot prevent OS-level screenshots or photos.
|
|
290
|
+
| Watermarking | Good | Good | MutationObserver + DOM-based watermarks work on modern browsers; heavy density may impact layout and perf.
|
|
291
|
+
| Extension detection | Limited | N/A | Heuristic-based; may produce false positives and won't detect all malicious extensions.
|
|
292
|
+
| Frame embedding protection | Good | Good | Works when combined with proper server headers (CSP/X-Frame-Options) for stronger enforcement.
|
|
293
|
+
|
|
294
|
+
> Notes: Protections operate in the browser and are best used as a **deterrent** and part of defense-in-depth. They are not a substitute for server-side access control or legal protections.
|
|
295
|
+
|
|
296
|
+
### Other important limitations
|
|
149
297
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
6. **Performance Considerations**: Extensive protection, particularly watermarking with high density, can impact page performance.
|
|
156
|
-
7. **Hardware Screenshots**: The library cannot prevent screenshots taken using physical cameras or external hardware.
|
|
157
|
-
8. **Browser Extensions**: Some browser extensions might interfere with or bypass certain protection mechanisms.
|
|
298
|
+
- **Client-Side Only**: All protections run in the browser and can potentially be bypassed by determined attackers.
|
|
299
|
+
- **Accessibility Impact**: Some protections can affect accessibility tools; test and provide alternative access for users who need it.
|
|
300
|
+
- **Performance Considerations**: High-density watermarks or many simultaneous observers may impact rendering performance on low-end devices.
|
|
301
|
+
- **Hardware Screenshots**: Cannot prevent photos taken with external cameras or devices.
|
|
302
|
+
- **Browser Extensions**: Extensions may interfere with or bypass protections; treat extension detection as heuristic rather than absolute.
|
|
158
303
|
|
|
159
304
|
Unlike DRM systems that encrypt content and control playback through specialized software, Content Security Toolkit focuses on preventing common extraction methods while maintaining compatibility with standard web browsers. It's designed as a deterrent rather than an unbreakable protection system.
|
|
160
305
|
|
|
@@ -168,4 +313,21 @@ For optimal protection, consider these implementation guidelines:
|
|
|
168
313
|
4. **Selective Protection**: Apply protection only to sensitive content to minimize performance impact
|
|
169
314
|
5. **Regular Updates**: Keep the library updated to benefit from security improvements
|
|
170
315
|
6. **Complementary Measures**: Combine with server-side protections and legal terms of use
|
|
171
|
-
7. **Accessibility Considerations**: Test with accessibility tools and provide alternative access methods for legitimate users with special needs
|
|
316
|
+
7. **Accessibility Considerations**: Test with accessibility tools and provide alternative access methods for legitimate users with special needs
|
|
317
|
+
|
|
318
|
+
## Contributing
|
|
319
|
+
|
|
320
|
+
Contributions are welcome! To contribute:
|
|
321
|
+
|
|
322
|
+
- Fork the repository and create a feature branch from `main`.
|
|
323
|
+
- Run `npm install`, `npm run lint`, `npm test`, and `npm run build` locally before pushing.
|
|
324
|
+
- Open a clear pull request explaining the change and link any relevant issues.
|
|
325
|
+
- Add tests for new behavior and update the README/examples when adding features.
|
|
326
|
+
|
|
327
|
+
Please be respectful and follow the project's Code of Conduct (see below).
|
|
328
|
+
|
|
329
|
+
## Code of Conduct
|
|
330
|
+
|
|
331
|
+
This project follows the Contributor Covenant. By participating you agree to the terms in `CODE_OF_CONDUCT.md`.
|
|
332
|
+
|
|
333
|
+
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "content-security-toolkit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,12 +19,19 @@
|
|
|
19
19
|
"examples:start": "cd examples && npm run start"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
|
-
"security",
|
|
22
|
+
"content-security",
|
|
23
23
|
"content-protection",
|
|
24
|
-
"
|
|
25
|
-
"
|
|
24
|
+
"web-security",
|
|
25
|
+
"anti-screenshot",
|
|
26
|
+
"devtools-detection",
|
|
27
|
+
"watermark",
|
|
28
|
+
"copy-protection",
|
|
29
|
+
"browser-security",
|
|
30
|
+
"typescript",
|
|
31
|
+
"javascript",
|
|
32
|
+
"frontend-security"
|
|
26
33
|
],
|
|
27
|
-
"author": "
|
|
34
|
+
"author": "Isonimus",
|
|
28
35
|
"license": "MIT",
|
|
29
36
|
"repository": {
|
|
30
37
|
"type": "git",
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { ProtectionMediator } from "../../mediator/types";
|
|
2
|
-
import type { ProtectionEvent, ProtectionEventType } from "../../mediator/protection-event";
|
|
3
|
-
/**
|
|
4
|
-
* Abstract base class for all event handlers
|
|
5
|
-
*/
|
|
6
|
-
export declare abstract class BaseEventHandler {
|
|
7
|
-
protected mediator: ProtectionMediator;
|
|
8
|
-
protected readonly COMPONENT_NAME: string;
|
|
9
|
-
protected debugMode: boolean;
|
|
10
|
-
protected subscriptionIds: string[];
|
|
11
|
-
/**
|
|
12
|
-
* Create a new event handler
|
|
13
|
-
* @param mediator The protection mediator
|
|
14
|
-
* @param componentName The name of the component
|
|
15
|
-
* @param debugMode Enable debug mode for troubleshooting
|
|
16
|
-
*/
|
|
17
|
-
constructor(mediator: ProtectionMediator, componentName: string, debugMode?: boolean);
|
|
18
|
-
/**
|
|
19
|
-
* Initialize the handler and subscribe to events
|
|
20
|
-
* This method should be implemented by subclasses
|
|
21
|
-
*/
|
|
22
|
-
protected abstract initialize(): void;
|
|
23
|
-
/**
|
|
24
|
-
* Log a debug message
|
|
25
|
-
* @param message Message to log
|
|
26
|
-
* @param args Additional arguments to log
|
|
27
|
-
*/
|
|
28
|
-
protected log(message: string, ...args: unknown[]): void;
|
|
29
|
-
/**
|
|
30
|
-
* Log a warning message
|
|
31
|
-
* @param message Warning message
|
|
32
|
-
* @param args Additional arguments to log
|
|
33
|
-
*/
|
|
34
|
-
protected warn(message: string, ...args: unknown[]): void;
|
|
35
|
-
/**
|
|
36
|
-
* Log an error message
|
|
37
|
-
* @param message Error message
|
|
38
|
-
* @param args Additional arguments to log
|
|
39
|
-
*/
|
|
40
|
-
protected error(message: string, ...args: unknown[]): void;
|
|
41
|
-
/**
|
|
42
|
-
* Set debug mode
|
|
43
|
-
* @param enabled Whether debug mode should be enabled
|
|
44
|
-
*/
|
|
45
|
-
setDebugMode(enabled: boolean): void;
|
|
46
|
-
/**
|
|
47
|
-
* Subscribe to an event and track the subscription ID
|
|
48
|
-
* @param eventType The event type to subscribe to
|
|
49
|
-
* @param handler The event handler function
|
|
50
|
-
* @param options Optional subscription options
|
|
51
|
-
* @returns The subscription ID
|
|
52
|
-
*/
|
|
53
|
-
protected subscribe(eventType: ProtectionEventType, handler: (event: ProtectionEvent) => void, options?: {
|
|
54
|
-
context?: string;
|
|
55
|
-
}): string;
|
|
56
|
-
/**
|
|
57
|
-
* Unsubscribe from all events and clean up
|
|
58
|
-
*/
|
|
59
|
-
dispose(): void;
|
|
60
|
-
/**
|
|
61
|
-
* Additional cleanup to be performed on disposal
|
|
62
|
-
* This method can be overridden by subclasses
|
|
63
|
-
*/
|
|
64
|
-
protected onDispose(): void;
|
|
65
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Abstract base class for all event handlers
|
|
3
|
-
*/
|
|
4
|
-
export class BaseEventHandler {
|
|
5
|
-
/**
|
|
6
|
-
* Create a new event handler
|
|
7
|
-
* @param mediator The protection mediator
|
|
8
|
-
* @param componentName The name of the component
|
|
9
|
-
* @param debugMode Enable debug mode for troubleshooting
|
|
10
|
-
*/
|
|
11
|
-
constructor(mediator, componentName, debugMode = false) {
|
|
12
|
-
this.subscriptionIds = [];
|
|
13
|
-
this.mediator = mediator;
|
|
14
|
-
this.COMPONENT_NAME = componentName;
|
|
15
|
-
this.debugMode = debugMode;
|
|
16
|
-
this.initialize();
|
|
17
|
-
if (this.debugMode) {
|
|
18
|
-
this.log("Initialized and subscribed to events");
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Log a debug message
|
|
23
|
-
* @param message Message to log
|
|
24
|
-
* @param args Additional arguments to log
|
|
25
|
-
*/
|
|
26
|
-
log(message, ...args) {
|
|
27
|
-
if (this.debugMode) {
|
|
28
|
-
console.log(`${this.COMPONENT_NAME}: ${message}`, ...args);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Log a warning message
|
|
33
|
-
* @param message Warning message
|
|
34
|
-
* @param args Additional arguments to log
|
|
35
|
-
*/
|
|
36
|
-
warn(message, ...args) {
|
|
37
|
-
if (this.debugMode) {
|
|
38
|
-
console.warn(`${this.COMPONENT_NAME}: ${message}`, ...args);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
// In non-debug mode, only log the message without args for brevity
|
|
42
|
-
console.warn(`${this.COMPONENT_NAME}: ${message}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Log an error message
|
|
47
|
-
* @param message Error message
|
|
48
|
-
* @param args Additional arguments to log
|
|
49
|
-
*/
|
|
50
|
-
error(message, ...args) {
|
|
51
|
-
console.error(`${this.COMPONENT_NAME}: ${message}`, ...args);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Set debug mode
|
|
55
|
-
* @param enabled Whether debug mode should be enabled
|
|
56
|
-
*/
|
|
57
|
-
setDebugMode(enabled) {
|
|
58
|
-
this.debugMode = enabled;
|
|
59
|
-
if (this.debugMode) {
|
|
60
|
-
this.log(`Debug mode ${enabled ? "enabled" : "disabled"}`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Subscribe to an event and track the subscription ID
|
|
65
|
-
* @param eventType The event type to subscribe to
|
|
66
|
-
* @param handler The event handler function
|
|
67
|
-
* @param options Optional subscription options
|
|
68
|
-
* @returns The subscription ID
|
|
69
|
-
*/
|
|
70
|
-
subscribe(eventType, handler, options) {
|
|
71
|
-
const subId = this.mediator.subscribe(eventType, handler, { ...options, context: options?.context || this.COMPONENT_NAME });
|
|
72
|
-
this.subscriptionIds.push(subId);
|
|
73
|
-
if (this.debugMode) {
|
|
74
|
-
this.log(`Subscribed to ${eventType} with ID ${subId}`);
|
|
75
|
-
}
|
|
76
|
-
return subId;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Unsubscribe from all events and clean up
|
|
80
|
-
*/
|
|
81
|
-
dispose() {
|
|
82
|
-
if (this.debugMode) {
|
|
83
|
-
this.log(`Disposing handler, unsubscribing from ${this.subscriptionIds.length} events`);
|
|
84
|
-
}
|
|
85
|
-
// Unsubscribe from all events
|
|
86
|
-
for (const id of this.subscriptionIds) {
|
|
87
|
-
this.mediator.unsubscribe(id);
|
|
88
|
-
}
|
|
89
|
-
this.subscriptionIds = [];
|
|
90
|
-
this.onDispose();
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Additional cleanup to be performed on disposal
|
|
94
|
-
* This method can be overridden by subclasses
|
|
95
|
-
*/
|
|
96
|
-
onDispose() {
|
|
97
|
-
// Default implementation does nothing
|
|
98
|
-
}
|
|
99
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ProtectionMediator } from "../types";
|
|
2
|
-
export declare class HandlerRegistry {
|
|
3
|
-
private handlers;
|
|
4
|
-
private mediator;
|
|
5
|
-
private debugMode;
|
|
6
|
-
constructor(mediator: ProtectionMediator, debugMode?: boolean);
|
|
7
|
-
private initializeHandlers;
|
|
8
|
-
setDebugMode(enabled: boolean): void;
|
|
9
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { DevToolsEventHandler } from "./devToolsEventHandler";
|
|
2
|
-
import { BrowserExtensionEventHandler } from "./extensionEventHandlers";
|
|
3
|
-
import { FrameEmbeddingEventHandler } from "./iFrameEventHandlers";
|
|
4
|
-
import { ScreenshotEventHandler } from "./screenShotEventHandlers";
|
|
5
|
-
export class HandlerRegistry {
|
|
6
|
-
constructor(mediator, debugMode = false) {
|
|
7
|
-
this.handlers = {};
|
|
8
|
-
this.mediator = mediator;
|
|
9
|
-
this.debugMode = debugMode;
|
|
10
|
-
this.initializeHandlers();
|
|
11
|
-
}
|
|
12
|
-
initializeHandlers() {
|
|
13
|
-
// Initialize all handlers
|
|
14
|
-
this.handlers.devTools = new DevToolsEventHandler(this.mediator, this.debugMode);
|
|
15
|
-
this.handlers.extension = new BrowserExtensionEventHandler(this.mediator, this.debugMode);
|
|
16
|
-
this.handlers.screenshot = new ScreenshotEventHandler(this.mediator, this.debugMode);
|
|
17
|
-
this.handlers.iFrame = new FrameEmbeddingEventHandler(this.mediator, this.debugMode);
|
|
18
|
-
// Add more handlers as you implement them
|
|
19
|
-
// this.handlers.screenshot = new ScreenshotEventHandler(this.mediator, this.debugMode);
|
|
20
|
-
// this.handlers.frameEmbedding = new FrameEmbeddingEventHandler(this.mediator, this.debugMode);
|
|
21
|
-
if (this.debugMode) {
|
|
22
|
-
console.log("HandlerRegistry: Initialized all event handlers");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
setDebugMode(enabled) {
|
|
26
|
-
this.debugMode = enabled;
|
|
27
|
-
// Update debug mode for all handlers
|
|
28
|
-
Object.values(this.handlers).forEach(handler => {
|
|
29
|
-
if (typeof handler.setDebugMode === 'function') {
|
|
30
|
-
handler.setDebugMode(enabled);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|