Skip to content

Commit 4b7d183

Browse files
authored
Minor improvements to zen-mode extension (#435)
* Decided to use `Object.defineProperty` to mark `Blob` objects. * Removed some stray debugging code. * Shared some constants. * Introduced a `jsonStringifyPrettyInDev` utility for the manifest.
1 parent e6c6a76 commit 4b7d183

File tree

10 files changed

+70
-58
lines changed

10 files changed

+70
-58
lines changed
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import { writeFile } from "node:fs/promises";
22
import path from "node:path";
33

4-
import prettier from "prettier";
5-
6-
import { isEnvironmentDev } from "@code-chronicles/util/isEnvironmentDev";
4+
import { jsonStringifyPrettyInDev } from "@code-chronicles/util/jsonStringifyPrettyInDev";
75

86
import { readAllGoodies } from "../package-goodies/readAllGoodies.ts";
97
import { WEB_APP_DIST } from "./constants.ts";
108

11-
async function readAllGoodiesAsString(): Promise<string> {
12-
const goodies = await readAllGoodies();
13-
const text = JSON.stringify(goodies);
14-
15-
if (isEnvironmentDev()) {
16-
// Could also change the arguments to `JSON.stringify` but thought we
17-
// could give Prettier the chance to do something fancier.
18-
return await prettier.format(text, { parser: "json" });
19-
}
20-
21-
return text + "\n";
22-
}
23-
249
export async function writeGoodiesJson(): Promise<void> {
25-
const text = await readAllGoodiesAsString();
10+
const goodies = await readAllGoodies();
2611

27-
await writeFile(path.join(WEB_APP_DIST, "goodies.json"), text, {
28-
encoding: "utf8",
29-
});
12+
// TODO: share the filenames via some constants
13+
await writeFile(
14+
path.join(WEB_APP_DIST, "goodies.json"),
15+
jsonStringifyPrettyInDev(goodies),
16+
{ encoding: "utf8" },
17+
);
3018
}

workspaces/eslint-config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@typescript-eslint/parser": "8.5.0",
2828
"eslint-import-resolver-typescript": "3.6.3",
2929
"eslint-plugin-import": "2.30.0",
30-
"eslint-plugin-import-x": "4.2.1",
30+
"eslint-plugin-import-x": "4.3.0",
3131
"eslint-plugin-jest": "28.8.3",
3232
"globals": "15.9.0",
3333
"typescript": "5.6.2"

workspaces/leetcode-zen-mode/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"typecheck": "tsc --pretty --project ."
2323
},
2424
"dependencies": {
25-
"@code-chronicles/util": "workspace:*"
25+
"@code-chronicles/util": "workspace:*",
26+
"nullthrows": "patch:nullthrows@npm%3A1.1.1#~/.yarn/patches/nullthrows-npm-1.1.1-3d1f817134.patch"
2627
},
2728
"devDependencies": {
2829
"@code-chronicles/eslint-config": "workspace:*",
@@ -32,6 +33,7 @@
3233
"prettier": "3.3.3",
3334
"ts-loader": "9.5.1",
3435
"tsx": "4.19.1",
36+
"type-fest": "4.26.1",
3537
"typescript": "5.6.2",
3638
"webpack": "5.94.0",
3739
"webpack-cli": "5.1.4"

workspaces/leetcode-zen-mode/src/extension/setUpFileReaderReadAsTextInterception.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export function setUpFileReaderReadAsTextInterception(): void {
55
const { readAsText } = FileReader.prototype;
66

77
FileReader.prototype.readAsText = function (blob) {
8-
(window as unknown as Record<string, unknown>).foo = blob;
98
if (!(blob instanceof Blob) || !Object.hasOwn(blob, GRAPHQL_XHR_RESPONSE)) {
109
readAsText.apply(
1110
this,

workspaces/leetcode-zen-mode/src/extension/setUpXhrResponseInterception.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import nullthrows from "nullthrows";
2+
13
import { GRAPHQL_XHR_RESPONSE } from "./constants.ts";
24

35
export function setUpXhrResponseInterception(): void {
@@ -9,15 +11,20 @@ export function setUpXhrResponseInterception(): void {
911
Object.defineProperty(XMLHttpRequest.prototype, "response", {
1012
...xhrResponseDescriptor,
1113
get() {
12-
const res = xhrResponseDescriptor.get!.call(this);
14+
const res = nullthrows(xhrResponseDescriptor.get).call(this);
15+
1316
if (
1417
this.responseURL === "https://leetcode.com/graphql/" &&
1518
res instanceof Blob &&
1619
res.type === "application/json"
1720
) {
18-
(res as unknown as Record<PropertyKey, unknown>)[GRAPHQL_XHR_RESPONSE] =
19-
true;
21+
Object.defineProperty(res, GRAPHQL_XHR_RESPONSE, {
22+
enumerable: false,
23+
configurable: false,
24+
value: undefined,
25+
});
2026
}
27+
2128
return res;
2229
},
2330
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const DIST_DIRECTORY = "dist";
2+
3+
export const SCRIPT_FILENAME = "main.js";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { JsonObject } from "type-fest";
2+
3+
import { SCRIPT_FILENAME } from "./constants.ts";
4+
import packageJson from "../../package.json" with { type: "json" };
5+
6+
export function getManifest(): JsonObject {
7+
return {
8+
name: "LeetCode Zen Mode",
9+
description: packageJson.description,
10+
version: packageJson.version,
11+
12+
// eslint-disable-next-line camelcase
13+
manifest_version: 3,
14+
// eslint-disable-next-line camelcase
15+
content_scripts: [
16+
{
17+
matches: ["https://*.leetcode.com/*"],
18+
js: [SCRIPT_FILENAME],
19+
// eslint-disable-next-line camelcase
20+
run_at: "document_start",
21+
world: "MAIN",
22+
},
23+
],
24+
};
25+
}

workspaces/leetcode-zen-mode/src/scripts/writeManifest.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
11
import { mkdir, writeFile } from "node:fs/promises";
22
import path from "node:path";
33

4-
import packageJson from "../../package.json" with { type: "json" };
4+
import { jsonStringifyPrettyInDev } from "@code-chronicles/util/jsonStringifyPrettyInDev";
55

6-
// TODO: figure out a way to reuse this in webpack and eslint configs
7-
const DIST_DIRECTORY = "dist";
6+
import { DIST_DIRECTORY } from "./constants.ts";
7+
import { getManifest } from "./getManifest.ts";
88

99
async function main(): Promise<void> {
1010
await mkdir(DIST_DIRECTORY, { recursive: true });
1111

12+
const manifest = getManifest();
13+
1214
await writeFile(
1315
path.join(DIST_DIRECTORY, "manifest.json"),
14-
JSON.stringify(
15-
{
16-
name: "LeetCode Zen Mode",
17-
description: packageJson.description,
18-
version: packageJson.version,
19-
20-
// eslint-disable-next-line camelcase
21-
manifest_version: 3,
22-
// eslint-disable-next-line camelcase
23-
content_scripts: [
24-
{
25-
matches: ["https://*.leetcode.com/*"],
26-
// TODO: share this constant with webpack
27-
js: ["main.js"],
28-
// eslint-disable-next-line camelcase
29-
run_at: "document_start",
30-
world: "MAIN",
31-
},
32-
],
33-
},
34-
// TODO: prettify or not based on NODE_ENV
35-
null,
36-
2,
37-
) + "\n",
16+
jsonStringifyPrettyInDev(manifest),
3817
{ encoding: "utf8" },
3918
);
4019
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { isEnvironmentDev } from "@code-chronicles/util/isEnvironmentDev";
2+
3+
export function jsonStringifyPrettyInDev(value: unknown): string {
4+
return isEnvironmentDev()
5+
? JSON.stringify(value, null, 2)
6+
: JSON.stringify(value);
7+
}

yarn.lock

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)