fix: some warnings, refactor: changed the donation look, moved it up

This commit is contained in:
joshpatra committed 2026-04-09 16:39:13 -04:00
1 parent 41d89e0993
commit 0b6bc5d0a0
10 files changed
+388 -65

No files matched your search

+64 -8
View File
@@ -1,7 +1,9 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { JSDOM } from "jsdom";
import { vi } from "vitest";
import { applyJSDOMWindow } from "./jsdom-globals.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -11,19 +13,73 @@ function readRepoFile(relPath) {
return fs.readFileSync(path.join(repoRoot, relPath), "utf8");
}
/**
* Parse HTML into a fresh JSDOM document so tests can reload scripts without
* top-level `const` redeclaration errors (avoids document.write).
*/
export function loadHtmlString(html) {
const dom = new JSDOM(html, {
url: "https://example.org/",
pretendToBeVisual: true,
runScripts: "dangerously"
});
applyJSDOMWindow(dom.window);
}
export function loadHtml(relPath) {
document.open();
document.write(readRepoFile(relPath));
document.close();
loadHtmlString(readRepoFile(relPath));
}
const WINDOW_GLOBAL_SKIP = new Set([
"alert",
"atob",
"blur",
"btoa",
"cancelAnimationFrame",
"captureEvents",
"clearInterval",
"clearTimeout",
"close",
"confirm",
"fetch",
"focus",
"getComputedStyle",
"matchMedia",
"open",
"prompt",
"queueMicrotask",
"releaseEvents",
"requestAnimationFrame",
"setInterval",
"setTimeout",
"stop"
]);
function mirrorExtensionGlobalsFromWindow(win) {
if (!win) return;
if (win.tc) {
globalThis.tc = win.tc;
}
for (const key of Object.keys(win)) {
if (WINDOW_GLOBAL_SKIP.has(key)) continue;
if (/^[A-Z]/.test(key)) continue;
const val = win[key];
if (typeof val === "function") {
globalThis[key] = val;
}
}
}
export function loadScript(relPath) {
window.eval(
const source =
"var chrome = window.chrome || globalThis.chrome;\n" +
readRepoFile(relPath) +
"\n//# sourceURL=" +
relPath
);
readRepoFile(relPath) +
"\n//# sourceURL=" +
relPath;
const el = document.createElement("script");
el.textContent = source;
document.head.appendChild(el);
mirrorExtensionGlobalsFromWindow(window);
}
export async function flushAsyncWork() {
+79 -10
View File
@@ -1,5 +1,6 @@
const fs = require("fs");
const path = require("path");
const { JSDOM } = require("jsdom");
const { vi } = require("vitest");
const ROOT = path.resolve(__dirname, "..", "..");
@@ -18,22 +19,90 @@ function readWorkspaceFile(relPath) {
}
function loadHtmlFile(relPath) {
document.open();
document.write(readWorkspaceFile(relPath));
document.close();
loadHtmlString(readWorkspaceFile(relPath));
}
function applyJSDOMWindow(win) {
globalThis.window = win;
globalThis.document = win.document;
globalThis.navigator = win.navigator;
globalThis.customElements = win.customElements;
globalThis.HTMLElement = win.HTMLElement;
globalThis.Element = win.Element;
globalThis.Node = win.Node;
globalThis.Text = win.Text;
globalThis.DocumentFragment = win.DocumentFragment;
globalThis.Event = win.Event;
globalThis.MouseEvent = win.MouseEvent;
globalThis.KeyboardEvent = win.KeyboardEvent;
globalThis.DOMParser = win.DOMParser;
globalThis.URL = win.URL;
globalThis.Blob = win.Blob;
globalThis.FileReader = win.FileReader;
win.Date = globalThis.Date;
win.open = vi.fn();
win.close = vi.fn();
}
function loadHtmlString(html) {
document.open();
document.write(html);
document.close();
const dom = new JSDOM(html, {
url: "https://example.org/",
pretendToBeVisual: true,
runScripts: "dangerously"
});
applyJSDOMWindow(dom.window);
}
const WINDOW_GLOBAL_SKIP = new Set([
"alert",
"atob",
"blur",
"btoa",
"cancelAnimationFrame",
"captureEvents",
"clearInterval",
"clearTimeout",
"close",
"confirm",
"fetch",
"focus",
"getComputedStyle",
"matchMedia",
"open",
"prompt",
"queueMicrotask",
"releaseEvents",
"requestAnimationFrame",
"setInterval",
"setTimeout",
"stop"
]);
function mirrorExtensionGlobalsFromWindow(win) {
if (!win) return;
if (win.tc) {
globalThis.tc = win.tc;
}
for (const key of Object.keys(win)) {
if (WINDOW_GLOBAL_SKIP.has(key)) continue;
if (/^[A-Z]/.test(key)) continue;
const val = win[key];
if (typeof val === "function") {
globalThis[key] = val;
}
}
}
function evaluateScript(relPath) {
const source = readWorkspaceFile(relPath);
window.eval(
`${source}\n//# sourceURL=${workspacePath(relPath).replace(/\\/g, "/")}`
);
const absPath = workspacePath(relPath);
const source =
"var chrome = window.chrome || globalThis.chrome;\n" +
readWorkspaceFile(relPath) +
`\n//# sourceURL=${absPath.replace(/\\/g, "/")}`;
const el = document.createElement("script");
el.textContent = source;
document.head.appendChild(el);
mirrorExtensionGlobalsFromWindow(window);
}
function fireDOMContentLoaded() {
+30
View File
@@ -0,0 +1,30 @@
/**
* Point Vitest/jsdom test globals at a new JSDOM window (no document.write).
* Call after creating `new JSDOM(html, options).window`.
*/
import { vi } from "vitest";
export function applyJSDOMWindow(win) {
globalThis.window = win;
globalThis.document = win.document;
globalThis.navigator = win.navigator;
globalThis.customElements = win.customElements;
globalThis.HTMLElement = win.HTMLElement;
globalThis.Element = win.Element;
globalThis.Node = win.Node;
globalThis.Text = win.Text;
globalThis.DocumentFragment = win.DocumentFragment;
globalThis.Event = win.Event;
globalThis.MouseEvent = win.MouseEvent;
globalThis.KeyboardEvent = win.KeyboardEvent;
globalThis.DOMParser = win.DOMParser;
globalThis.URL = win.URL;
globalThis.Blob = win.Blob;
globalThis.FileReader = win.FileReader;
// Vitest fake timers patch host `Date`; jsdom’s window keeps its own otherwise.
win.Date = globalThis.Date;
win.open = vi.fn();
win.close = vi.fn();
}