-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge-script.js
More file actions
185 lines (166 loc) · 5.82 KB
/
bridge-script.js
File metadata and controls
185 lines (166 loc) · 5.82 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
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
// ISOLATED world content script - bridge between MAIN world and background
// Inject MAIN world scripts
// Settings are passed via KAISIGN_GET_SETTINGS postMessage (no inline script needed - CSP safe)
(function injectScripts() {
const container = document.documentElement || document.head || document.body;
if (!container) return;
// Inject MAIN world scripts (fallback if manifest injection failed)
const scripts = [
'name-resolution-service.js', 'subgraph-metadata.js', 'onchain-verifier.js',
'runtime-registry.js', 'metadata.js', 'eip712-decoder.js', 'decode.js',
'recursive-decoder.js', 'advanced-decoder.js', 'content-script.js'
];
for (const file of scripts) {
const el = document.createElement('script');
el.src = chrome.runtime.getURL(file);
el.async = false;
container.appendChild(el);
el.onload = () => el.remove();
el.onerror = () => el.remove();
}
})();
// Check chrome.runtime availability
if (typeof chrome === 'undefined' || !chrome.runtime) {
console.error('[KaiSign Bridge] CRITICAL: chrome.runtime not available!');
}
/**
* Safely send message to background script with error handling for extension context invalidation.
* Returns null if the extension context is invalid.
*/
function safeSendMessage(message, callback) {
try {
// Check if extension context is still valid
if (!chrome.runtime?.id) {
console.warn('[KaiSign Bridge] Extension context invalidated - please refresh the page');
callback?.({ error: 'Extension was reloaded. Please refresh the page.' });
return;
}
chrome.runtime.sendMessage(message, (response) => {
// Check for chrome.runtime.lastError (set when extension context is invalid)
if (chrome.runtime.lastError) {
console.warn('[KaiSign Bridge] Message failed:', chrome.runtime.lastError.message);
callback?.({ error: 'Extension context lost. Please refresh the page.' });
return;
}
callback?.(response);
});
} catch (error) {
console.warn('[KaiSign Bridge] Failed to send message:', error.message);
callback?.({ error: 'Extension context invalid. Please refresh the page.' });
}
}
// Listen for messages from MAIN world content script
window.addEventListener('message', (event) => {
// Accept messages from page context (MAIN world)
const message = event.data;
// Filter for KaiSign messages
if (!message || !message.type || !message.type.startsWith('KAISIGN_')) return;
console.log('[KaiSign Bridge] Received from MAIN world:', message.type);
// Check chrome.runtime availability
if (typeof chrome === 'undefined' || !chrome.runtime) {
console.error('[KaiSign Bridge] chrome.runtime not available, cannot forward message');
window.postMessage({
type: message.type.replace('KAISIGN_', 'KAISIGN_') + '_RESPONSE',
error: 'Extension context not available'
}, '*');
return;
}
// Forward to background script
switch (message.type) {
case 'KAISIGN_SAVE_TX':
safeSendMessage({
type: 'SAVE_TRANSACTION',
data: message.data
}, (response) => {
console.log('[KaiSign Bridge] Save response:', response);
// Optionally send response back to MAIN world
window.postMessage({
type: 'KAISIGN_SAVE_TX_RESPONSE',
success: response?.success,
count: response?.count,
error: response?.error
}, '*');
});
break;
case 'KAISIGN_GET_TXS':
safeSendMessage({
type: 'GET_TRANSACTIONS'
}, (response) => {
window.postMessage({
type: 'KAISIGN_GET_TXS_RESPONSE',
transactions: response?.transactions || [],
error: response?.error
}, '*');
});
break;
case 'KAISIGN_CLEAR_TXS':
safeSendMessage({
type: 'CLEAR_TRANSACTIONS'
}, (response) => {
window.postMessage({
type: 'KAISIGN_CLEAR_TXS_RESPONSE',
success: response?.success,
error: response?.error
}, '*');
});
break;
case 'KAISIGN_FETCH_BLOB':
console.log('[KaiSign Bridge] FETCH_BLOB request:', message.url);
safeSendMessage({
type: 'FETCH_BLOB',
url: message.url
}, (response) => {
console.log('[KaiSign Bridge] FETCH_BLOB response:', {
hasData: !!response?.data,
dataPreview: response?.data?.substring?.(0, 200),
error: response?.error
});
window.postMessage({
type: 'KAISIGN_BLOB_RESPONSE',
messageId: message.messageId,
data: response?.data,
error: response?.error
}, '*');
});
break;
case 'KAISIGN_RPC_CALL':
safeSendMessage({
type: 'RPC_CALL',
rpcUrl: message.rpcUrl,
method: message.method,
params: message.params
}, (response) => {
window.postMessage({
type: 'KAISIGN_RPC_RESPONSE',
messageId: message.messageId,
result: response?.result,
error: response?.error
}, '*');
});
break;
case 'KAISIGN_GET_SETTINGS':
safeSendMessage({ type: 'GET_SETTINGS' }, (response) => {
window.postMessage({
type: 'KAISIGN_SETTINGS_RESPONSE',
settings: response?.settings || {},
error: response?.error
}, '*');
});
break;
}
});
// Listen for settings updates from background (when user saves in options page)
try {
chrome.runtime.onMessage.addListener((message) => {
if (message.type === 'KAISIGN_SETTINGS_UPDATED') {
// Forward to MAIN world so services can react to URL changes
window.postMessage({
type: 'KAISIGN_SETTINGS_UPDATED',
settings: message.settings
}, '*');
}
});
} catch (error) {
console.warn('[KaiSign Bridge] Failed to add message listener:', error.message);
}
console.log('[KaiSign] Bridge script ready');