Skip to content

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

feat(chrome-ext): add support for more sites (#1255)

Elijah Potter0f62deb924cf
diff --git a/packages/chrome-plugin/src/Highlights.ts b/packages/chrome-plugin/src/Highlights.ts
index 9be8ec967..393cb1193 100644
--- a/packages/chrome-plugin/src/Highlights.ts
+++ b/packages/chrome-plugin/src/Highlights.ts
@@ -109,28 +109,20 @@ export default class Highlights {
return el.parentElement.parentElement;
}
- const scr = getShredditComposerRoot(el);
-
- if (scr != null) {
- return scr.parentElement;
- }
-
- const qr = getQuillJsRoot(el);
-
- if (qr != null) {
- return qr.parentElement;
- }
-
- const lexicalRoot = getLexicalRoot(el);
-
- if (lexicalRoot != null) {
- return lexicalRoot.parentElement;
- }
-
- const trixRoot = getTrixRoot(el);
-
- if (trixRoot != null) {
- return trixRoot.parentElement;
+ const queries = [
+ getNotionRoot,
+ getMediumRoot,
+ getShredditComposerRoot,
+ getQuillJsRoot,
+ getLexicalRoot,
+ getTrixRoot,
+ ];
+
+ for (const query of queries) {
+ const root = query(el);
+ if (root != null) {
+ return root.parentElement;
+ }
}
return el.parentElement;
@@ -194,6 +186,21 @@ function getQuillJsRoot(el: HTMLElement): HTMLElement | null {
return findAncestor(el, (node: HTMLElement) => node.classList.contains('ql-container'));
}
+/** Determines if a given node is a child of a Medium.com editor instance.
+ * If so, returns the root node of that instance. */
+function getMediumRoot(el: HTMLElement): HTMLElement | null {
+ return findAncestor(
+ el,
+ (node: HTMLElement) => node.nodeName == 'MAIN' && location.hostname == 'medium.com',
+ );
+}
+
+/** Determines if a given node is a child of a Notion editor instance.
+ * If so, returns the root node of that instance. */
+function getNotionRoot(el: HTMLElement): HTMLElement | null {
+ return document.getElementById('notion-app');
+}
+
/**
* Determines whether a given element would form the containing block
* for a descendant with `position: fixed`, based on CSS transforms,
diff --git a/packages/chrome-plugin/src/PopupHandler.ts b/packages/chrome-plugin/src/PopupHandler.ts
index d2aa16a3c..e1bdc48eb 100644
--- a/packages/chrome-plugin/src/PopupHandler.ts
+++ b/packages/chrome-plugin/src/PopupHandler.ts
@@ -21,10 +21,6 @@ export default class PopupHandler {
}
private onPointerDown(e: PointerEvent) {
- console.log('pointerdown');
- console.log([e.x, e.y]);
- console.log(this.currentLintBoxes);
-
for (let i = 0; i < this.currentLintBoxes.length; i++) {
const box = this.currentLintBoxes[i];
diff --git a/packages/chrome-plugin/src/ProtocolClient.ts b/packages/chrome-plugin/src/ProtocolClient.ts
index 4ba7a4a24..c54139015 100644
--- a/packages/chrome-plugin/src/ProtocolClient.ts
+++ b/packages/chrome-plugin/src/ProtocolClient.ts
@@ -2,10 +2,23 @@ import type { Dialect, LintConfig } from 'harper.js';
import { LRUCache } from 'lru-cache';
import type { UnpackedLint } from './unpackLint';
-/** A wrapper around Chrome's messaging protocol for communicating with the background worker. */
export default class ProtocolClient {
+ private static readonly lintCache = new LRUCache<string, UnpackedLint[]>({
+ max: 500,
+ ttl: 5_000,
+ });
+
+ private static cacheKey(text: string, domain: string): string {
+ return `${domain}:${text}`;
+ }
+
public static async lint(text: string, domain: string): Promise<UnpackedLint[]> {
- return (await chrome.runtime.sendMessage({ kind: 'lint', text, domain })).lints;
+ const key = this.cacheKey(text, domain);
+ const cached = this.lintCache.get(key);
+ if (cached) return cached;
+ const resp = await chrome.runtime.sendMessage({ kind: 'lint', text, domain });
+ this.lintCache.set(key, resp.lints);
+ return resp.lints;
}
public static async getLintConfig(): Promise<LintConfig> {
@@ -13,6 +26,7 @@ export default class ProtocolClient {
}
public static async setLintConfig(lintConfig: LintConfig): Promise<void> {
+ this.lintCache.clear();
await chrome.runtime.sendMessage({ kind: 'setConfig', config: lintConfig });
}
@@ -29,9 +43,7 @@ export default class ProtocolClient {
}
public static async getDomainEnabled(domain: string): Promise<boolean> {
- const resp = await chrome.runtime.sendMessage({ kind: 'getDomainStatus', domain });
-
- return resp.enabled;
+ return (await chrome.runtime.sendMessage({ kind: 'getDomainStatus', domain })).enabled;
}
public static async setDomainEnabled(domain: string, enabled: boolean): Promise<void> {
@@ -39,6 +51,7 @@ export default class ProtocolClient {
}
public static async addToUserDictionary(word: string): Promise<void> {
+ this.lintCache.clear();
await chrome.runtime.sendMessage({ kind: 'addToUserDictionary', word });
}
}
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index ec0901ae0..73d43fea4 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -57,6 +57,10 @@ async function enableDefaultDomains() {
'pootlewriter.com',
'www.tumblr.com',
'dayone.me',
+ 'medium.com',
+ 'x.com',
+ 'www.notion.so',
+ 'hashnode.com',
];
for (const item of defaultEnabledDomains) {
diff --git a/packages/chrome-plugin/src/contentScript/index.ts b/packages/chrome-plugin/src/contentScript/index.ts
index b1b78830e..7ea330f1c 100644
--- a/packages/chrome-plugin/src/contentScript/index.ts
+++ b/packages/chrome-plugin/src/contentScript/index.ts
@@ -22,6 +22,10 @@ function scan() {
const leafs = leafNodes(this);
for (const leaf of leafs) {
+ if (leaf.parentElement?.closest('[contenteditable="false"],[disabled],[readonly]') != null) {
+ continue;
+ }
+
fw.addTarget(leaf as HTMLElement);
}
});