-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageScript.js
836 lines (711 loc) · 33.1 KB
/
pageScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// This script runs in the context of the page and can monitor the actual window.postMessage calls
(function () {
// Track all registered event listeners
const listeners = [];
let logEnabled = false;
let consoleEnhancementEnabled = true;
let reroutingEnabled = true;
let consolePatched = false;
// Store original console methods
let originalConsoleLog = null;
let originalConsoleDir = null;
// Helper function to generate a fingerprint for a function
function getFunctionFingerprint(fn) {
// Return placeholder for our own handlers
if (fn.__postMessageMonitor_handler) {
return {
code: "[PostMessage Monitor's internal handler]",
location: "extension",
hash: 0
};
}
const fnStr = fn.toString();
// Create a unique ID for this function
const fnHash = String(fnStr).split('').reduce((a, b) => (((a << 5) - a) + b.charCodeAt(0)) | 0, 0);
// Try to extract real location from function source if possible
let location = "unknown";
// Look for source URL comment that some minifiers/bundlers add
const sourceURLMatch = fnStr.match(/\/\/[#@]\s*sourceURL=\s*(\S+)/);
if (sourceURLMatch) {
location = sourceURLMatch[1];
} else {
// Check for source mapping URL
const sourceMappingURLMatch = fnStr.match(/\/\/[#@]\s*sourceMappingURL=\s*(\S+)/);
if (sourceMappingURLMatch) {
location = "mapped: " + sourceMappingURLMatch[1];
} else {
// Try to get location from error stack (less reliable)
try {
// Create an error and parse its stack
const err = new Error();
// Force creation of stack property
Error.captureStackTrace(err, getFunctionFingerprint);
const stackLines = err.stack.split('\n');
// Look for a stack line that isn't from our extension
for (let i = 1; i < stackLines.length; i++) {
const line = stackLines[i];
// Skip our extension's code
if (line.includes('postmessage-monitor') || line.includes('chrome-extension')) {
continue;
}
const match = line.match(/at .* \((.*):(\d+):(\d+)\)/) ||
line.match(/at (.*):(\d+):(\d+)/);
if (match) {
location = `${match[1]}:${match[2]}`;
break;
}
}
} catch (e) {
// If anything goes wrong, fall back to "unknown"
location = "unknown";
}
}
}
return {
code: fnStr.length > 100 ? fnStr.substring(0, 100) + '...' : fnStr,
location: location,
hash: fnHash
};
}
// Helper function to check if a function is our own monitoring code
function isOurOwnListener(fn) {
if (!fn) return false;
// Check for special marker property
if (fn.__postMessageMonitor_handler) return true;
// Check the function string for our identifying comment
const fnStr = fn.toString();
return fnStr.includes("// Ignore our own messages") ||
fnStr.includes("__postMessageMonitor") ||
fnStr.includes("__postMessageMonitorControl");
}
// Additional filtering helper for UI/display
function isExtensionListener(code) {
if (!code) return false;
return code.includes("// Ignore our own messages") ||
code.includes("__postMessageMonitor") ||
code.includes("PostMessage Monitor") ||
code.includes("[PostMessage Monitor]");
}
// Library detection and unwrapping helpers
const wrappers = {
// Check if Raven/Sentry is present
isRavenPresent: function () {
return typeof window.Raven !== 'undefined' || typeof window.Sentry !== 'undefined';
},
// Check if New Relic is present
isNewRelicPresent: function () {
return typeof window.newrelic !== 'undefined' || typeof window.NREUM !== 'undefined';
},
// Check if Rollbar is present
isRollbarPresent: function () {
return typeof window.Rollbar !== 'undefined';
},
// Check if Bugsnag is present
isBugsnagPresent: function () {
return typeof window.Bugsnag !== 'undefined';
},
// Check if jQuery is present
isjQueryPresent: function () {
return typeof window.jQuery !== 'undefined' || typeof window.$ !== 'undefined';
},
// Unwrap Raven/Sentry wrapped function
unwrapRaven: function (fn) {
if (typeof fn !== 'function') return fn;
// Check for Raven.__wrap pattern
if (fn.__raven__ && typeof fn.__orig__ === 'function') {
return fn.__orig__;
}
// Check for Sentry wrap pattern
if (fn.__sentry_wrapped__ && typeof fn.__sentry_original__ === 'function') {
return fn.__sentry_original__;
}
return fn;
},
// Unwrap New Relic wrapped function
unwrapNewRelic: function (fn) {
if (typeof fn !== 'function') return fn;
// Check for New Relic wrapping patterns
if (fn.nr && typeof fn.__nr_original === 'function') {
return fn.__nr_original;
}
// Another NR pattern
if (fn.__NR_original && typeof fn.__NR_original === 'function') {
return fn.__NR_original;
}
return fn;
},
// Unwrap Rollbar wrapped function
unwrapRollbar: function (fn) {
if (typeof fn !== 'function') return fn;
// Check for Rollbar wrapping pattern
if (fn._rollbar_wrapped && typeof fn._rollbar_wrapped === 'function') {
return fn._rollbar_wrapped;
}
return fn;
},
// Unwrap Bugsnag wrapped function
unwrapBugsnag: function (fn) {
if (typeof fn !== 'function') return fn;
// Check for Bugsnag wrapping pattern
if (fn.bugsnag && typeof fn.bugsnag.originalFunction === 'function') {
return fn.bugsnag.originalFunction;
}
return fn;
},
// Unwrap jQuery event handler
unwrapjQuery: function (fn) {
if (typeof fn !== 'function') return fn;
// Check for jQuery handler pattern
if (fn.guid && fn.handler && typeof fn.handler === 'function') {
return fn.handler;
}
return fn;
},
// Try all unwrappers to get the original function
unwrapAll: function (fn) {
if (typeof fn !== 'function') return fn;
let unwrapped = fn;
// Keep unwrapping until we can't unwrap anymore
let prevFn;
do {
prevFn = unwrapped;
if (this.isRavenPresent()) unwrapped = this.unwrapRaven(unwrapped);
if (this.isNewRelicPresent()) unwrapped = this.unwrapNewRelic(unwrapped);
if (this.isRollbarPresent()) unwrapped = this.unwrapRollbar(unwrapped);
if (this.isBugsnagPresent()) unwrapped = this.unwrapBugsnag(unwrapped);
if (this.isjQueryPresent()) unwrapped = this.unwrapjQuery(unwrapped);
// If the function reference hasn't changed, we're done unwrapping
} while (unwrapped !== prevFn);
return unwrapped;
}
};
// Monitor addEventListener calls
const originalAddEventListener = window.EventTarget.prototype.addEventListener;
window.EventTarget.prototype.addEventListener = function (type, listener, options) {
// Only track message event listeners
if (type === 'message' && typeof listener === 'function') {
// Check if this is our own listener
if (isOurOwnListener(listener)) {
// Mark our own listener for future reference
listener.__postMessageMonitor_handler = true;
// Call original function and skip tracking
return originalAddEventListener.apply(this, arguments);
}
// Try to unwrap the listener to get the original function
const unwrappedListener = wrappers.unwrapAll(listener);
// Skip if unwrapped listener is our own
if (isOurOwnListener(unwrappedListener)) {
// Call original function and skip tracking
return originalAddEventListener.apply(this, arguments);
}
// Create fingerprints for both wrapped and unwrapped versions
const wrappedFingerprint = getFunctionFingerprint(listener);
const unwrappedFingerprint = unwrappedListener !== listener ?
getFunctionFingerprint(unwrappedListener) :
wrappedFingerprint;
// Create a sanitized version of the fingerprints that can be cloned
const wrappedFingerprintSafe = {
code: wrappedFingerprint.code,
location: wrappedFingerprint.location,
hash: wrappedFingerprint.hash
};
const unwrappedFingerprintSafe = {
code: unwrappedFingerprint.code,
location: unwrappedFingerprint.location,
hash: unwrappedFingerprint.hash
};
// Add to our tracked listeners - using safe references that can be cloned
const listenerInfo = {
type: 'message',
wrapped: wrappedFingerprintSafe,
unwrapped: unwrappedFingerprintSafe,
isUnwrapped: unwrappedListener !== listener,
// We don't store actual function references, as they can't be cloned
wrapperType: getWrapperType(listener),
target: this === window ? 'window' : 'other-target',
timestamp: Date.now()
};
listeners.push(listenerInfo);
// Override the listener with a special proxy that helps with debugging
if (unwrappedListener !== listener && reroutingEnabled) {
const proxyListener = function (event) {
// Log information about this event being triggered
if (logEnabled) {
console.group(`%c[PostMessage Monitor] Message event caught`, 'color: #4688f1; font-weight: bold;');
console.log('Event:', event);
console.log('Original wrapper:', listener);
console.log('Unwrapped handler:', unwrappedListener);
console.log('Source path:', event.source ? getWindowPath(event.source) : 'unknown');
console.log('Target path:', getWindowPath(window));
console.groupEnd();
}
// Call the original listener
return listener.apply(this, arguments);
};
// Store a reference to the original and unwrapped functions
// We use __postMessageMonitor prefix to avoid conflicts
proxyListener.__postMessageMonitor_original = listener;
proxyListener.__postMessageMonitor_unwrapped = unwrappedListener;
proxyListener.__postMessageMonitor_handler = true; // Mark as our code
// Replace the listener with our proxy
arguments[1] = proxyListener;
}
// Notify about the new listener
notifyListenersUpdated();
}
// Call the original function
return originalAddEventListener.apply(this, arguments);
};
// Monitor removeEventListener calls
const originalRemoveEventListener = window.EventTarget.prototype.removeEventListener;
window.EventTarget.prototype.removeEventListener = function (type, listener, options) {
// Only track message event listeners
if (type === 'message' && typeof listener === 'function') {
// Check if this is our proxy listener
if (listener.__postMessageMonitor_original) {
// Get the original listener
const originalListener = listener.__postMessageMonitor_original;
// Replace with original for removal
arguments[1] = originalListener;
// Handle our tracking separately
const fingerprint = getFunctionFingerprint(originalListener);
// Remove from our tracked listeners
const index = listeners.findIndex(l =>
l.type === 'message' &&
l.wrapped.hash === fingerprint.hash
);
if (index !== -1) {
listeners.splice(index, 1);
notifyListenersUpdated();
}
// Call the original function with the original listener
return originalRemoveEventListener.apply(this, arguments);
}
// Normal case - not our proxy
const fingerprint = getFunctionFingerprint(listener);
// Remove from our tracked listeners
const index = listeners.findIndex(l =>
l.type === 'message' &&
l.wrapped.hash === fingerprint.hash
);
if (index !== -1) {
listeners.splice(index, 1);
notifyListenersUpdated();
}
}
// Call the original function
return originalRemoveEventListener.apply(this, arguments);
};
// Helper function to get path to a window object
function getWindowPath(win) {
if (!win) return 'unknown';
try {
if (win === window) return 'self';
if (win === top) return 'top';
if (win === parent) return 'parent';
// Check if it's one of our frames
for (let i = 0; i < frames.length; i++) {
if (win === frames[i]) return `frames[${i}]`;
}
// Check if it's a frame's frame
for (let i = 0; i < frames.length; i++) {
try {
const subframes = frames[i].frames;
for (let j = 0; j < subframes.length; j++) {
if (win === subframes[j]) return `frames[${i}].frames[${j}]`;
}
} catch (e) {
// Cross-origin access might fail
}
}
return 'unknown-window';
} catch (e) {
return 'access-denied';
}
}
// Determine the type of wrapper used for a function
function getWrapperType(fn) {
if (!fn) return 'unknown';
if (fn.__raven__ || fn.__sentry_wrapped__) return 'Raven/Sentry';
if (fn.nr || fn.__NR_original) return 'New Relic';
if (fn._rollbar_wrapped) return 'Rollbar';
if (fn.bugsnag) return 'Bugsnag';
if (fn.guid && fn.handler) return 'jQuery';
return 'unknown wrapper';
}
// Monitor postMessage calls
const originalPostMessage = window.postMessage;
window.postMessage = function (message, targetOrigin, transfer) {
// Ignore our own messages
if (message && (message.__postMessageMonitor || message.__postMessageMonitorControl)) {
return originalPostMessage.apply(this, arguments);
}
try {
// Clone the message to ensure it can be serialized
let safeMessage;
try {
// Test if the message is serializable by using structured clone algorithm
safeMessage = structuredClone(message);
} catch (e) {
// If not serializable, create a simple representation
safeMessage = {
__simplified: true,
type: typeof message,
toString: String(message).substring(0, 500)
};
if (typeof message === 'object' && message !== null) {
safeMessage.keys = Object.keys(message);
}
}
// Always send to extension
window.postMessage({
__postMessageMonitor: {
type: "MESSAGE_SENT",
data: safeMessage,
targetOrigin: targetOrigin,
timestamp: Date.now()
}
}, "*");
// Only log to console if enabled
if (logEnabled) {
console.group(`%c[PostMessage Monitor] Outgoing Message to ${targetOrigin || '*'}`, 'color: #1976d2; font-weight: bold;');
console.log('Data:', message);
console.log('Target origin:', targetOrigin || '*');
console.log('Sender path:', getWindowPath(window));
// Get call stack info
const stack = new Error().stack;
console.log('Call stack:', stack);
// Find the caller of postMessage
const callMatch = stack.split('\n')[2].match(/at (.*) \((.*):(\d+):(\d+)\)/);
if (callMatch) {
console.log('Called from:', `${callMatch[1]} at ${callMatch[2]}:${callMatch[3]}`);
}
console.groupEnd();
}
} catch (e) {
console.error('[PostMessage Monitor] Error handling message:', e);
}
// Call the original function
return originalPostMessage.apply(this, arguments);
};
// Also handle Window.prototype.postMessage for completeness
if (Window.prototype.postMessage && Window.prototype.postMessage !== window.postMessage) {
const originalWindowPrototypePostMessage = Window.prototype.postMessage;
Window.prototype.postMessage = function (message, targetOrigin, transfer) {
// Ignore our own messages
if (message && (message.__postMessageMonitor || message.__postMessageMonitorControl)) {
return originalWindowPrototypePostMessage.apply(this, arguments);
}
try {
const targetWindow = this;
const targetPath = getWindowPath(targetWindow);
// Clone the message to ensure it can be serialized
let safeMessage;
try {
// Test if the message is serializable
safeMessage = structuredClone(message);
} catch (e) {
// If not serializable, create a simple representation
safeMessage = {
__simplified: true,
type: typeof message,
toString: String(message).substring(0, 500)
};
if (typeof message === 'object' && message !== null) {
safeMessage.keys = Object.keys(message);
}
}
// Always send to extension
window.postMessage({
__postMessageMonitor: {
type: "MESSAGE_SENT",
data: safeMessage,
targetOrigin: targetOrigin,
targetPath: targetPath,
timestamp: Date.now()
}
}, "*");
// Only log to console if enabled
if (logEnabled) {
console.group(`%c[PostMessage Monitor] Cross-window Message to ${targetPath}`, 'color: #e91e63; font-weight: bold;');
console.log('Data:', message);
console.log('Target origin:', targetOrigin || '*');
console.log('Target window:', targetWindow);
console.log('Target path:', targetPath);
console.log('Sender path:', getWindowPath(window));
// Get call stack info
const stack = new Error().stack;
console.log('Call stack:', stack);
// Find the caller
const callMatch = stack.split('\n')[2].match(/at (.*) \((.*):(\d+):(\d+)\)/);
if (callMatch) {
console.log('Called from:', `${callMatch[1]} at ${callMatch[2]}:${callMatch[3]}`);
}
console.groupEnd();
}
} catch (e) {
console.error('[PostMessage Monitor] Error handling cross-window message:', e);
}
// Call the original function
return originalWindowPrototypePostMessage.apply(this, arguments);
};
}
// Capture incoming messages
window.addEventListener('message', function (event) {
// Ignore our own messages
if (event.data && (event.data.__postMessageMonitor || event.data.__postMessageMonitorControl)) {
// Check if this is a control message
if (event.data.__postMessageMonitorControl) {
const control = event.data.__postMessageMonitorControl;
if (control.type === "UPDATE_LOGGING") {
logEnabled = control.enabled;
} else if (control.type === "UPDATE_CONSOLE_ENHANCEMENT") {
// Update console enhancement setting
consoleEnhancementEnabled = control.enabled;
// If disabling, restore original console methods
if (!control.enabled && originalConsoleLog) {
console.log = originalConsoleLog;
console.dir = originalConsoleDir;
} else if (control.enabled && !consolePatched) {
// Re-apply console patches
patchConsole();
}
} else if (control.type === "UPDATE_REROUTING") {
// Update listener rerouting setting
reroutingEnabled = control.enabled;
} else if (control.type === "INIT_SETTINGS" && control.settings) {
// Apply all settings at once
logEnabled = control.settings.logEnabled;
consoleEnhancementEnabled = control.settings.consoleEnhancementEnabled;
reroutingEnabled = control.settings.reroutingEnabled;
// Apply console patching based on new settings
if (consoleEnhancementEnabled && !consolePatched) {
patchConsole();
} else if (!consoleEnhancementEnabled && originalConsoleLog) {
console.log = originalConsoleLog;
console.dir = originalConsoleDir;
}
}
}
return;
}
try {
// Get source window path if available
const sourcePath = event.source ? getWindowPath(event.source) : 'unknown';
// Clone the message data to ensure it can be serialized
let safeData;
try {
// Test if the data is serializable
safeData = structuredClone(event.data);
} catch (e) {
// If not serializable, create a simple representation
safeData = {
__simplified: true,
type: typeof event.data,
toString: String(event.data).substring(0, 500)
};
if (typeof event.data === 'object' && event.data !== null) {
safeData.keys = Object.keys(event.data);
}
}
// Always send to extension
window.postMessage({
__postMessageMonitor: {
type: "MESSAGE_RECEIVED",
data: safeData,
sourceOrigin: event.origin,
sourcePath: sourcePath,
timestamp: Date.now()
}
}, "*");
// Only log to console if enabled
if (logEnabled) {
console.group(`%c[PostMessage Monitor] Incoming Message from ${event.origin}`, 'color: #43a047; font-weight: bold;');
console.log('Data:', event.data);
console.log('Origin:', event.origin);
console.log('Source path:', sourcePath);
console.log('Target path:', getWindowPath(window));
console.log('Event:', event);
// If we have active listeners, show them
const activeListeners = listeners.filter(l => l.type === 'message' &&
// Filter out our own listeners
!l.wrapped.code.includes("// Ignore our own messages") &&
!l.wrapped.code.includes("__postMessageMonitor") &&
!l.unwrapped.code.includes("// Ignore our own messages") &&
!l.unwrapped.code.includes("__postMessageMonitor"));
if (activeListeners.length > 0) {
console.group('Active listeners that will receive this message:');
activeListeners.forEach((listener, idx) => {
console.group(`Listener #${idx + 1}`);
if (listener.isUnwrapped) {
console.log('Original wrapper:', listener.wrapped.code);
console.log('Unwrapped function:', listener.unwrapped.code);
console.log('Wrapper type:', listener.wrapperType || 'unknown');
} else {
console.log('Function:', listener.wrapped.code);
}
console.log('Added at:', new Date(listener.timestamp).toLocaleTimeString());
console.log('Location:', listener.unwrapped.location);
console.groupEnd();
});
console.groupEnd();
}
console.groupEnd();
}
} catch (e) {
console.error('[PostMessage Monitor] Error handling incoming message:', e);
}
}, true); // Use capture to see messages before other handlers
// Hook into console to improve message event display
function patchConsole() {
if (!console || !console.log) return;
// Store references to the original methods if not already saved
if (!originalConsoleLog) {
originalConsoleLog = console.log;
originalConsoleDir = console.dir;
}
// Enhance console.log for event objects
console.log = function (...args) {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
// Check if this is a MessageEvent
if (arg && arg instanceof MessageEvent) {
// If next arg isn't a string descriptor, add one
if (i === args.length - 1 || typeof args[i + 1] !== 'string') {
// Insert extra info about the message event
originalConsoleLog.call(this, arg, '(MessageEvent details:)');
originalConsoleLog.call(this, 'Data:', arg.data);
originalConsoleLog.call(this, 'Origin:', arg.origin);
originalConsoleLog.call(this, 'Source:', arg.source ? getWindowPath(arg.source) : 'unknown');
continue;
}
}
// Regular handling for other arguments
originalConsoleLog.call(this, arg);
}
};
// Enhance console.dir for event listener objects
console.dir = function (obj, options) {
// If this is a function that might be a message event listener
if (typeof obj === 'function') {
const unwrapped = wrappers.unwrapAll(obj);
if (unwrapped !== obj) {
originalConsoleLog.call(this, '%cUnwrapped event listener:', 'color: #e91e63; font-weight: bold');
originalConsoleDir.call(this, unwrapped, options);
originalConsoleLog.call(this, '%cOriginal wrapped function:', 'color: #888; font-weight: bold');
}
}
// Call the original method
originalConsoleDir.call(this, obj, options);
};
consolePatched = true;
}
// Notify about listener updates
function notifyListenersUpdated() {
try {
// Filter out our own listeners before notifying
const filteredListeners = listeners.filter(listener => {
return !(listener.wrapped.code.includes("// Ignore our own messages") ||
listener.wrapped.code.includes("__postMessageMonitor") ||
listener.unwrapped.code.includes("// Ignore our own messages") ||
listener.unwrapped.code.includes("__postMessageMonitor"));
});
// Create a serializable copy of the listeners
const serializableListeners = filteredListeners.map(listener => {
// Make sure we only include properties that can be cloned
return {
type: listener.type,
wrapped: listener.wrapped,
unwrapped: listener.unwrapped,
isUnwrapped: listener.isUnwrapped,
wrapperType: listener.wrapperType,
target: listener.target,
timestamp: listener.timestamp
};
});
window.postMessage({
__postMessageMonitor: {
type: "LISTENERS_UPDATED",
listeners: serializableListeners
}
}, "*");
} catch (e) {
console.error('[PostMessage Monitor] Error notifying about listener updates:', e);
}
}
// Try to patch libraries that wrap event listeners
function patchLibraries() {
// Patch Raven/Sentry wrap method
if (window.Raven && typeof Raven.wrap === 'function') {
const originalRavenWrap = Raven.wrap;
Raven.wrap = function (options, fn) {
const wrapped = originalRavenWrap.apply(this, arguments);
// Make the original function accessible
if (wrapped && typeof fn === 'function') {
wrapped.__postMessageMonitor_unwrapped = fn;
}
return wrapped;
};
}
// Patch Sentry wrap method
if (window.Sentry && typeof Sentry.wrap === 'function') {
const originalSentryWrap = Sentry.wrap;
Sentry.wrap = function (options, fn) {
const wrapped = originalSentryWrap.apply(this, arguments);
// Make the original function accessible
if (wrapped && typeof fn === 'function') {
wrapped.__postMessageMonitor_unwrapped = fn;
}
return wrapped;
};
}
// Patch jQuery event add method if available
if (window.jQuery) {
const originalOn = jQuery.fn.on;
jQuery.fn.on = function (types, selector, data, fn) {
// Handle the different argument patterns
let handler;
if (typeof selector === 'function') {
handler = selector;
selector = undefined;
} else if (typeof data === 'function') {
handler = data;
data = undefined;
} else {
handler = fn;
}
// If this is a message event, mark it
if (types && typeof types === 'string' && types.includes('message') && typeof handler === 'function') {
// Store the original handler
handler.__postMessageMonitor_jquery_original = true;
}
return originalOn.apply(this, arguments);
};
}
}
// Detect existing message listeners
// This is a best-effort approach as we can't access listeners already added
// We'll catch all new ones going forward
// Initial notification of listeners
notifyListenersUpdated();
// Re-check listeners on DOM content loaded
document.addEventListener('DOMContentLoaded', function () {
// Try to patch libraries
patchLibraries();
// Initial console patching
if (consoleEnhancementEnabled) {
patchConsole();
}
notifyListenersUpdated();
});
// Also check on load
window.addEventListener('load', function () {
// Try to patch libraries again
patchLibraries();
// Make sure console is patched
if (consoleEnhancementEnabled && !consolePatched) {
patchConsole();
}
notifyListenersUpdated();
});
})();