From cb5f88f5fe81cd68c832f29f0084236c150c5c10 Mon Sep 17 00:00:00 2001 From: jonathankap Date: Wed, 25 Sep 2024 09:20:06 -0700 Subject: [PATCH] Reactor cleanup (#191) --- packages/reactor/index.ts | 4 ++-- packages/reactor/interfaces.ts | 2 +- packages/reactor/main.ts | 6 ++--- packages/reactor/modifications.ts | 24 +++++++++---------- .../reactor/modifications/adjacentHTML.ts | 2 +- packages/reactor/modifications/highlight.ts | 2 +- packages/reactor/modifications/noop.ts | 2 +- packages/reactor/modifications/remove.ts | 2 +- packages/reactor/modifications/replace.ts | 2 +- packages/reactor/modifications/replaceAll.ts | 6 ++--- packages/reactor/modifications/swapImage.ts | 2 +- packages/reactor/modifications/toast.ts | 2 +- packages/reactor/mutationObserver.ts | 10 ++++---- packages/reactor/reactor.ts | 8 +++---- packages/reactor/tests/modifications.test.ts | 2 +- packages/reactor/tests/mutation.test.ts | 2 +- packages/reactor/tsconfig.json | 4 +++- packages/reactor/utils.ts | 2 +- 18 files changed, 43 insertions(+), 41 deletions(-) diff --git a/packages/reactor/index.ts b/packages/reactor/index.ts index 2b4417ed..7a783141 100644 --- a/packages/reactor/index.ts +++ b/packages/reactor/index.ts @@ -4,8 +4,8 @@ export type { Highlighter, Modification, ModificationRequest, -} from "./interfaces"; +} from "./interfaces.js"; -import Reactor from "./reactor"; +import Reactor from "./reactor.js"; export { Reactor }; \ No newline at end of file diff --git a/packages/reactor/interfaces.ts b/packages/reactor/interfaces.ts index 730b1a5b..b0a0f6dd 100644 --- a/packages/reactor/interfaces.ts +++ b/packages/reactor/interfaces.ts @@ -1,4 +1,4 @@ -import { generateRandomString } from "./utils"; +import { generateRandomString } from "./utils.js"; export type Modification = { selector?: string; diff --git a/packages/reactor/main.ts b/packages/reactor/main.ts index 2d7a05e6..6a81ad64 100644 --- a/packages/reactor/main.ts +++ b/packages/reactor/main.ts @@ -2,9 +2,9 @@ import type { AppliedModifications, DomJsonExportNode, ModificationRequest, -} from "./interfaces"; -import { generateModifications } from "./modifications"; -import { parseRequest } from "./utils"; +} from "./interfaces.js"; +import { generateModifications } from "./modifications.js"; +import { parseRequest } from "./utils.js"; export async function modifyHtml( htmlString: string, diff --git a/packages/reactor/modifications.ts b/packages/reactor/modifications.ts index dcaaf5db..d94bc429 100644 --- a/packages/reactor/modifications.ts +++ b/packages/reactor/modifications.ts @@ -1,11 +1,11 @@ -import { AdjacentHTMLModification } from "./modifications/adjacentHTML"; -import { HighlightModification } from "./modifications/highlight"; -import { NoopModification } from "./modifications/noop"; -import { RemoveModification } from "./modifications/remove"; -import { ReplaceModification } from "./modifications/replace"; -import { ReplaceAllModification } from "./modifications/replaceAll"; -import { SwapImageModification } from "./modifications/swapImage"; -import { ToastModification } from "./modifications/toast"; +import { AdjacentHTMLModification } from "./modifications/adjacentHTML.js"; +import { HighlightModification } from "./modifications/highlight.js"; +import { NoopModification } from "./modifications/noop.js"; +import { RemoveModification } from "./modifications/remove.js"; +import { ReplaceModification } from "./modifications/replace.js"; +import { ReplaceAllModification } from "./modifications/replaceAll.js"; +import { SwapImageModification } from "./modifications/swapImage.js"; +import { ToastModification } from "./modifications/toast.js"; import type { AppliableModification, @@ -13,7 +13,7 @@ import type { Highlighter, Modification, ModificationRequest, -} from "./interfaces"; +} from "./interfaces.js"; export class AppliedModificationsImpl implements AppliedModifications { modificationRequest: ModificationRequest; @@ -70,9 +70,9 @@ export async function generateModifications( // construct a new NodeListOf from items found by the xpath elements = []; if (!mod.xpath.startsWith("//html")) { - mod.xpath = `//html/${mod.xpath}`; + mod.xpath = `//html${mod.xpath}`; } - const xpath = document.evaluate( + const xpath = window.document.evaluate( mod.xpath, doc, null, @@ -201,7 +201,7 @@ export function matchesSelector(element: Element, mod: Modification): boolean { if (mod.selector) { return element.matches(mod.selector); } else if (mod.xpath) { - const xpathResult = document.evaluate(mod.xpath, document, null, XPathResult.BOOLEAN_TYPE, null); + const xpathResult = window.document.evaluate(mod.xpath, window.document, null, XPathResult.BOOLEAN_TYPE, null); return xpathResult.booleanValue; } diff --git a/packages/reactor/modifications/adjacentHTML.ts b/packages/reactor/modifications/adjacentHTML.ts index 15f2d591..81b2dfcd 100644 --- a/packages/reactor/modifications/adjacentHTML.ts +++ b/packages/reactor/modifications/adjacentHTML.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; export class AdjacentHTMLModification extends AppliableModification { elementId: string; diff --git a/packages/reactor/modifications/highlight.ts b/packages/reactor/modifications/highlight.ts index 7c0708b4..cf5ba6b6 100644 --- a/packages/reactor/modifications/highlight.ts +++ b/packages/reactor/modifications/highlight.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; import * as cssSelector from "css-selector-generator"; export class HighlightModification extends AppliableModification { diff --git a/packages/reactor/modifications/noop.ts b/packages/reactor/modifications/noop.ts index 6c142fec..0323969b 100644 --- a/packages/reactor/modifications/noop.ts +++ b/packages/reactor/modifications/noop.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; export class NoopModification extends AppliableModification { action: string; diff --git a/packages/reactor/modifications/remove.ts b/packages/reactor/modifications/remove.ts index a6682368..0aa017a6 100644 --- a/packages/reactor/modifications/remove.ts +++ b/packages/reactor/modifications/remove.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; import * as cssSelector from "css-selector-generator"; export class RemoveModification extends AppliableModification { diff --git a/packages/reactor/modifications/replace.ts b/packages/reactor/modifications/replace.ts index ff7f6f36..5e8aa56e 100644 --- a/packages/reactor/modifications/replace.ts +++ b/packages/reactor/modifications/replace.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; import * as cssSelector from "css-selector-generator"; export class ReplaceModification extends AppliableModification { diff --git a/packages/reactor/modifications/replaceAll.ts b/packages/reactor/modifications/replaceAll.ts index f3c4aa59..b6276292 100644 --- a/packages/reactor/modifications/replaceAll.ts +++ b/packages/reactor/modifications/replaceAll.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; export class ReplaceAllModification extends AppliableModification { element: Element; @@ -106,7 +106,7 @@ function walkTree( const changeNodes: Node[] = []; const changes: TreeChange[] = []; - const treeWalker = document.createTreeWalker( + const treeWalker = window.document.createTreeWalker( rootElement, NodeFilter.SHOW_TEXT, (node) => { @@ -195,7 +195,7 @@ function replaceText( for (let i = 0; i < split.length; i++) { if (typeof split[i] !== "undefined") { - const textNode = document.createTextNode(split[i] || ""); + const textNode = window.document.createTextNode(split[i] || ""); parentElement.insertBefore(textNode, nextSibling); if (i % 2 !== 0) { diff --git a/packages/reactor/modifications/swapImage.ts b/packages/reactor/modifications/swapImage.ts index 713a3742..f1f392bb 100644 --- a/packages/reactor/modifications/swapImage.ts +++ b/packages/reactor/modifications/swapImage.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; import * as cssSelector from "css-selector-generator"; export class SwapImageModification extends AppliableModification { diff --git a/packages/reactor/modifications/toast.ts b/packages/reactor/modifications/toast.ts index 58702b80..412bc607 100644 --- a/packages/reactor/modifications/toast.ts +++ b/packages/reactor/modifications/toast.ts @@ -1,4 +1,4 @@ -import { AppliableModification } from "../interfaces"; +import { AppliableModification } from "../interfaces.js"; export class ToastModification extends AppliableModification { message: string; diff --git a/packages/reactor/mutationObserver.ts b/packages/reactor/mutationObserver.ts index e3373cf5..7b31844b 100644 --- a/packages/reactor/mutationObserver.ts +++ b/packages/reactor/mutationObserver.ts @@ -1,6 +1,6 @@ -import Reactor from "./reactor"; -import { AppliedModificationsImpl } from "./modifications"; -import { applyModification, matchesSelector } from "./modifications"; +import Reactor from "./reactor.js"; +import { AppliedModificationsImpl } from "./modifications.js"; +import { applyModification, matchesSelector } from "./modifications.js"; export class ReactorMutationObserver { private reactor: Reactor; @@ -42,7 +42,7 @@ export class ReactorMutationObserver { } walkAddedElements(element: Element) { - const treeWalker = document.createTreeWalker( + const treeWalker = window.document.createTreeWalker( element, NodeFilter.SHOW_ELEMENT, null @@ -68,7 +68,7 @@ export class ReactorMutationObserver { } walkRemovedElements(element: Element) { - const treeWalker = document.createTreeWalker( + const treeWalker = window.document.createTreeWalker( element, NodeFilter.SHOW_ELEMENT, null diff --git a/packages/reactor/reactor.ts b/packages/reactor/reactor.ts index b26661ff..d31afcd3 100644 --- a/packages/reactor/reactor.ts +++ b/packages/reactor/reactor.ts @@ -3,13 +3,13 @@ import type { DomJsonExportNode, Highlighter, ModificationRequest, -} from "./interfaces"; -import { htmlElementToJson } from "./main"; +} from "./interfaces.js"; +import { htmlElementToJson } from "./main.js"; import { AppliedModificationsImpl, generateModifications, -} from "./modifications"; -import { ReactorMutationObserver } from "./mutationObserver"; +} from "./modifications.js"; +import { ReactorMutationObserver } from "./mutationObserver.js"; /** * Reactor applied modifications to the current page. Modifications diff --git a/packages/reactor/tests/modifications.test.ts b/packages/reactor/tests/modifications.test.ts index 62cfc28e..f42e0d8e 100644 --- a/packages/reactor/tests/modifications.test.ts +++ b/packages/reactor/tests/modifications.test.ts @@ -11,7 +11,7 @@ describe("Utils", () => { // Vitest beforeEach function for setup beforeEach(() => { - doc = document.implementation.createHTMLDocument("Test Document"); + doc = window.document.implementation.createHTMLDocument("Test Document"); }); describe("applyModification", () => { diff --git a/packages/reactor/tests/mutation.test.ts b/packages/reactor/tests/mutation.test.ts index f6a66789..3a065c24 100644 --- a/packages/reactor/tests/mutation.test.ts +++ b/packages/reactor/tests/mutation.test.ts @@ -11,7 +11,7 @@ describe("test mutation listeners", {}, () => { // Vitest beforeEach function for setup beforeEach(() => { - doc = document.implementation.createHTMLDocument("Test Document"); + doc = window.document.implementation.createHTMLDocument("Test Document"); reactor = new Reactor(); reactor.attach(doc, { diff --git a/packages/reactor/tsconfig.json b/packages/reactor/tsconfig.json index 001bfa82..4b213349 100644 --- a/packages/reactor/tsconfig.json +++ b/packages/reactor/tsconfig.json @@ -7,7 +7,9 @@ "module": "ESNext", "target": "ESNext", "esModuleInterop": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true }, "include": ["index.ts", "receivers", "utils", "commands"], "exclude": ["node_modules", "dist", "test"] diff --git a/packages/reactor/utils.ts b/packages/reactor/utils.ts index 4eba2125..dc79270c 100644 --- a/packages/reactor/utils.ts +++ b/packages/reactor/utils.ts @@ -1,6 +1,6 @@ import type { ModificationRequest, -} from "./interfaces"; +} from "./interfaces.js"; export function parseRequest(userRequest: string): ModificationRequest { try {