89 lines
2.5 KiB
JavaScript
89 lines
2.5 KiB
JavaScript
// Content script for potential webpage integration
|
|
// This can be used to inject CryptoVault functionality into webpages
|
|
|
|
(function() {
|
|
'use strict';
|
|
|
|
// Create a bridge between webpage and extension
|
|
const cryptoVaultBridge = {
|
|
async isAvailable() {
|
|
try {
|
|
const response = await chrome.runtime.sendMessage({ action: 'getStatus' });
|
|
return response.success && response.status;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
},
|
|
|
|
async sign(message) {
|
|
try {
|
|
const response = await chrome.runtime.sendMessage({
|
|
action: 'sign',
|
|
message: Array.from(new TextEncoder().encode(message))
|
|
});
|
|
return response.success ? response.signature : null;
|
|
} catch (error) {
|
|
console.error('CryptoVault sign error:', error);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
async getPublicKey() {
|
|
try {
|
|
const response = await chrome.runtime.sendMessage({ action: 'getCurrentKeypairPublicKey' });
|
|
return response.success ? response.publicKey : null;
|
|
} catch (error) {
|
|
console.error('CryptoVault getPublicKey error:', error);
|
|
return null;
|
|
}
|
|
}
|
|
};
|
|
|
|
// Expose to window for webpage access (optional)
|
|
if (window.location.protocol === 'https:' || window.location.hostname === 'localhost') {
|
|
window.cryptoVault = cryptoVaultBridge;
|
|
|
|
// Dispatch event to let webpage know CryptoVault is available
|
|
window.dispatchEvent(new CustomEvent('cryptovault-ready', {
|
|
detail: { available: true }
|
|
}));
|
|
}
|
|
|
|
// Listen for messages from webpage
|
|
window.addEventListener('message', async (event) => {
|
|
if (event.source !== window || !event.data.cryptoVault) return;
|
|
|
|
const { action, id, data } = event.data;
|
|
let result;
|
|
|
|
try {
|
|
switch (action) {
|
|
case 'sign':
|
|
result = await cryptoVaultBridge.sign(data.message);
|
|
break;
|
|
case 'getPublicKey':
|
|
result = await cryptoVaultBridge.getPublicKey();
|
|
break;
|
|
case 'isAvailable':
|
|
result = await cryptoVaultBridge.isAvailable();
|
|
break;
|
|
default:
|
|
throw new Error('Unknown action: ' + action);
|
|
}
|
|
|
|
window.postMessage({
|
|
cryptoVaultResponse: true,
|
|
id,
|
|
success: true,
|
|
result
|
|
}, '*');
|
|
} catch (error) {
|
|
window.postMessage({
|
|
cryptoVaultResponse: true,
|
|
id,
|
|
success: false,
|
|
error: error.message
|
|
}, '*');
|
|
}
|
|
});
|
|
})(); |