Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JkYTqsvYu6AYr9yQ3Q4Nt6
26 lines
1 KiB
JavaScript
26 lines
1 KiB
JavaScript
// The extension's settings, kept in chrome.storage.sync (they follow the user's Chrome profile on every computer).
|
|
// Shared by the background service worker, the popup and the options page (ES modules). The content script is not
|
|
// a module: it asks the background for them (message "getSettings").
|
|
|
|
export const DEFAULTS = Object.freeze({
|
|
term: 'example',
|
|
color: '#ffe066',
|
|
enabled: true,
|
|
});
|
|
|
|
/** @returns {Promise<typeof DEFAULTS>} every setting, the defaults filled in */
|
|
export async function getSettings() {
|
|
return { ...DEFAULTS, ...(await chrome.storage.sync.get(DEFAULTS)) };
|
|
}
|
|
|
|
/** Saves some settings (the others are kept). */
|
|
export async function saveSettings(values) {
|
|
const clean = {};
|
|
for (const key of Object.keys(DEFAULTS)) {
|
|
if (key in values) clean[key] = values[key];
|
|
}
|
|
if (typeof clean.term === 'string') clean.term = clean.term.trim().slice(0, 100);
|
|
if (typeof clean.color === 'string' && !/^#[0-9a-f]{6}$/i.test(clean.color)) delete clean.color;
|
|
await chrome.storage.sync.set(clean);
|
|
return getSettings();
|
|
}
|