-
Notifications
You must be signed in to change notification settings - Fork 223
/
content.ts
45 lines (40 loc) · 1.44 KB
/
content.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { errorPage, jsonToHTML } from "./jsonformatter";
import { installCollapseEventListeners } from "./collapse";
import { safeStringEncodeNums } from "./safe-encode-numbers";
/**
* This script runs on every page. It communicates with the background script
* to help decide whether to treat the contents of the page as JSON.
*/
chrome.runtime.sendMessage("jsonview-is-json", (response: boolean) => {
if (!response) {
return;
}
// At least in chrome, the JSON is wrapped in a pre tag.
const jsonElems = document.getElementsByTagName("pre");
let content: string | null = null;
if (jsonElems.length >= 1) {
content = jsonElems[0].textContent;
} else {
// Sometimes there's no pre? I'm not sure why this would happen
content = (document.body.firstChild ?? document.body).textContent;
}
let outputDoc = "";
let jsonObj: any = null;
if (content === null) {
outputDoc = errorPage(new Error("No content"), "", document.URL);
} else {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
jsonObj = JSON.parse(safeStringEncodeNums(content));
outputDoc = jsonToHTML(jsonObj, document.URL);
} catch (e) {
outputDoc = errorPage(
e instanceof Error ? e : typeof e === "string" ? new Error(e) : new Error("Unknown error"),
content,
document.URL
);
}
}
document.documentElement.innerHTML = outputDoc;
installCollapseEventListeners();
});