Skip to content

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

fix(chrome-ext): edge cases (#1674)

Elijah Potter90da24c5af1e
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index ec8ae7c39..c480c195b 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -47,6 +47,7 @@ getDialect().then(setDialect);
async function enableDefaultDomains() {
const defaultEnabledDomains = [
+ 'old.reddit.com',
'chatgpt.com',
'www.perplexity.ai',
'textarea.online',
diff --git a/packages/chrome-plugin/src/computeLintBoxes.ts b/packages/chrome-plugin/src/computeLintBoxes.ts
index 8de174981..1aea7e233 100644
--- a/packages/chrome-plugin/src/computeLintBoxes.ts
+++ b/packages/chrome-plugin/src/computeLintBoxes.ts
@@ -1,13 +1,7 @@
import type { Span } from 'harper.js';
-import {
- domRectToBox,
- type IgnorableLintBox,
- isBottomEdgeInBox,
- type LintBox,
- shrinkBoxToFit,
-} from './Box';
+import { domRectToBox, type IgnorableLintBox, isBottomEdgeInBox, shrinkBoxToFit } from './Box';
import { getRangeForTextSpan } from './domUtils';
-import { getLexicalRoot, getSlateRoot } from './editorUtils';
+import { getLexicalEditable, getSlateRoot } from './editorUtils';
import ProtocolClient from './ProtocolClient';
import TextFieldRange from './TextFieldRange';
import { applySuggestion, type UnpackedLint, type UnpackedSuggestion } from './unpackLint';
@@ -81,7 +75,7 @@ export default function computeLintBoxes(el: HTMLElement, lint: UnpackedLint): I
function replaceValue(el: HTMLElement, value: string) {
const slateRoot = getSlateRoot(el);
- const lexicalRoot = getLexicalRoot(el);
+ const lexicalRoot = getLexicalEditable(el);
if (isFormEl(el)) {
el.dispatchEvent(new InputEvent('beforeinput', { bubbles: true, data: value }));
diff --git a/packages/chrome-plugin/src/editorUtils.ts b/packages/chrome-plugin/src/editorUtils.ts
index fa19412a9..43f9342f0 100644
--- a/packages/chrome-plugin/src/editorUtils.ts
+++ b/packages/chrome-plugin/src/editorUtils.ts
@@ -18,6 +18,25 @@ export function findAncestor(
return null;
}
+export function findChild(
+ el: HTMLElement,
+ predicate: (el: HTMLElement) => boolean,
+): HTMLElement | null {
+ const queue: HTMLElement[] = Array.from(el.children) as HTMLElement[];
+
+ while (queue.length > 0) {
+ const node = queue.shift() as HTMLElement;
+
+ if (predicate(node)) {
+ return node;
+ }
+
+ queue.push(...(Array.from(node.children) as HTMLElement[]));
+ }
+
+ return null;
+}
+
/** Determines if a given node is a child of a P2 editor instance.
* If so, returns the root node of that instance. */
export function getP2Root(el: HTMLElement): HTMLElement | null {
@@ -41,6 +60,16 @@ export function getLexicalRoot(el: HTMLElement): HTMLElement | null {
);
}
+export function getLexicalEditable(el: HTMLElement): HTMLElement | null {
+ const lexical = getLexicalRoot(el);
+
+ if (lexical == null) {
+ return null;
+ }
+
+ return findChild(lexical, (node: HTMLElement) => node.getAttribute('contenteditable') == 'true');
+}
+
/** Determines if a given node is a child of a Slate.js editor instance.
* If so, returns the root node of that instance. */
export function getSlateRoot(el: HTMLElement): HTMLElement | null {