feat: implement browser extension UI with WebAssembly integration
This commit is contained in:
81
extension/dist/background/index.js
vendored
Normal file
81
extension/dist/background/index.js
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
// Background service worker for Modular Vault Extension
|
||||
// Handles state persistence between popup sessions
|
||||
|
||||
console.log('Background service worker started');
|
||||
|
||||
// Store session state locally for quicker access
|
||||
let sessionState = {
|
||||
currentKeyspace: null,
|
||||
keypairs: [],
|
||||
selectedKeypair: null
|
||||
};
|
||||
|
||||
// Initialize state from storage
|
||||
chrome.storage.local.get(['currentKeyspace', 'keypairs', 'selectedKeypair'])
|
||||
.then(state => {
|
||||
sessionState = {
|
||||
currentKeyspace: state.currentKeyspace || null,
|
||||
keypairs: state.keypairs || [],
|
||||
selectedKeypair: state.selectedKeypair || null
|
||||
};
|
||||
console.log('Session state loaded from storage:', sessionState);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to load session state:', error);
|
||||
});
|
||||
|
||||
// Handle messages from the popup
|
||||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
console.log('Background received message:', message.action, message.type || '');
|
||||
|
||||
// Update session state
|
||||
if (message.action === 'update_session') {
|
||||
try {
|
||||
const { type, data } = message;
|
||||
|
||||
// Update our local state
|
||||
if (type === 'keyspace') {
|
||||
sessionState.currentKeyspace = data;
|
||||
} else if (type === 'keypair_selected') {
|
||||
sessionState.selectedKeypair = data;
|
||||
} else if (type === 'keypair_added') {
|
||||
sessionState.keypairs = [...sessionState.keypairs, data];
|
||||
} else if (type === 'keypairs_loaded') {
|
||||
// Replace the entire keypair list with what came from the vault
|
||||
console.log('Updating keypairs from vault:', data);
|
||||
sessionState.keypairs = data;
|
||||
} else if (type === 'session_locked') {
|
||||
// When locking, we don't need to maintain keypairs in memory anymore
|
||||
// since they'll be reloaded from the vault when unlocking
|
||||
sessionState = {
|
||||
currentKeyspace: null,
|
||||
keypairs: [], // Clear keypairs from memory since they're in the vault
|
||||
selectedKeypair: null
|
||||
};
|
||||
}
|
||||
|
||||
// Persist to storage
|
||||
chrome.storage.local.set(sessionState)
|
||||
.then(() => {
|
||||
console.log('Updated session state in storage:', sessionState);
|
||||
sendResponse({ success: true });
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to persist session state:', error);
|
||||
sendResponse({ success: false, error: error.message });
|
||||
});
|
||||
|
||||
return true; // Keep connection open for async response
|
||||
} catch (error) {
|
||||
console.error('Error in update_session message handler:', error);
|
||||
sendResponse({ success: false, error: error.message });
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Get session state
|
||||
if (message.action === 'get_session') {
|
||||
sendResponse(sessionState);
|
||||
return false; // No async response needed
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user