added deviceId, ExtensionStore, working on CoursePopup
This commit is contained in:
@@ -5,7 +5,7 @@ import onInstall from './events/onInstall';
|
||||
import onNewChromeSession from './events/onNewChromeSession';
|
||||
import onServiceWorkerAlive from './events/onServiceWorkerAlive';
|
||||
import onUpdate from './events/onUpdate';
|
||||
import { sessionStore } from '../shared/storage/sessionStore';
|
||||
import { SessionStore } from './storage/SessionStore';
|
||||
import browserActionHandler from './handler/browserActionHandler';
|
||||
import hotReloadingHandler from './handler/hotReloadingHandler';
|
||||
import tabManagementHandler from './handler/tabManagementHandler';
|
||||
@@ -38,9 +38,9 @@ const messageListener = new MessageListener<BACKGROUND_MESSAGES>({
|
||||
|
||||
messageListener.listen();
|
||||
|
||||
sessionStore.getChromeSessionId().then(async chromeSessionId => {
|
||||
SessionStore.getChromeSessionId().then(async chromeSessionId => {
|
||||
if (!chromeSessionId) {
|
||||
await sessionStore.setChromeSessionId(generateRandomId(10));
|
||||
await SessionStore.setChromeSessionId(generateRandomId(10));
|
||||
onNewChromeSession();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,26 +1,9 @@
|
||||
import { SECOND } from 'src/shared/util/time';
|
||||
import { ExtensionStore } from '../storage/ExtensionStore';
|
||||
|
||||
/**
|
||||
* Called when the extension is first installed or synced onto a new machine
|
||||
*/
|
||||
export default async function onInstall() {
|
||||
// set the uninstall url
|
||||
chrome.runtime.setUninstallURL('https://www.google.com');
|
||||
logOnInstallEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
* making sure we are not sending duplicate install event for users that have synced browsers
|
||||
* sync storage get's cleared on browser uninstall, so re-installing the browser will trigger the event
|
||||
*/
|
||||
function logOnInstallEvent() {
|
||||
setTimeout(async () => {
|
||||
const manifest = chrome.runtime.getManifest();
|
||||
const INSTALL_KEY = `${manifest.short_name}-installed`;
|
||||
const storage = await chrome.storage.sync.get(INSTALL_KEY);
|
||||
if (!storage[INSTALL_KEY]) {
|
||||
// TODO: send install event
|
||||
await chrome.storage.sync.set({ [INSTALL_KEY]: true });
|
||||
}
|
||||
}, 5 * SECOND);
|
||||
await ExtensionStore.setVersion(chrome.runtime.getManifest().version);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import { hotReloadTab } from 'src/background/util/hotReloadTab';
|
||||
import { ExtensionStore } from '../storage/ExtensionStore';
|
||||
|
||||
/**
|
||||
* Called when the extension is updated (or when the extension is reloaded in development mode)
|
||||
*/
|
||||
export default function onUpdate() {
|
||||
export default async function onUpdate() {
|
||||
await Promise.all([
|
||||
ExtensionStore.setLastUpdate(Date.now()),
|
||||
ExtensionStore.setVersion(chrome.runtime.getManifest().version),
|
||||
]);
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
hotReloadTab();
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import HotReloadingMessages from 'src/shared/messages/HotReloadingMessages';
|
||||
import { MessageHandler } from 'chrome-extension-toolkit';
|
||||
import { devStore } from 'src/shared/storage/devStore';
|
||||
import { DevStore } from 'src/background/storage/DevStore';
|
||||
|
||||
const hotReloadingHandler: MessageHandler<HotReloadingMessages> = {
|
||||
async reloadExtension({ sendResponse }) {
|
||||
const isExtensionReloading = await devStore.getIsExtensionReloading();
|
||||
const isExtensionReloading = await DevStore.getIsExtensionReloading();
|
||||
if (!isExtensionReloading) return sendResponse();
|
||||
|
||||
const isTabReloading = await devStore.getIsExtensionReloading();
|
||||
const isTabReloading = await DevStore.getIsExtensionReloading();
|
||||
if (isTabReloading) {
|
||||
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
const tabToReload = tabs[0];
|
||||
|
||||
await devStore.setReloadTabId(tabToReload?.id);
|
||||
await DevStore.setReloadTabId(tabToReload?.id);
|
||||
}
|
||||
chrome.runtime.reload();
|
||||
},
|
||||
|
||||
25
src/background/storage/DevStore.ts
Normal file
25
src/background/storage/DevStore.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { createStore } from 'chrome-extension-toolkit';
|
||||
|
||||
/**
|
||||
* A store that is used to store data that is only relevant during development
|
||||
*/
|
||||
interface IDevStore {
|
||||
/** the tabId for the debug tab */
|
||||
debugTabId?: number;
|
||||
/** whether the debug tab is visible */
|
||||
wasDebugTabVisible?: boolean;
|
||||
/** whether we should enable extension reloading */
|
||||
isExtensionReloading?: boolean;
|
||||
/** whether we should enable tab reloading */
|
||||
isTabReloading?: boolean;
|
||||
/** The id of the tab that we want to reload (after the extension reloads itself ) */
|
||||
reloadTabId?: number;
|
||||
}
|
||||
|
||||
export const DevStore = createStore<IDevStore>('DEV_STORE', {
|
||||
debugTabId: undefined,
|
||||
isTabReloading: true,
|
||||
wasDebugTabVisible: false,
|
||||
isExtensionReloading: true,
|
||||
reloadTabId: undefined,
|
||||
});
|
||||
31
src/background/storage/ExtensionStore.ts
Normal file
31
src/background/storage/ExtensionStore.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { createStore } from 'chrome-extension-toolkit';
|
||||
|
||||
/**
|
||||
* A store that is used for storing user options
|
||||
*/
|
||||
interface IExtensionStore {
|
||||
/** These values are cached in storage, so that we can know the previous version that the extension was before the current update. Is only used for onUpdate */
|
||||
version: string;
|
||||
/** When was the last update */
|
||||
lastUpdate: number;
|
||||
/** A unique identifier generated for the current user in lieu of a userId */
|
||||
deviceId: string;
|
||||
}
|
||||
|
||||
const base = createStore<IExtensionStore>('EXTENSION_STORE', {
|
||||
version: chrome.runtime.getManifest().version,
|
||||
lastUpdate: Date.now(),
|
||||
deviceId: '',
|
||||
});
|
||||
|
||||
export const ExtensionStore = base.modify({
|
||||
async getDeviceId() {
|
||||
const deviceId = await base.getDeviceId();
|
||||
if (deviceId) {
|
||||
return deviceId;
|
||||
}
|
||||
const newDeviceId = uuidv4();
|
||||
return newDeviceId;
|
||||
},
|
||||
});
|
||||
16
src/background/storage/OptionsStore.ts
Normal file
16
src/background/storage/OptionsStore.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { createStore } from 'chrome-extension-toolkit';
|
||||
|
||||
/**
|
||||
* A store that is used for storing user options
|
||||
*/
|
||||
interface IOptionsStore {
|
||||
/** whether we should automatically highlight conflicts on the course schedule page */
|
||||
shouldHighlightConflicts: boolean;
|
||||
/** whether we should automatically scroll to load more courses on the course schedule page (without having to click next) */
|
||||
shouldScrollToLoad: boolean;
|
||||
}
|
||||
|
||||
export const OptionsStore = createStore<IOptionsStore>('OPTIONS_STORE', {
|
||||
shouldHighlightConflicts: true,
|
||||
shouldScrollToLoad: true,
|
||||
});
|
||||
15
src/background/storage/SessionStore.ts
Normal file
15
src/background/storage/SessionStore.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { createStore, Store } from 'chrome-extension-toolkit';
|
||||
|
||||
interface ISessionStore {
|
||||
chromeSessionId?: string;
|
||||
}
|
||||
|
||||
export const SessionStore = createStore<ISessionStore>(
|
||||
'SESSION_STORE',
|
||||
{
|
||||
chromeSessionId: undefined,
|
||||
},
|
||||
{
|
||||
area: 'session',
|
||||
}
|
||||
);
|
||||
@@ -1,4 +1,4 @@
|
||||
import { devStore } from 'src/shared/storage/devStore';
|
||||
import { DevStore } from 'src/background/storage/DevStore';
|
||||
|
||||
/**
|
||||
* A list of websites that we don't want to reload when the extension reloads (becuase it'd be hella annoying lmao)
|
||||
@@ -24,7 +24,7 @@ const HOT_RELOADING_WHITELIST = [
|
||||
* @returns a promise that resolves when the tab is reloaded
|
||||
*/
|
||||
export async function hotReloadTab(): Promise<void> {
|
||||
const { getIsTabReloading, getReloadTabId } = devStore;
|
||||
const { getIsTabReloading, getReloadTabId } = DevStore;
|
||||
|
||||
const [isTabReloading, reloadTabId] = await Promise.all([getIsTabReloading(), getReloadTabId()]);
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { devStore } from 'src/shared/storage/devStore';
|
||||
import { DevStore } from 'src/background/storage/DevStore';
|
||||
|
||||
/**
|
||||
* Open the debug tab as the first tab
|
||||
*/
|
||||
export async function openDebugTab() {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const debugTabId = await devStore.getDebugTabId();
|
||||
const debugTabId = await DevStore.getDebugTabId();
|
||||
|
||||
const isAlreadyOpen = await (await chrome.tabs.query({})).some(tab => tab.id === debugTabId);
|
||||
if (isAlreadyOpen) return;
|
||||
|
||||
const wasVisible = await devStore.getWasDebugTabVisible();
|
||||
const wasVisible = await DevStore.getWasDebugTabVisible();
|
||||
|
||||
const tab = await chrome.tabs.create({
|
||||
url: chrome.runtime.getURL('debug.html'),
|
||||
@@ -19,6 +19,6 @@ export async function openDebugTab() {
|
||||
index: 0,
|
||||
});
|
||||
|
||||
await devStore.setDebugTabId(tab.id);
|
||||
await DevStore.setDebugTabId(tab.id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user