67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
// content.js
|
|
// This script runs on the web app page (e.g. localhost:5173)
|
|
|
|
console.log('Tools App Extension: Content script injected');
|
|
|
|
// Listen for messages from the Web App (Vue)
|
|
window.addEventListener('message', (event) => {
|
|
// We should verify the origin, but since we are running on the page itself, we trust window messages
|
|
// from our own app.
|
|
if (event.source !== window) return;
|
|
|
|
if (event.data.type && event.data.type === 'TOOLS_APP_INIT') {
|
|
// console.log('Tools App Extension: Received init from Web App');
|
|
window.postMessage({ type: 'TOOLS_APP_EXTENSION_READY', version: '1.0' }, '*');
|
|
}
|
|
|
|
// Heartbeat check
|
|
if (event.data.type === 'TOOLS_APP_PING') {
|
|
try {
|
|
// Only respond if the extension context is still valid
|
|
if (chrome.runtime && chrome.runtime.id) {
|
|
window.postMessage({ type: 'TOOLS_APP_PONG' }, '*');
|
|
}
|
|
} catch (e) {
|
|
// Extension context invalidated
|
|
// console.warn('Extension context invalidated during ping');
|
|
}
|
|
}
|
|
|
|
// Example: Receive request to sniff clipboard
|
|
if (event.data.type === 'TOOLS_APP_START_SNIFFING') {
|
|
// console.log('Tools App Extension: Start sniffing request');
|
|
// Relay to background script
|
|
try {
|
|
chrome.runtime.sendMessage({ action: 'startSniffing' });
|
|
} catch (e) {
|
|
console.warn('Tools App Extension: Connection lost, please reload the page', e);
|
|
}
|
|
}
|
|
|
|
if (event.data.type === 'TOOLS_APP_STOP_SNIFFING') {
|
|
// console.log('Tools App Extension: Stop sniffing request');
|
|
try {
|
|
chrome.runtime.sendMessage({ action: 'stopSniffing' });
|
|
} catch (e) {
|
|
// ignore
|
|
}
|
|
}
|
|
});
|
|
|
|
// Listen for messages from the Extension Background
|
|
try {
|
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
if (request.action === 'clipboardUpdate') {
|
|
// Send to Web App
|
|
window.postMessage({ type: 'TOOLS_APP_CLIPBOARD_UPDATE', content: request.content }, '*');
|
|
}
|
|
|
|
// Respond to background ping to confirm we are alive
|
|
if (request.action === 'ping') {
|
|
sendResponse('pong');
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.warn('Tools App Extension: Could not add listener', e);
|
|
}
|