JiraFeedback/background.js
2025-01-23 23:04:49 +05:30

65 lines
1.9 KiB
JavaScript

// Store the editor window ID
let editorWindowId = null;
let popupWindowId = null;
let screenshotData = null;
// Listen for extension installation or update
chrome.runtime.onInstalled.addListener(() => {
console.log('Jira Feedback Extension installed/updated');
});
// Listen for messages from popup and editor
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Background script received message:', message);
try {
switch (message.type) {
case 'editor-ready':
// Store editor window ID
editorWindowId = sender.tab.id;
if (screenshotData) {
// Send screenshot data to editor
chrome.tabs.sendMessage(editorWindowId, {
type: 'init-editor',
screenshot: screenshotData
});
}
sendResponse({ success: true });
break;
case 'init-editor':
// Store screenshot data
screenshotData = message.screenshot;
if (editorWindowId) {
// Send to editor if it's ready
chrome.tabs.sendMessage(editorWindowId, {
type: 'init-editor',
screenshot: screenshotData
});
}
sendResponse({ success: true });
break;
case 'save-screenshot':
// Broadcast edited screenshot to all extension pages
chrome.runtime.sendMessage({
type: 'screenshot-saved',
screenshot: message.screenshot
}).catch(error => {
console.log('Error broadcasting screenshot:', error);
});
sendResponse({ success: true });
break;
default:
console.log('Unknown message type:', message.type);
sendResponse({ success: false, error: 'Unknown message type' });
}
} catch (error) {
console.error('Error handling message:', error);
sendResponse({ success: false, error: error.message });
}
return true; // Keep the message channel open for async response
});