content-security-toolkit 1.0.0

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.
Files changed (162) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +171 -0
  3. package/dist/config/default-extensions-config.json +103 -0
  4. package/dist/core/ContentProtector.d.ts +63 -0
  5. package/dist/core/ContentProtector.js +279 -0
  6. package/dist/core/index.d.ts +1 -0
  7. package/dist/core/index.js +2 -0
  8. package/dist/core/mediator/ContentProtectionMediator.d.ts +86 -0
  9. package/dist/core/mediator/ContentProtectionMediator.js +238 -0
  10. package/dist/core/mediator/eventDataTypes.d.ts +205 -0
  11. package/dist/core/mediator/eventDataTypes.js +23 -0
  12. package/dist/core/mediator/handlers/abstractEventHandler.d.ts +67 -0
  13. package/dist/core/mediator/handlers/abstractEventHandler.js +106 -0
  14. package/dist/core/mediator/handlers/baseEventHandler.d.ts +65 -0
  15. package/dist/core/mediator/handlers/baseEventHandler.js +99 -0
  16. package/dist/core/mediator/handlers/devToolsEventHandler.d.ts +9 -0
  17. package/dist/core/mediator/handlers/devToolsEventHandler.js +95 -0
  18. package/dist/core/mediator/handlers/eventHandlerRegistry.d.ts +9 -0
  19. package/dist/core/mediator/handlers/eventHandlerRegistry.js +34 -0
  20. package/dist/core/mediator/handlers/extensionEventHandlers.d.ts +40 -0
  21. package/dist/core/mediator/handlers/extensionEventHandlers.js +140 -0
  22. package/dist/core/mediator/handlers/iFrameEventHandlers.d.ts +27 -0
  23. package/dist/core/mediator/handlers/iFrameEventHandlers.js +93 -0
  24. package/dist/core/mediator/handlers/index.d.ts +9 -0
  25. package/dist/core/mediator/handlers/index.js +34 -0
  26. package/dist/core/mediator/handlers/screenShotEventHandlers.d.ts +34 -0
  27. package/dist/core/mediator/handlers/screenShotEventHandlers.js +111 -0
  28. package/dist/core/mediator/protection-event.d.ts +94 -0
  29. package/dist/core/mediator/protection-event.js +43 -0
  30. package/dist/core/mediator/types.d.ts +105 -0
  31. package/dist/core/mediator/types.js +1 -0
  32. package/dist/index.d.ts +4 -0
  33. package/dist/index.js +5 -0
  34. package/dist/strategies/AbstractStrategy.d.ts +152 -0
  35. package/dist/strategies/AbstractStrategy.js +296 -0
  36. package/dist/strategies/AbstractStrategy.mediator.d.ts +162 -0
  37. package/dist/strategies/AbstractStrategy.mediator.js +349 -0
  38. package/dist/strategies/ClipboardStrategy.d.ts +67 -0
  39. package/dist/strategies/ClipboardStrategy.js +291 -0
  40. package/dist/strategies/ContextMenuStrategy.d.ts +60 -0
  41. package/dist/strategies/ContextMenuStrategy.js +454 -0
  42. package/dist/strategies/DevToolsStrategy copy.d.ts +85 -0
  43. package/dist/strategies/DevToolsStrategy copy.js +362 -0
  44. package/dist/strategies/DevToolsStrategy-detectorManager.d.ts +70 -0
  45. package/dist/strategies/DevToolsStrategy-detectorManager.js +309 -0
  46. package/dist/strategies/DevToolsStrategy-simple.d.ts +75 -0
  47. package/dist/strategies/DevToolsStrategy-simple.js +366 -0
  48. package/dist/strategies/DevToolsStrategy.d.ts +55 -0
  49. package/dist/strategies/DevToolsStrategy.js +314 -0
  50. package/dist/strategies/ExtensionStrategy.d.ts +66 -0
  51. package/dist/strategies/ExtensionStrategy.js +486 -0
  52. package/dist/strategies/IFrameStrategy.d.ts +49 -0
  53. package/dist/strategies/IFrameStrategy.js +255 -0
  54. package/dist/strategies/KeyboardStrategy.d.ts +35 -0
  55. package/dist/strategies/KeyboardStrategy.js +130 -0
  56. package/dist/strategies/PrintStrategy.d.ts +47 -0
  57. package/dist/strategies/PrintStrategy.js +201 -0
  58. package/dist/strategies/ScreenshotStrategy.d.ts +90 -0
  59. package/dist/strategies/ScreenshotStrategy.js +488 -0
  60. package/dist/strategies/SelectionStrategy.d.ts +49 -0
  61. package/dist/strategies/SelectionStrategy.js +216 -0
  62. package/dist/strategies/StrategyRegistry.d.ts +133 -0
  63. package/dist/strategies/StrategyRegistry.js +379 -0
  64. package/dist/strategies/WatermarkStrategy.d.ts +47 -0
  65. package/dist/strategies/WatermarkStrategy.js +273 -0
  66. package/dist/strategies/index.d.ts +9 -0
  67. package/dist/strategies/index.js +10 -0
  68. package/dist/types/index.d.ts +271 -0
  69. package/dist/types/index.js +16 -0
  70. package/dist/utils/DOMObserver.d.ts +68 -0
  71. package/dist/utils/DOMObserver.js +134 -0
  72. package/dist/utils/base/LoggableComponent.d.ts +62 -0
  73. package/dist/utils/base/LoggableComponent.js +95 -0
  74. package/dist/utils/debuggerDetector/debuggerDetectionWorker.d.ts +6 -0
  75. package/dist/utils/debuggerDetector/debuggerDetectionWorker.js +24 -0
  76. package/dist/utils/debuggerDetector/debuggerDetector.d.ts +55 -0
  77. package/dist/utils/debuggerDetector/debuggerDetector.js +158 -0
  78. package/dist/utils/debuggerDetector/firefoxDetector.d.ts +8 -0
  79. package/dist/utils/debuggerDetector/firefoxDetector.js +64 -0
  80. package/dist/utils/detection.d.ts +29 -0
  81. package/dist/utils/detection.js +267 -0
  82. package/dist/utils/detectors/AbstractDevToolsDetector.d.ts +105 -0
  83. package/dist/utils/detectors/AbstractDevToolsDetector.js +136 -0
  84. package/dist/utils/detectors/dateToStringDetector.d.ts +43 -0
  85. package/dist/utils/detectors/dateToStringDetector.js +96 -0
  86. package/dist/utils/detectors/debugLibDetector.d.ts +64 -0
  87. package/dist/utils/detectors/debugLibDetector.js +195 -0
  88. package/dist/utils/detectors/debuggerDetectionWorker.d.ts +6 -0
  89. package/dist/utils/detectors/debuggerDetectionWorker.js +24 -0
  90. package/dist/utils/detectors/debuggerDetector.d.ts +51 -0
  91. package/dist/utils/detectors/debuggerDetector.js +211 -0
  92. package/dist/utils/detectors/defineGetterDetector.d.ts +48 -0
  93. package/dist/utils/detectors/defineGetterDetector.js +150 -0
  94. package/dist/utils/detectors/detectorInterface.d.ts +36 -0
  95. package/dist/utils/detectors/detectorInterface.js +1 -0
  96. package/dist/utils/detectors/devToolsDetectorManager.d.ts +88 -0
  97. package/dist/utils/detectors/devToolsDetectorManager.js +246 -0
  98. package/dist/utils/detectors/firefoxDetector.d.ts +8 -0
  99. package/dist/utils/detectors/firefoxDetector.js +64 -0
  100. package/dist/utils/detectors/funcToStringDetector.d.ts +43 -0
  101. package/dist/utils/detectors/funcToStringDetector.js +90 -0
  102. package/dist/utils/detectors/regToStringDetector.d.ts +43 -0
  103. package/dist/utils/detectors/regToStringDetector.js +129 -0
  104. package/dist/utils/detectors/sizeDetector.d.ts +54 -0
  105. package/dist/utils/detectors/sizeDetector.js +134 -0
  106. package/dist/utils/detectors/timingDetector.d.ts +55 -0
  107. package/dist/utils/detectors/timingDetector.js +143 -0
  108. package/dist/utils/dom.d.ts +20 -0
  109. package/dist/utils/dom.js +83 -0
  110. package/dist/utils/environment.d.ts +29 -0
  111. package/dist/utils/environment.js +267 -0
  112. package/dist/utils/eventManager.d.ts +167 -0
  113. package/dist/utils/eventManager.js +556 -0
  114. package/dist/utils/index.d.ts +2 -0
  115. package/dist/utils/index.js +3 -0
  116. package/dist/utils/intervalManager.d.ts +96 -0
  117. package/dist/utils/intervalManager.js +229 -0
  118. package/dist/utils/keyboardShortcutManager/keyboardShortcutManager.d.ts +41 -0
  119. package/dist/utils/keyboardShortcutManager/keyboardShortcutManager.js +135 -0
  120. package/dist/utils/keyboardShortcutManager/keyboardShortcuts.d.ts +18 -0
  121. package/dist/utils/keyboardShortcutManager/keyboardShortcuts.js +195 -0
  122. package/dist/utils/logging/LogLevel.d.ts +21 -0
  123. package/dist/utils/logging/LogLevel.js +46 -0
  124. package/dist/utils/logging/LoggingConfig.d.ts +68 -0
  125. package/dist/utils/logging/LoggingConfig.js +64 -0
  126. package/dist/utils/logging/LoggingFactory.d.ts +22 -0
  127. package/dist/utils/logging/LoggingFactory.js +61 -0
  128. package/dist/utils/logging/LoggingService.d.ts +235 -0
  129. package/dist/utils/logging/LoggingService.js +385 -0
  130. package/dist/utils/logging/SimpleLoggingService.d.ts +39 -0
  131. package/dist/utils/logging/SimpleLoggingService.js +58 -0
  132. package/dist/utils/logging/advanced/LogLevel.d.ts +21 -0
  133. package/dist/utils/logging/advanced/LogLevel.js +46 -0
  134. package/dist/utils/logging/advanced/LoggingConfig.d.ts +68 -0
  135. package/dist/utils/logging/advanced/LoggingConfig.js +64 -0
  136. package/dist/utils/logging/advanced/LoggingFactory.d.ts +22 -0
  137. package/dist/utils/logging/advanced/LoggingFactory.js +61 -0
  138. package/dist/utils/logging/advanced/LoggingService.d.ts +235 -0
  139. package/dist/utils/logging/advanced/LoggingService.js +385 -0
  140. package/dist/utils/logging/simple/Loggable.d.ts +33 -0
  141. package/dist/utils/logging/simple/Loggable.js +1 -0
  142. package/dist/utils/logging/simple/LoggingDelegate.d.ts +42 -0
  143. package/dist/utils/logging/simple/LoggingDelegate.js +53 -0
  144. package/dist/utils/logging/simple/SimpleLoggingService.d.ts +39 -0
  145. package/dist/utils/logging/simple/SimpleLoggingService.js +58 -0
  146. package/dist/utils/orientation.d.ts +15 -0
  147. package/dist/utils/orientation.js +32 -0
  148. package/dist/utils/protectedContentManager-simple.d.ts +86 -0
  149. package/dist/utils/protectedContentManager-simple.js +180 -0
  150. package/dist/utils/protectedContentManager.d.ts +162 -0
  151. package/dist/utils/protectedContentManager.js +427 -0
  152. package/dist/utils/screenshotDetector.d.ts +72 -0
  153. package/dist/utils/screenshotDetector.js +179 -0
  154. package/dist/utils/securityOverlayManager-observer-pause.d.ts +283 -0
  155. package/dist/utils/securityOverlayManager-observer-pause.js +878 -0
  156. package/dist/utils/securityOverlayManager-simple.d.ts +197 -0
  157. package/dist/utils/securityOverlayManager-simple.js +552 -0
  158. package/dist/utils/securityOverlayManager.d.ts +260 -0
  159. package/dist/utils/securityOverlayManager.js +774 -0
  160. package/dist/utils/timeoutManager.d.ts +55 -0
  161. package/dist/utils/timeoutManager.js +121 -0
  162. package/package.json +54 -0
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { Loggable } from "./Loggable";
2
+ /**
3
+ * Delegate class that implements logging functionality
4
+ * Can be composed into any class that needs logging
5
+ */
6
+ export declare class LoggingDelegate implements Loggable {
7
+ private logger;
8
+ /**
9
+ * Create a new LoggingDelegate
10
+ * @param componentName Name of the component for log prefixing
11
+ * @param debugMode Whether debug mode is enabled
12
+ */
13
+ constructor(componentName: string, debugMode?: boolean);
14
+ /**
15
+ * Log a debug message if debug mode is enabled
16
+ * @param message Message to log
17
+ * @param args Additional arguments to log
18
+ */
19
+ log(message: string, ...args: unknown[]): void;
20
+ /**
21
+ * Log a warning message
22
+ * @param message Warning message
23
+ * @param args Additional arguments to log
24
+ */
25
+ warn(message: string, ...args: unknown[]): void;
26
+ /**
27
+ * Log an error message
28
+ * @param message Error message
29
+ * @param args Additional arguments to log
30
+ */
31
+ error(message: string, ...args: unknown[]): void;
32
+ /**
33
+ * Set debug mode
34
+ * @param enabled Whether debug mode should be enabled
35
+ */
36
+ setDebugMode(enabled: boolean): void;
37
+ /**
38
+ * Check if debug mode is enabled
39
+ * @returns Whether debug mode is enabled
40
+ */
41
+ isDebugEnabled(): boolean;
42
+ }
@@ -0,0 +1,53 @@
1
+ import { SimpleLoggingService } from "./SimpleLoggingService";
2
+ /**
3
+ * Delegate class that implements logging functionality
4
+ * Can be composed into any class that needs logging
5
+ */
6
+ export class LoggingDelegate {
7
+ /**
8
+ * Create a new LoggingDelegate
9
+ * @param componentName Name of the component for log prefixing
10
+ * @param debugMode Whether debug mode is enabled
11
+ */
12
+ constructor(componentName, debugMode = false) {
13
+ this.logger = new SimpleLoggingService(componentName, debugMode);
14
+ }
15
+ /**
16
+ * Log a debug message if debug mode is enabled
17
+ * @param message Message to log
18
+ * @param args Additional arguments to log
19
+ */
20
+ log(message, ...args) {
21
+ this.logger.log(message, ...args);
22
+ }
23
+ /**
24
+ * Log a warning message
25
+ * @param message Warning message
26
+ * @param args Additional arguments to log
27
+ */
28
+ warn(message, ...args) {
29
+ this.logger.warn(message, ...args);
30
+ }
31
+ /**
32
+ * Log an error message
33
+ * @param message Error message
34
+ * @param args Additional arguments to log
35
+ */
36
+ error(message, ...args) {
37
+ this.logger.error(message, ...args);
38
+ }
39
+ /**
40
+ * Set debug mode
41
+ * @param enabled Whether debug mode should be enabled
42
+ */
43
+ setDebugMode(enabled) {
44
+ this.logger.setDebugMode(enabled);
45
+ }
46
+ /**
47
+ * Check if debug mode is enabled
48
+ * @returns Whether debug mode is enabled
49
+ */
50
+ isDebugEnabled() {
51
+ return this.logger.isDebugEnabled();
52
+ }
53
+ }
@@ -0,0 +1,39 @@
1
+ export declare class SimpleLoggingService {
2
+ private componentName;
3
+ private debugMode;
4
+ /**
5
+ * Create a new SimpleLoggingService
6
+ * for consistent logging across the toolkit
7
+ * @param componentName Name of the component for log prefixing
8
+ * @param debugMode Whether debug mode is enabled
9
+ */
10
+ constructor(componentName: string, debugMode?: boolean);
11
+ /**
12
+ * Log a debug message if debug mode is enabled
13
+ * @param message Message to log
14
+ * @param args Additional arguments to log
15
+ */
16
+ log(message: string, ...args: unknown[]): void;
17
+ /**
18
+ * Log a warning message
19
+ * @param message Warning message
20
+ * @param args Additional arguments to log
21
+ */
22
+ warn(message: string, ...args: unknown[]): void;
23
+ /**
24
+ * Log an error message
25
+ * @param message Error message
26
+ * @param args Additional arguments to log
27
+ */
28
+ error(message: string, ...args: unknown[]): void;
29
+ /**
30
+ * Set debug mode
31
+ * @param enabled Whether debug mode should be enabled
32
+ */
33
+ setDebugMode(enabled: boolean): void;
34
+ /**
35
+ * Get current debug mode
36
+ * @returns Whether debug mode is enabled
37
+ */
38
+ isDebugEnabled(): boolean;
39
+ }
@@ -0,0 +1,58 @@
1
+ export class SimpleLoggingService {
2
+ /**
3
+ * Create a new SimpleLoggingService
4
+ * for consistent logging across the toolkit
5
+ * @param componentName Name of the component for log prefixing
6
+ * @param debugMode Whether debug mode is enabled
7
+ */
8
+ constructor(componentName, debugMode = false) {
9
+ this.componentName = componentName;
10
+ this.debugMode = debugMode;
11
+ }
12
+ /**
13
+ * Log a debug message if debug mode is enabled
14
+ * @param message Message to log
15
+ * @param args Additional arguments to log
16
+ */
17
+ log(message, ...args) {
18
+ if (this.debugMode) {
19
+ console.log(`${this.componentName}: ${message}`, ...args);
20
+ }
21
+ }
22
+ /**
23
+ * Log a warning message
24
+ * @param message Warning message
25
+ * @param args Additional arguments to log
26
+ */
27
+ warn(message, ...args) {
28
+ if (this.debugMode) {
29
+ console.warn(`${this.componentName}: ${message}`, ...args);
30
+ }
31
+ else {
32
+ // In non-debug mode, only log the message without args for brevity
33
+ console.warn(`${this.componentName}: ${message}`);
34
+ }
35
+ }
36
+ /**
37
+ * Log an error message
38
+ * @param message Error message
39
+ * @param args Additional arguments to log
40
+ */
41
+ error(message, ...args) {
42
+ console.error(`${this.componentName}: ${message}`, ...args);
43
+ }
44
+ /**
45
+ * Set debug mode
46
+ * @param enabled Whether debug mode should be enabled
47
+ */
48
+ setDebugMode(enabled) {
49
+ this.debugMode = enabled;
50
+ }
51
+ /**
52
+ * Get current debug mode
53
+ * @returns Whether debug mode is enabled
54
+ */
55
+ isDebugEnabled() {
56
+ return this.debugMode;
57
+ }
58
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Check if the device is currently in landscape mode using the Screen Orientation API
3
+ * @returns true if in landscape mode, false if in portrait mode
4
+ */
5
+ export declare function isLandscape(): boolean;
6
+ /**
7
+ * Get the current orientation type
8
+ * @returns The orientation type string or null if not available
9
+ */
10
+ export declare function getOrientationType(): string | null;
11
+ /**
12
+ * Get the current orientation angle in degrees
13
+ * @returns The orientation angle in degrees or 0 if not available
14
+ */
15
+ export declare function getOrientationAngle(): number;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Check if the device is currently in landscape mode using the Screen Orientation API
3
+ * @returns true if in landscape mode, false if in portrait mode
4
+ */
5
+ export function isLandscape() {
6
+ if (typeof screen === 'undefined' || !screen.orientation) {
7
+ // Fallback to window dimensions if Screen Orientation API is not available
8
+ return window.innerWidth > window.innerHeight;
9
+ }
10
+ // Use Screen Orientation API
11
+ return screen.orientation.type.includes('landscape');
12
+ }
13
+ /**
14
+ * Get the current orientation type
15
+ * @returns The orientation type string or null if not available
16
+ */
17
+ export function getOrientationType() {
18
+ if (typeof screen === 'undefined' || !screen.orientation) {
19
+ return window.innerWidth > window.innerHeight ? 'landscape' : 'portrait';
20
+ }
21
+ return screen.orientation.type;
22
+ }
23
+ /**
24
+ * Get the current orientation angle in degrees
25
+ * @returns The orientation angle in degrees or 0 if not available
26
+ */
27
+ export function getOrientationAngle() {
28
+ if (typeof screen === 'undefined' || !screen.orientation) {
29
+ return 0;
30
+ }
31
+ return screen.orientation.angle;
32
+ }
@@ -0,0 +1,86 @@
1
+ import type { MediatorAware, ProtectionMediator } from "../core/mediator/types";
2
+ /**
3
+ * Options for the protected content placeholder
4
+ */
5
+ export interface PlaceholderOptions {
6
+ /**
7
+ * Title to display in the placeholder
8
+ */
9
+ title?: string;
10
+ /**
11
+ * Main message to display
12
+ */
13
+ message?: string;
14
+ /**
15
+ * Secondary message to display
16
+ */
17
+ secondaryMessage?: string;
18
+ /**
19
+ * Text color for the placeholder
20
+ */
21
+ textColor?: string;
22
+ /**
23
+ * Background color for the placeholder
24
+ */
25
+ backgroundColor?: string;
26
+ }
27
+ /**
28
+ * Utility class to manage protected content by hiding and revealing it
29
+ */
30
+ export declare class ProtectedContentManager implements MediatorAware {
31
+ readonly COMPONENT_NAME = "ProtectedContentManager";
32
+ private mediator;
33
+ private targetElement;
34
+ private originalContent;
35
+ private debugMode;
36
+ /**
37
+ * Create a new ProtectedContentManager
38
+ * @param targetElement Element containing sensitive content to protect
39
+ * @param debugMode Enable debug mode for troubleshooting
40
+ */
41
+ constructor(targetElement: HTMLElement, debugMode?: boolean);
42
+ /**
43
+ * Set the mediator
44
+ * to communicate with the other components
45
+ */
46
+ setMediator(mediator: ProtectionMediator): void;
47
+ /**
48
+ * Handle content hidden event
49
+ */
50
+ private handleContentHidden;
51
+ /**
52
+ * Handle content restored event
53
+ */
54
+ private handleContentRestored;
55
+ /**
56
+ * Hide the original content and replace it with a placeholder
57
+ * @param options Options for customizing the placeholder
58
+ * @returns True if content was hidden, false if there was no content to hide
59
+ */
60
+ hideContent(options: PlaceholderOptions): boolean;
61
+ /**
62
+ * Restore the original content
63
+ * @returns True if content was restored, false if there was no content to restore
64
+ */
65
+ restoreContent(): boolean;
66
+ /**
67
+ * Check if content is currently hidden
68
+ * @returns True if content is hidden, false otherwise
69
+ */
70
+ isContentHidden(): boolean;
71
+ /**
72
+ * Update the target element
73
+ * @param element New target element
74
+ */
75
+ updateTargetElement(element: HTMLElement): void;
76
+ /**
77
+ * Get the current target element
78
+ * @returns The current target element
79
+ */
80
+ getTargetElement(): HTMLElement;
81
+ /**
82
+ * Set debug mode
83
+ * @param enabled Whether debug mode should be enabled
84
+ */
85
+ setDebugMode(enabled: boolean): void;
86
+ }
@@ -0,0 +1,180 @@
1
+ import { ProtectionEventType } from "../core/mediator/protection-event";
2
+ /**
3
+ * Utility class to manage protected content by hiding and revealing it
4
+ */
5
+ export class ProtectedContentManager {
6
+ /**
7
+ * Create a new ProtectedContentManager
8
+ * @param targetElement Element containing sensitive content to protect
9
+ * @param debugMode Enable debug mode for troubleshooting
10
+ */
11
+ constructor(targetElement, debugMode = false) {
12
+ this.COMPONENT_NAME = "ProtectedContentManager";
13
+ this.mediator = null;
14
+ this.originalContent = null;
15
+ this.targetElement = targetElement;
16
+ this.debugMode = debugMode;
17
+ }
18
+ /**
19
+ * Set the mediator
20
+ * to communicate with the other components
21
+ */
22
+ setMediator(mediator) {
23
+ this.mediator = mediator;
24
+ // Subscribe only to general events directly related to content management
25
+ this.mediator.subscribe(ProtectionEventType.CONTENT_HIDDEN, this.handleContentHidden.bind(this), {
26
+ context: this.COMPONENT_NAME,
27
+ });
28
+ this.mediator.subscribe(ProtectionEventType.CONTENT_RESTORED, this.handleContentRestored.bind(this), {
29
+ context: this.COMPONENT_NAME,
30
+ });
31
+ if (this.debugMode) {
32
+ console.log("ProtectedContentManager: Mediator set and subscriptions established");
33
+ }
34
+ }
35
+ /**
36
+ * Handle content hidden event
37
+ */
38
+ handleContentHidden(event) {
39
+ try {
40
+ if (this.debugMode) {
41
+ console.log(`ProtectedContentManager: Received content hidden event from ${event.source}`, event.data);
42
+ }
43
+ const data = event.data;
44
+ if (!data || !data.options)
45
+ return;
46
+ // Only process if this is for our target element or we're the default handler
47
+ if (data.targetElement && data.targetElement !== this.targetElement)
48
+ return;
49
+ this.hideContent(data.options);
50
+ }
51
+ catch (error) {
52
+ console.error("ProtectedContentManager: Error handling content hidden event", error);
53
+ }
54
+ }
55
+ /**
56
+ * Handle content restored event
57
+ */
58
+ handleContentRestored(event) {
59
+ try {
60
+ if (this.debugMode) {
61
+ console.log(`ProtectedContentManager: Received content restored event from ${event.source}`, event.data);
62
+ }
63
+ const data = event.data;
64
+ // Only process if this is for our target element or we're the default handler
65
+ if (data && data.targetElement && data.targetElement !== this.targetElement)
66
+ return;
67
+ this.restoreContent();
68
+ }
69
+ catch (error) {
70
+ console.error("ProtectedContentManager: Error handling content restored event", error);
71
+ }
72
+ }
73
+ /**
74
+ * Hide the original content and replace it with a placeholder
75
+ * @param options Options for customizing the placeholder
76
+ * @returns True if content was hidden, false if there was no content to hide
77
+ */
78
+ hideContent(options) {
79
+ if (!this.targetElement)
80
+ return false;
81
+ // Store original content if not already stored
82
+ if (this.originalContent === null) {
83
+ this.originalContent = this.targetElement.innerHTML;
84
+ if (this.debugMode) {
85
+ console.log("ProtectedContentManager: Original content stored");
86
+ }
87
+ }
88
+ // Create placeholder content with custom styles
89
+ const placeholderStyles = {
90
+ padding: "20px",
91
+ textAlign: "center",
92
+ color: options.textColor || "white",
93
+ backgroundColor: options.backgroundColor || "rgba(0, 0, 0, 0.05)",
94
+ borderRadius: "8px",
95
+ margin: "20px",
96
+ boxShadow: "0 2px 10px rgba(0, 0, 0, 0.1)",
97
+ };
98
+ // Convert styles object to inline style string
99
+ const styleString = Object.entries(placeholderStyles)
100
+ .map(([key, value]) => `${key}: ${value}`)
101
+ .join("; ");
102
+ // Replace content with a placeholder that uses the options text
103
+ this.targetElement.innerHTML = `
104
+ <div style="${styleString}">
105
+ <h2 style="font-size: 24px; margin-bottom: 20px; color: ${options.textColor || "black"};">
106
+ ${options.title || "Content Protected"}
107
+ </h2>
108
+ <p style="font-size: 16px; margin-bottom: 15px; color: ${options.textColor || "black"};">
109
+ ${options.message || "This content is protected for security reasons."}
110
+ </p>
111
+ ${options.secondaryMessage
112
+ ? `
113
+ <p style="font-size: 16px; color: ${options.textColor || "black"};">
114
+ ${options.secondaryMessage}
115
+ </p>
116
+ `
117
+ : ""}
118
+ <div style="margin-top: 20px; padding-top: 20px; border-top: 1px solid rgba(255, 255, 255, 0.2);">
119
+ <p style="font-size: 14px; color: ${options.textColor || "black"}; opacity: 0.8;">
120
+ This content is protected by ContentSecurityToolkit
121
+ </p>
122
+ </div>
123
+ </div>
124
+ `;
125
+ if (this.debugMode) {
126
+ console.log("ProtectedContentManager: Content hidden");
127
+ }
128
+ return true;
129
+ }
130
+ /**
131
+ * Restore the original content
132
+ * @returns True if content was restored, false if there was no content to restore
133
+ */
134
+ restoreContent() {
135
+ if (!this.targetElement || this.originalContent === null)
136
+ return false;
137
+ // Restore the original content
138
+ this.targetElement.innerHTML = this.originalContent;
139
+ this.originalContent = null;
140
+ if (this.debugMode) {
141
+ console.log("ProtectedContentManager: Original content restored");
142
+ }
143
+ return true;
144
+ }
145
+ /**
146
+ * Check if content is currently hidden
147
+ * @returns True if content is hidden, false otherwise
148
+ */
149
+ isContentHidden() {
150
+ return this.originalContent !== null;
151
+ }
152
+ /**
153
+ * Update the target element
154
+ * @param element New target element
155
+ */
156
+ updateTargetElement(element) {
157
+ // If content is hidden, restore it before changing the target
158
+ if (this.isContentHidden()) {
159
+ this.restoreContent();
160
+ }
161
+ this.targetElement = element;
162
+ if (this.debugMode) {
163
+ console.log("ProtectedContentManager: Target element updated");
164
+ }
165
+ }
166
+ /**
167
+ * Get the current target element
168
+ * @returns The current target element
169
+ */
170
+ getTargetElement() {
171
+ return this.targetElement;
172
+ }
173
+ /**
174
+ * Set debug mode
175
+ * @param enabled Whether debug mode should be enabled
176
+ */
177
+ setDebugMode(enabled) {
178
+ this.debugMode = enabled;
179
+ }
180
+ }
@@ -0,0 +1,162 @@
1
+ import type { MediatorAware, ProtectionMediator } from "../core/mediator/types";
2
+ /**
3
+ * Options for the protected content placeholder
4
+ */
5
+ export interface PlaceholderOptions {
6
+ /**
7
+ * Title to display in the placeholder
8
+ */
9
+ title?: string;
10
+ /**
11
+ * Main message to display
12
+ */
13
+ message?: string;
14
+ /**
15
+ * Secondary message to display
16
+ */
17
+ secondaryMessage?: string;
18
+ /**
19
+ * Text color for the placeholder
20
+ */
21
+ textColor?: string;
22
+ /**
23
+ * Background color for the placeholder
24
+ */
25
+ backgroundColor?: string;
26
+ }
27
+ /**
28
+ * Utility class to manage protected content by hiding and revealing it
29
+ */
30
+ export declare class ProtectedContentManager implements MediatorAware {
31
+ readonly COMPONENT_NAME = "ProtectedContentManager";
32
+ private mediator;
33
+ private targetElement;
34
+ private originalContent;
35
+ private debugMode;
36
+ private logger;
37
+ private contentStates;
38
+ private activeStateId;
39
+ private stateQueue;
40
+ private onContentHiddenCallback?;
41
+ private onContentRestoredCallback?;
42
+ private lastHideReason;
43
+ /**
44
+ * Create a new ProtectedContentManager
45
+ * @param targetElement Element containing sensitive content to protect
46
+ * @param debugMode Enable debug mode for troubleshooting
47
+ */
48
+ constructor(targetElement: HTMLElement, debugMode?: boolean);
49
+ /**
50
+ * Set the mediator to communicate with the other components
51
+ * @param mediator The protection mediator instance
52
+ */
53
+ setMediator(mediator: ProtectionMediator): void;
54
+ /**
55
+ * Handle content hidden event
56
+ * @param event The protection event containing content hidden data
57
+ */
58
+ private handleContentHidden;
59
+ /**
60
+ * Handle content restored event
61
+ * @param event The protection event containing content restored data
62
+ */
63
+ private handleContentRestored;
64
+ /**
65
+ * Register a new content state
66
+ * @param owner The strategy or component that owns this state
67
+ * @param reason The reason for hiding content
68
+ * @param options Options for the placeholder
69
+ * @param priority Priority for content hiding (higher numbers take precedence)
70
+ * @returns The ID of the registered content state
71
+ */
72
+ private registerContentState;
73
+ /**
74
+ * Add a content state to the queue
75
+ * @param stateId ID of the content state to add to queue
76
+ */
77
+ private addToQueue;
78
+ /**
79
+ * Apply a specific content state by ID
80
+ * @param stateId ID of the content state to apply
81
+ * @returns True if the state was applied successfully
82
+ */
83
+ private applyContentStateById;
84
+ /**
85
+ * Remove content states for a specific owner
86
+ * @param owner The owner to remove states for
87
+ * @returns The number of states removed
88
+ */
89
+ private removeContentStatesByOwner;
90
+ /**
91
+ * Hide the original content and replace it with a placeholder
92
+ * @param options Options for customizing the placeholder
93
+ * @returns True if content was hidden, false if there was no content to hide
94
+ */
95
+ hideContent(options: PlaceholderOptions): boolean;
96
+ /**
97
+ * Restore the original content
98
+ * @returns True if content was restored, false if there was no content to restore
99
+ */
100
+ restoreContent(): boolean;
101
+ /**
102
+ * Check if content is currently hidden
103
+ * @returns True if content is hidden, false otherwise
104
+ */
105
+ isContentHidden(): boolean;
106
+ /**
107
+ * Get the currently active content state ID
108
+ * @returns The active content state ID or null if none is active
109
+ */
110
+ getActiveContentStateId(): string | null;
111
+ /**
112
+ * Get all content states for a specific owner
113
+ * @param owner The owner to get states for
114
+ * @returns Array of content state IDs
115
+ */
116
+ getContentStatesByOwner(owner: string): string[];
117
+ /**
118
+ * Update the target element
119
+ * @param element New target element
120
+ */
121
+ updateTargetElement(element: HTMLElement): void;
122
+ /**
123
+ * Get the current target element
124
+ * @returns The current target element
125
+ */
126
+ getTargetElement(): HTMLElement;
127
+ /**
128
+ * Set debug mode
129
+ * @param enabled Whether debug mode should be enabled
130
+ */
131
+ setDebugMode(enabled: boolean): void;
132
+ /**
133
+ * Set callbacks for content visibility changes
134
+ * Useful for frameworks like Vue that need to re-mount components after content restoration
135
+ * @param onHidden Callback invoked when content is hidden
136
+ * @param onRestored Callback invoked when content is restored
137
+ */
138
+ setContentCallbacks(onHidden?: (reason: string, targetElement: HTMLElement | null) => void, onRestored?: (targetElement: HTMLElement | null) => void): void;
139
+ /**
140
+ * Clear all content states
141
+ * @returns The number of states removed
142
+ */
143
+ clearAllContentStates(): number;
144
+ /**
145
+ * Get debug information about content states
146
+ * @returns Object with debug information
147
+ */
148
+ getDebugInfo(): {
149
+ totalStates: number;
150
+ statesByOwner: Record<string, number>;
151
+ statesByReason: Record<string, number>;
152
+ activeStateId: string | null;
153
+ queueLength: number;
154
+ stateDetails: Array<{
155
+ id: string;
156
+ owner: string;
157
+ reason: string;
158
+ priority: number;
159
+ hiddenAt: number;
160
+ }>;
161
+ };
162
+ }