Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(chrome-ext): add configuration options (#1461)

Elijah Potterfaf64e2abd02
diff --git a/packages/chrome-plugin/src/ProtocolClient.ts b/packages/chrome-plugin/src/ProtocolClient.ts
index 8c0f3cb21..2d9f89dee 100644
--- a/packages/chrome-plugin/src/ProtocolClient.ts
+++ b/packages/chrome-plugin/src/ProtocolClient.ts
@@ -44,6 +44,7 @@ export default class ProtocolClient {
}
public static async getDomainEnabled(domain: string): Promise<boolean> {
+ this.lintCache.clear();
return (await chrome.runtime.sendMessage({ kind: 'getDomainStatus', domain })).enabled;
}
@@ -51,9 +52,27 @@ export default class ProtocolClient {
await chrome.runtime.sendMessage({ kind: 'setDomainStatus', enabled, domain });
}
- public static async addToUserDictionary(word: string): Promise<void> {
+ public static async getDefaultEnabled(): Promise<boolean> {
+ this.lintCache.clear();
+ return (await chrome.runtime.sendMessage({ kind: 'getDefaultStatus' })).enabled;
+ }
+
+ public static async setDefaultEnabled(enabled: boolean): Promise<void> {
+ await chrome.runtime.sendMessage({ kind: 'setDefaultStatus', enabled });
+ }
+
+ public static async addToUserDictionary(words: string[]): Promise<void> {
this.lintCache.clear();
- await chrome.runtime.sendMessage({ kind: 'addToUserDictionary', word });
+ await chrome.runtime.sendMessage({ kind: 'addToUserDictionary', words });
+ }
+
+ public static async setUserDictionary(words: string[]): Promise<void> {
+ this.lintCache.clear();
+ await chrome.runtime.sendMessage({ kind: 'setUserDictionary', words });
+ }
+
+ public static async getUserDictionary(): Promise<string[]> {
+ return (await chrome.runtime.sendMessage({ kind: 'getUserDictionary' })).words;
}
public static async ignoreHash(hash: string): Promise<void> {
diff --git a/packages/chrome-plugin/src/SuggestionBox.ts b/packages/chrome-plugin/src/SuggestionBox.ts
index 349808e6a..a0b6cb1e2 100644
--- a/packages/chrome-plugin/src/SuggestionBox.ts
+++ b/packages/chrome-plugin/src/SuggestionBox.ts
@@ -53,7 +53,7 @@ function addToDictionary(box: LintBox): any {
{
className: 'harper-btn',
onclick: () => {
- ProtocolClient.addToUserDictionary(box.lint.problem_text);
+ ProtocolClient.addToUserDictionary([box.lint.problem_text]);
},
title: 'Add word to user dictionary',
'aria-label': 'Add word to user dictionary',
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index 4af4f0545..a851489a5 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -3,20 +3,24 @@ import {
type AddToUserDictionaryRequest,
type GetConfigRequest,
type GetConfigResponse,
+ type GetDefaultStatusResponse,
type GetDialectRequest,
type GetDialectResponse,
type GetDomainStatusRequest,
type GetDomainStatusResponse,
type GetLintDescriptionsRequest,
type GetLintDescriptionsResponse,
+ type GetUserDictionaryResponse,
type IgnoreLintRequest,
type LintRequest,
type LintResponse,
type Request,
type Response,
type SetConfigRequest,
+ type SetDefaultStatusRequest,
type SetDialectRequest,
type SetDomainStatusRequest,
+ type SetUserDictionaryRequest,
type UnitResponse,
createUnitResponse,
} from '../protocol';
@@ -106,6 +110,14 @@ function handleRequest(message: Request): Promise<Response> {
return handleAddToUserDictionary(message);
case 'ignoreLint':
return handleIgnoreLint(message);
+ case 'setDefaultStatus':
+ return handleSetDefaultStatus(message);
+ case 'getDefaultStatus':
+ return handleGetDefaultStatus();
+ case 'getUserDictionary':
+ return handleGetUserDictionary();
+ case 'setUserDictionary':
+ return handleSetUserDictionary(message);
}
}
@@ -146,6 +158,14 @@ async function handleIgnoreLint(req: IgnoreLintRequest): Promise<UnitResponse> {
return createUnitResponse();
}
+
+async function handleGetDefaultStatus(): Promise<GetDefaultStatusResponse> {
+ return {
+ kind: 'getDefaultStatus',
+ enabled: await enabledByDefault(),
+ };
+}
+
async function handleGetDomainStatus(
req: GetDomainStatusRequest,
): Promise<GetDomainStatusResponse> {
@@ -162,18 +182,37 @@ async function handleSetDomainStatus(req: SetDomainStatusRequest): Promise<UnitR
return createUnitResponse();
}
+async function handleSetDefaultStatus(req: SetDefaultStatusRequest): Promise<UnitResponse> {
+ await setDefaultEnable(req.enabled);
+
+ return createUnitResponse();
+}
+
async function handleGetLintDescriptions(
req: GetLintDescriptionsRequest,
): Promise<GetLintDescriptionsResponse> {
return { kind: 'getLintDescriptions', descriptions: await linter.getLintDescriptionsHTML() };
}
+async function handleSetUserDictionary(req: SetUserDictionaryRequest): Promise<UnitResponse> {
+ await resetDictionary();
+ await addToDictionary(req.words);
+
+ return createUnitResponse();
+}
+
async function handleAddToUserDictionary(req: AddToUserDictionaryRequest): Promise<UnitResponse> {
- await addToDictionary(req.word);
+ await addToDictionary(req.words);
return createUnitResponse();
}
+async function handleGetUserDictionary(): Promise<GetUserDictionaryResponse> {
+ const dict = await getUserDictionary();
+
+ return { kind: 'getUserDictionary', words: dict };
+}
+
/** Set the lint configuration inside the global `linter` and in permanent storage. */
async function setLintConfig(lintConfig: LintConfig): Promise<void> {
await linter.setLintConfig(lintConfig);
@@ -235,7 +274,9 @@ function formatDomainKey(domain: string): string {
/** Check if Harper has been enabled for a given domain. */
async function enabledForDomain(domain: string): Promise<boolean> {
- const req = await chrome.storage.local.get({ [formatDomainKey(domain)]: false });
+ const req = await chrome.storage.local.get({
+ [formatDomainKey(domain)]: await enabledByDefault(),
+ });
return req[formatDomainKey(domain)];
}
@@ -244,20 +285,38 @@ async function setDomainEnable(domain: string, status: boolean) {
await chrome.storage.local.set({ [formatDomainKey(domain)]: status });
}
+/** Set whether Harper is enabled by default. */
+async function setDefaultEnable(status: boolean) {
+ await chrome.storage.local.set({ defaultEnable: status });
+}
+
+/** Check if Harper has been enabled by default. */
+async function enabledByDefault(): Promise<boolean> {
+ const req = await chrome.storage.local.get({ defaultEnable: false });
+ return req.defaultEnable;
+}
+
/** Check whether Harper's state has been set for a given domain. */
async function isDomainSet(domain: string): Promise<boolean> {
const resp = await chrome.storage.local.get(formatDomainKey(domain));
return typeof resp[formatDomainKey(domain)] == 'boolean';
}
-/** Add a word to the persistent user dictionary. */
-async function addToDictionary(word: string): Promise<void> {
- const words = await linter.exportWords();
- words.push(word);
+/** Reset the persistent user dictionary. */
+async function resetDictionary(): Promise<void> {
+ await chrome.storage.local.set({ userDictionary: null });
+
+ initializeLinter(await linter.getDialect());
+}
+
+/** Add words to the persistent user dictionary. */
+async function addToDictionary(words: string[]): Promise<void> {
+ const exported = await linter.exportWords();
+ exported.push(...words);
await Promise.all([
- linter.importWords(words),
- chrome.storage.local.set({ userDictionary: words }),
+ linter.importWords(exported),
+ chrome.storage.local.set({ userDictionary: exported }),
]);
}
diff --git a/packages/chrome-plugin/src/options/Options.svelte b/packages/chrome-plugin/src/options/Options.svelte
index db860d89f..3b7898bd7 100644
--- a/packages/chrome-plugin/src/options/Options.svelte
+++ b/packages/chrome-plugin/src/options/Options.svelte
@@ -1,5 +1,5 @@
<script lang="ts">
-import { Button, Input, Select, Toggle } from 'flowbite-svelte';
+import { Button, Checkbox, Input, Select, Toggle } from 'flowbite-svelte';
import { Dialect, type LintConfig } from 'harper.js';
import logo from '/logo.png';
import ProtocolClient from '../ProtocolClient';
@@ -9,6 +9,8 @@ let lintDescriptions: Record<string, string> = $state({});
let searchQuery = $state('');
let searchQueryLower = $derived(searchQuery.toLowerCase());
let dialect = $state(Dialect.American);
+let defaultEnabled = $state(false);
+let userDict = $state('');
$effect(() => {
ProtocolClient.setLintConfig(lintConfig);
@@ -18,6 +20,15 @@ $effect(() => {
ProtocolClient.setDialect(dialect);
});
+$effect(() => {
+ ProtocolClient.setDefaultEnabled(defaultEnabled);
+});
+
+$effect(() => {
+ console.log('hit');
+ ProtocolClient.setUserDictionary(stringToDict(userDict));
+});
+
ProtocolClient.getLintConfig().then((l) => {
lintConfig = l;
});
@@ -30,6 +41,14 @@ ProtocolClient.getDialect().then((d) => {
dialect = d;
});
+ProtocolClient.getDefaultEnabled().then((d) => {
+ defaultEnabled = d;
+});
+
+ProtocolClient.getUserDictionary().then((d) => {
+ userDict = dictToString(d.toSorted());
+});
+
function configValueToString(value: boolean | undefined): string {
switch (value) {
case true:
@@ -54,6 +73,19 @@ function configStringToValue(str: string): boolean | undefined | null {
throw 'Fell through case';
}
+
+/** Converts the content of a text area to viable dictionary values. */
+export function stringToDict(s: string): string[] {
+ return s
+ .split('\n')
+ .map((s) => s.trim())
+ .filter((v) => v.length > 0);
+}
+
+/** Converts the content of a text area to viable dictionary values. */
+export function dictToString(values: string[]): string {
+ return values.map((v) => v.trim()).join('\n');
+}
</script>
<!-- centered wrapper with side gutters -->
@@ -79,6 +111,27 @@ function configStringToValue(str: string): boolean | undefined | null {
</Select>
</div>
</div>
+
+ <div class="space-y-5">
+ <div class="flex items-center justify-between">
+ <div class="flex flex-col">
+ <span class="font-medium">Enable on New Sites by Default</span>
+ <span class="font-light">Can make some apps behave abnormally.</span>
+ </div>
+ <input type="checkbox" bind:checked={defaultEnabled}/>
+ </div>
+ </div>
+
+ <div class="space-y-5">
+ <div class="flex items-center justify-between">
+ <div class="flex flex-col">
+ <span class="font-medium">User Dictionary</span>
+ <span class="font-light">Each word should be on its own line.</span>
+ </div>
+ <textarea bind:value={userDict} />
+ </div>
+ </div>
+
</section>
<!-- ── RULES ─────────────────────────────── -->
diff --git a/packages/chrome-plugin/src/protocol.ts b/packages/chrome-plugin/src/protocol.ts
index 2afeaabf5..202be09f8 100644
--- a/packages/chrome-plugin/src/protocol.ts
+++ b/packages/chrome-plugin/src/protocol.ts
@@ -9,9 +9,13 @@ export type Request =
| SetDialectRequest
| GetDialectRequest
| SetDomainStatusRequest
+ | SetDefaultStatusRequest
| GetDomainStatusRequest
+ | GetDefaultStatusRequest
| AddToUserDictionaryRequest
- | IgnoreLintRequest;
+ | SetUserDictionaryRequest
+ | IgnoreLintRequest
+ | GetUserDictionaryRequest;
export type Response =
| LintResponse
@@ -19,7 +23,9 @@ export type Response =
| UnitResponse
| GetLintDescriptionsResponse
| GetDialectResponse
- | GetDomainStatusResponse;
+ | GetDomainStatusResponse
+ | GetDefaultStatusResponse
+ | GetUserDictionaryResponse;
export type LintRequest = {
kind: 'lint';
@@ -80,15 +86,43 @@ export type GetDomainStatusResponse = {
enabled: boolean;
};
+export type GetDefaultStatusRequest = {
+ kind: 'getDefaultStatus';
+};
+
+export type GetDefaultStatusResponse = {
+ kind: 'getDefaultStatus';
+ enabled: boolean;
+};
+
export type SetDomainStatusRequest = {
kind: 'setDomainStatus';
domain: string;
enabled: boolean;
};
+export type SetDefaultStatusRequest = {
+ kind: 'setDefaultStatus';
+ enabled: boolean;
+};
+
export type AddToUserDictionaryRequest = {
kind: 'addToUserDictionary';
- word: string;
+ words: string[];
+};
+
+export type SetUserDictionaryRequest = {
+ kind: 'setUserDictionary';
+ words: string[];
+};
+
+export type GetUserDictionaryRequest = {
+ kind: 'getUserDictionary';
+};
+
+export type GetUserDictionaryResponse = {
+ kind: 'getUserDictionary';
+ words: string[];
};
export type IgnoreLintRequest = {