using my boilerplate yuh

This commit is contained in:
Sriram Hariharan
2023-02-22 22:51:38 -06:00
parent 21d7056aae
commit bce2717088
91 changed files with 32400 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
export default interface BrowserActionMessages {
/** make it so that clicking the browser action will open the popup.html */
enableBrowserAction: () => void;
/** make it so that clicking the browser action will respond to interactions from the content script */
disableBrowserAction: () => void;
}

View File

@@ -0,0 +1,3 @@
export default interface HotReloadingMessages {
reloadExtension: () => void;
}

View File

@@ -0,0 +1,21 @@
/**
* Messages for managing the user's open tabs list
*/
export default interface TabManagementMessages {
/**
* Opens a new tab with the given URL
* @param data The URL to open
*/
openNewTab: (data: { url: string }) => chrome.tabs.Tab;
/**
* Gets the ID of the current tab (the tab that sent the message)
* @returns The ID of the current tab
*/
getTabId: () => number;
/**
* Removes the tab with the given ID
* @param data The ID of the tab to remove
* @returns The ID of the tab that was removed
*/
removeTab: (data: { tabId: number }) => void;
}

View File

@@ -0,0 +1,6 @@
/**
* This is a type with all the message definitions that can be sent TO specific tabs
*/
export default interface TAB_MESSAGES {
reAnalyzePage: (data: { url: string }) => void;
}

View File

@@ -0,0 +1,21 @@
import { createMessenger } from 'chrome-extension-toolkit';
import TAB_MESSAGES from './TabMessages';
import BrowserActionMessages from './BrowserActionMessages';
import HotReloadingMessages from './HotReloadingMessages';
import TabManagementMessages from './TabManagementMessages';
/**
* This is a type with all the message definitions that can be sent TO the background script
*/
export type BACKGROUND_MESSAGES = BrowserActionMessages & TabManagementMessages & HotReloadingMessages;
/**
* A utility object that can be used to send type-safe messages to the background script
*/
export const bMessenger = createMessenger<BACKGROUND_MESSAGES>('background');
/**
* A utility object that can be used to send type-safe messages to specific tabs
*/
export const tabMessenger = createMessenger<TAB_MESSAGES>('tab');

View 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,
});

View 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',
}
);

25
src/shared/util/random.ts Normal file
View File

@@ -0,0 +1,25 @@
/**
* Generate a random ID
*
* @returns string of size 10 made up of random numbers and letters
* @param length the length of the ID to generate
* @example "cdtl9l88pj"
*/
export function generateRandomId(length: number = 10): string {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let result = '';
for (let i = 0; i < length; i += 1) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
/**
* Generate a random number between min and max
* @param min the minimum number
* @param max the maximum number
* @returns a random number between min and max
*/
export function rangeRandom(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

34
src/shared/util/string.ts Normal file
View File

@@ -0,0 +1,34 @@
/**
* Given a string, returns a string with the first letter capitalized.
* @input The string to capitalize.
*/
export function capitalize(input: string): string {
try {
return input.charAt(0).toUpperCase() + input.substring(1).toLowerCase();
} catch (err) {
return input;
}
}
/**
* Given a string, returns a string with the first letter capitalized.
* @param input capitalize the first letter of this string
* @returns the string with the first letter capitalized
*/
export function capitalizeFirstLetter(input: string): string {
return input.charAt(0).toUpperCase() + input.slice(1);
}
/**
* Cuts the
* @param input The string to ellipsify.
* @param length The length of the string to return.
* @returns The ellipsified string.
*/
export const ellipsify = (input: string, chars: number): string => {
let ellipisifed = input;
if (input && input.length > chars) {
ellipisifed = `${input.substring(0, chars)}...`;
}
return ellipisifed;
};

19
src/shared/util/time.ts Normal file
View File

@@ -0,0 +1,19 @@
export const MILLISECOND = 1;
export const SECOND = 1000 * MILLISECOND;
export const MINUTE = 60 * SECOND;
export const HOUR = 60 * MINUTE;
export const DAY = 24 * HOUR;
/**
*
*/
export const sleep = (milliseconds: number): Promise<void> => new Promise(resolve => setTimeout(resolve, milliseconds));
/**
* Checks to see if expired by the time first stored and the time frame that it is stored for
*
* @param time time it was stored
* @param threshold time frame it can be stored for
* @return true if expired, false if the time frame is still in range
*/
export const didExpire = (time: number, threshold: number): boolean => time + threshold <= Date.now();