Files
brightwheel-exporter/scripts/background.js
T

52 lines
1.5 KiB
JavaScript

/**
* Brightwheel Exporter - Service Worker (Background Script)
* Manifest V3
*/
chrome.runtime.onInstalled.addListener(() => {
console.log('[Brightwheel Exporter] Extension installed.');
});
// Handle incoming messages from popup, dashboard, or content scripts
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'OPEN_DASHBOARD') {
const dashboardUrl = chrome.runtime.getURL('dashboard/dashboard.html');
// Check if dashboard is already open
chrome.tabs.query({ url: dashboardUrl }, (tabs) => {
if (tabs && tabs.length > 0) {
chrome.tabs.update(tabs[0].id, { active: true });
if (tabs[0].windowId) {
chrome.windows.update(tabs[0].windowId, { focused: true });
}
} else {
chrome.tabs.create({ url: dashboardUrl });
}
sendResponse({ success: true });
});
return true; // Keep message channel open for async response
}
if (message.action === 'DOWNLOAD_BLOB_URL') {
chrome.downloads.download({
url: message.url,
filename: message.filename,
saveAs: true
}, (downloadId) => {
if (chrome.runtime.lastError) {
sendResponse({ success: false, error: chrome.runtime.lastError.message });
} else {
sendResponse({ success: true, downloadId });
}
});
return true;
}
if (message.action === 'GET_BRIGHTWHEEL_TABS') {
chrome.tabs.query({ url: 'https://schools.mybrightwheel.com/*' }, (tabs) => {
sendResponse({ tabs: tabs || [] });
});
return true;
}
});