UserScriptProjectTemplate/src/utils/storage.ts
JonasPfalzgraf 88aeab8f29 feat: Enhance UserScript structure and functionality
- Updated package.json to include new scripts for development, production builds, linting, formatting, and cleaning.
- Added ESLint and Prettier for code quality and formatting.
- Refactored main application class to extend EventEmitter and manage modules.
- Introduced ExampleModule to demonstrate module structure and functionality.
- Created utility classes for DOM manipulation, event handling, and persistent storage.
- Added TypeScript definitions for UserScript environment.
- Improved TypeScript configuration with stricter checks and path aliases.
- Updated Vite configuration to handle development and production builds more effectively.
- Enhanced user script header generation to support environment-specific configurations.
2025-07-11 17:47:22 +02:00

61 lines
1.3 KiB
TypeScript

/**
* Storage utility for UserScript persistent data
*/
export class Storage {
/**
* Set a value in persistent storage
*/
static set<T>(key: string, value: T): void {
try {
const serialized = JSON.stringify(value);
GM_setValue(key, serialized);
} catch (error) {
console.error(`Failed to store value for key "${key}":`, error);
}
}
/**
* Get a value from persistent storage
*/
static get<T>(key: string, defaultValue?: T): T | undefined {
try {
const stored = GM_getValue(key);
if (stored === undefined) {
return defaultValue;
}
return JSON.parse(stored) as T;
} catch (error) {
console.error(`Failed to retrieve value for key "${key}":`, error);
return defaultValue;
}
}
/**
* Remove a value from persistent storage
*/
static remove(key: string): void {
GM_deleteValue(key);
}
/**
* Check if a key exists in storage
*/
static has(key: string): boolean {
return GM_getValue(key) !== undefined;
}
/**
* Get all keys from storage
*/
static getAllKeys(): string[] {
return GM_listValues();
}
/**
* Clear all storage (use with caution!)
*/
static clear(): void {
const keys = GM_listValues();
keys.forEach(key => GM_deleteValue(key));
}
}