-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication-script.js
More file actions
82 lines (71 loc) · 2.46 KB
/
communication-script.js
File metadata and controls
82 lines (71 loc) · 2.46 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
/**
* KaiSign Extension - Communication Script
* Runs in isolated world to handle extension messaging
*/
console.log('[KaiSign-Comm] Communication script loaded');
// Listen for messages from the main world content script
window.addEventListener('message', async (event) => {
// Only accept messages from same origin and with our signature
if (event.source !== window || !event.data.type || event.data.source !== 'kaisign-main') {
return;
}
console.log('[KaiSign-Comm] Received message from main world:', event.data);
const { type, payload } = event.data;
try {
switch (type) {
case 'SAVE_TRANSACTION':
// Send transaction to background script for storage
if (chrome && chrome.runtime) {
chrome.runtime.sendMessage({
type: 'TRANSACTION_CAPTURED',
data: payload
});
console.log('[KaiSign-Comm] ✅ Transaction sent to background script');
// Send success back to main world
window.postMessage({
source: 'kaisign-comm',
type: 'SAVE_TRANSACTION_RESPONSE',
success: true
}, '*');
} else {
throw new Error('Chrome runtime not available');
}
break;
case 'GET_TRANSACTIONS':
// Request transaction history from background
if (chrome && chrome.runtime) {
chrome.runtime.sendMessage({ type: 'GET_TRANSACTION_HISTORY' }, (response) => {
window.postMessage({
source: 'kaisign-comm',
type: 'GET_TRANSACTIONS_RESPONSE',
success: response?.success || false,
data: response?.data || null
}, '*');
});
}
break;
case 'OPEN_POPUP':
// Request popup window from background
if (chrome && chrome.runtime) {
chrome.runtime.sendMessage({
type: 'OPEN_TRANSACTION_POPUP',
data: payload
});
console.log('[KaiSign-Comm] ✅ Popup request sent to background');
}
break;
default:
console.warn('[KaiSign-Comm] Unknown message type:', type);
}
} catch (error) {
console.error('[KaiSign-Comm] Error handling message:', error);
// Send error back to main world
window.postMessage({
source: 'kaisign-comm',
type: type + '_RESPONSE',
success: false,
error: error.message
}, '*');
}
});
console.log('[KaiSign-Comm] Communication script ready');