Skip to content

Commit

Permalink
Actually we don't need to transform the document
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Dec 29, 2023
1 parent a6d30ab commit 71b6ffc
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## JSONView 2.6.0
## JSONView 2.6.1

- Restore JSONView's ability to work in Firefox without having to manually disable the built-in JSON viewer.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "jsonview@brh.numbera.com",
"version": "2.6.0",
"version": "2.6.1",
"name": "jsonview",
"title": "JSONView",
"description": "View JSON documents in the browser.",
Expand Down
41 changes: 1 addition & 40 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,6 @@ function isRedirect(status: number) {
return status >= 300 && status < 400;
}

/**
* Use the filterResponseData API to transform a JSON document to HTML. This
* converts to the same HTML that Chrome does by default - it's only used in
* Firefox.
*/
function transformResponseToJSON(details: chrome.webRequest.WebResponseHeadersDetails) {
const filter = browser.webRequest.filterResponseData(details.requestId);

const dec = new TextDecoder("utf-8");
const enc = new TextEncoder();
let content = "";

filter.ondata = (event) => {
content += dec.decode(event.data, { stream: true });
};

filter.onstop = (_event: Event) => {
content += dec.decode();
const outputDoc = `<!DOCTYPE html><html><head><meta charset="utf-8"></head><body><pre>${escape(
content
)}</pre></body></html>`;

filter.write(enc.encode(outputDoc));
filter.disconnect();
};
}

function escape(s: string) {
const lookup = {
"&": "&amp;",
'"': "&quot;",
"'": "&apos;",
"<": "&lt;",
">": "&gt;",
} as { [key: string]: string };
return s.replace(/[&"'<>]/g, (c) => lookup[c]);
}

function detectJSON(event: chrome.webRequest.WebResponseHeadersDetails) {
if (!event.responseHeaders || isRedirect(event.statusCode)) {
return;
Expand All @@ -65,8 +27,7 @@ function detectJSON(event: chrome.webRequest.WebResponseHeadersDetails) {
) {
jsonUrls.add(event.url);
if (typeof browser !== "undefined" && "filterResponseData" in browser.webRequest) {
header.value = "text/html";
transformResponseToJSON(event);
header.value = "text/plain";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "JSONView",
"version": "2.6.0",
"version": "2.6.1",
"description": "__MSG_extensionDescription__",
"author": "Benjamin Hollis",
"homepage_url": "https://jsonview.com/",
Expand Down

0 comments on commit 71b6ffc

Please sign in to comment.