-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcontent.js
More file actions
82 lines (70 loc) · 2.06 KB
/
Copy pathcontent.js
File metadata and controls
82 lines (70 loc) · 2.06 KB
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
// Inject the interceptor script into the page
(function() {
const DEBUG = false; // Set to true for debugging
function log(...args) {
if (DEBUG) {
console.log('[Stream Panel Content]', ...args);
}
}
const script = document.createElement('script');
script.src = chrome.runtime.getURL('inject.js');
script.onload = function() {
this.remove();
};
// Try to inject as early as possible
const target = document.head || document.documentElement;
if (target) {
target.appendChild(script);
} else {
// If document is not ready, wait for DOMContentLoaded
document.addEventListener('DOMContentLoaded', function() {
(document.head || document.documentElement).appendChild(script);
});
}
})();
// Determine if current context is an iframe
const isIframe = window !== window.top;
const frameUrl = window.location.href;
const DEBUG = false; // Set to true for debugging
function log(...args) {
if (DEBUG) {
console.log('[Stream Panel Content]', ...args);
}
}
// Listen for messages from injected script
window.addEventListener('message', function(event) {
// Only accept messages from the same window
if (event.source !== window) return;
// Check message source
if (!event.data || event.data.source !== 'stream-panel-inject') return;
const payload = event.data.payload;
log('Received message from inject:', payload.type, payload);
// Add frame information
payload.isIframe = isIframe;
payload.frameUrl = frameUrl;
// Forward to background script
try {
chrome.runtime.sendMessage({
source: 'stream-panel-content',
payload: payload
});
log('Forwarded to background');
} catch (e) {
// Extension context may be invalidated
console.warn('[Stream Panel] Failed to send message:', e.message);
}
});
// Notify background that content script is ready
try {
chrome.runtime.sendMessage({
source: 'stream-panel-content',
payload: {
type: 'content-ready',
isIframe: isIframe,
frameUrl: frameUrl,
timestamp: Date.now()
}
});
} catch (e) {
// Ignore
}