The saveOptionStorage(e, options) function is a helper designed to handle UI events (like change or input) and automatically persist the corresponding settings to chrome.storage.local. It also sends a chrome.runtime.sendMessage({ options: true }) to notify other parts of the extension that options have changed.
It handles several input types:
- Textareas: Splits content by newline and filters out empty lines, storing them as an array.
- Checkboxes: Stores the
checked boolean state. - Radio Buttons: When a radio button is clicked, it iterates through the provided
options array to update the storage state for the entire radio group. - Text Inputs: Trims whitespace. If a text input is cleared, the key is removed from storage.
- Custom Commands: If the target is
regexCommand or customCommand, it uses specific logic to map the input to storage keys (e.g., prefixing customCommand with the selected copyMethod).
// Example: Handling a checkbox change event
const event = { target: { id: 'myCheckbox', type: 'checkbox', checked: true } };
await saveOptionStorage(event, []);
// Example: Handling a textarea change event
const event = { target: { id: 'myList', tagName: 'TEXTAREA', value: 'item1\nitem2' } };
await saveOptionStorage(event, []);