-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
95 lines (84 loc) · 3.17 KB
/
content.js
File metadata and controls
95 lines (84 loc) · 3.17 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
83
84
85
86
87
88
89
90
91
92
93
94
95
// ConsentFlow Debugger — Content Script (Bridge)
// Runs at document_start in the content script isolated world.
// Injects the page-context engine, relays data between page and popup.
(function () {
'use strict';
if (window.__cfContentLoaded) return;
window.__cfContentLoaded = true;
// ─── Inject page-context script synchronously ─────────────────
const script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js');
script.onload = function () { script.remove(); };
(document.head || document.documentElement).appendChild(script);
// ─── State cache (populated by messages from injected.js) ─────
let cachedState = null;
let pendingRequests = {};
// Listen for messages from the injected page-context script
window.addEventListener('message', function (event) {
if (!event.data || event.data.source !== 'cf-debug-injected') return;
if (event.data.type === 'full-state') {
cachedState = event.data.payload;
// Resolve any pending request
const reqId = event.data.requestId;
if (reqId && pendingRequests[reqId]) {
pendingRequests[reqId](cachedState);
delete pendingRequests[reqId];
}
}
if (event.data.type === 'ready') {
// Request initial state
requestFreshState();
}
if (event.data.type === 'event') {
// Update cache incrementally if we have one
if (cachedState && event.data.payload) {
cachedState.timeline = cachedState.timeline || [];
cachedState.timeline.push(event.data.payload);
if (cachedState.timeline.length > 300) {
cachedState.timeline = cachedState.timeline.slice(-300);
}
}
}
});
function requestFreshState() {
return new Promise(function (resolve) {
const reqId = 'req_' + Date.now() + '_' + Math.random().toString(36).substring(2, 8);
pendingRequests[reqId] = resolve;
window.postMessage({
source: 'cf-debug-content',
type: 'get-state',
requestId: reqId
}, '*');
// Timeout after 3s
setTimeout(function () {
if (pendingRequests[reqId]) {
pendingRequests[reqId](cachedState);
delete pendingRequests[reqId];
}
}, 3000);
});
}
// ─── Handle messages from popup / background ──────────────────
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (!request || !request.action) return;
if (request.action === 'ping') {
sendResponse({ ok: true });
return;
}
if (request.action === 'getState') {
// Return cached state or request fresh
if (cachedState && cachedState.ready) {
// Still request a fresh one in the background for next time
requestFreshState();
sendResponse(cachedState);
} else {
requestFreshState().then(function (state) {
sendResponse(state || { ready: false });
});
return true; // keep channel open for async
}
}
});
// Request initial state after a delay to let injected.js initialize
setTimeout(requestFreshState, 2000);
})();