* feat(build): inline chrome-extension-toolkit fix: tsconfig docs: add chrome-extension-toolkit README.md chore: update imports fix: stores fix: chrome-extension-toolkit ForegroundMessenger fix: calendarBackgroundHandler fix: format and lint fix: path alias fix: add jsdom env and fix imports Co-authored-by: Sriram Hariharan <sghsri@gmail.com> * build: vite storybook config crx toolkit line --------- Co-authored-by: Sriram Hariharan <sghsri@gmail.com> Co-authored-by: Derek <derex1987@gmail.com>
72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
import { MessageListener } from '@chrome-extension-toolkit';
|
|
import type { BACKGROUND_MESSAGES } from '@shared/messages';
|
|
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
|
|
import updateBadgeText from '@shared/util/updateBadgeText';
|
|
|
|
import onInstall from './events/onInstall';
|
|
import onServiceWorkerAlive from './events/onServiceWorkerAlive';
|
|
import onUpdate from './events/onUpdate';
|
|
import browserActionHandler from './handler/browserActionHandler';
|
|
import calendarBackgroundHandler from './handler/calendarBackgroundHandler';
|
|
import CESHandler from './handler/CESHandler';
|
|
import gitHubStatsHandler from './handler/gitHubStatsHandler';
|
|
import tabManagementHandler from './handler/tabManagementHandler';
|
|
import userScheduleHandler from './handler/userScheduleHandler';
|
|
|
|
onServiceWorkerAlive();
|
|
|
|
/**
|
|
* will be triggered on either install or update
|
|
* (will also be triggered on a user's sync'd browsers (on other devices)))
|
|
*/
|
|
chrome.runtime.onInstalled.addListener(details => {
|
|
switch (details.reason) {
|
|
case 'install':
|
|
onInstall();
|
|
break;
|
|
case 'update':
|
|
onUpdate();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
|
|
// migration/login logic
|
|
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
|
|
// console.log(changeInfo);
|
|
if (changeInfo.url === 'https://utdirect.utexas.edu/apps/registrar/course_schedule/utrp_login/') {
|
|
function openPopupAction() {
|
|
chrome.tabs.onActivated.removeListener(openPopupAction);
|
|
chrome.action.openPopup();
|
|
}
|
|
|
|
chrome.tabs.onActivated.addListener(openPopupAction);
|
|
await chrome.tabs.remove(tabId);
|
|
}
|
|
});
|
|
|
|
// initialize the message listener that will listen for messages from the content script
|
|
const messageListener = new MessageListener<BACKGROUND_MESSAGES>({
|
|
...browserActionHandler,
|
|
...tabManagementHandler,
|
|
...userScheduleHandler,
|
|
...CESHandler,
|
|
...calendarBackgroundHandler,
|
|
...gitHubStatsHandler,
|
|
});
|
|
|
|
messageListener.listen();
|
|
|
|
UserScheduleStore.subscribe('schedules', async schedules => {
|
|
const index = await UserScheduleStore.get('activeIndex');
|
|
const numCourses = schedules.newValue[index]?.courses?.length;
|
|
updateBadgeText(numCourses || 0);
|
|
});
|
|
|
|
UserScheduleStore.subscribe('activeIndex', async ({ newValue }) => {
|
|
const schedules = await UserScheduleStore.get('schedules');
|
|
const numCourses = schedules[newValue]?.courses?.length;
|
|
updateBadgeText(numCourses || 0);
|
|
});
|