Skip to content

Commit

Permalink
No longer special case Firefox
Browse files Browse the repository at this point in the history
bhollis committed May 3, 2023
1 parent 2d67c1c commit 249c496
Showing 3 changed files with 12 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## JSONView 2.4.0

- Preserve indentation when copying JSON.
- In Chrome, the JSON object is available from the console via the global "data" property.
- The JSON object is available from the console via the global "data" property.
- Increased the number of content types that will be recognized as JSON.
- Added Indonesian localization.

35 changes: 11 additions & 24 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { jsonToHTML, errorPage } from "./jsonformatter";
import { safeStringEncodeNums } from "./safe-encode-numbers";

/**
* This is the background script that runs independent of any document. It listens to main frame requests
* and kicks in if the headers indicate JSON. If we have the filterResponseData API available, we will use
* that to directly change the content of the response to HTML. Otherwise we interface with a content script
* to reformat the page.
* This is the background script that runs independent of any document. It
* listens to main frame requests and kicks in if the headers indicate JSON. If
* we have the filterResponseData API available, we will use that to change the
* page to what Chrome displays for JSON (this is only used in Firefox). Then a
* content script reformats the page.
*/

// Look for JSON if the content type is "application/json",
@@ -32,17 +30,8 @@ function transformResponseToJSON(details: chrome.webRequest.WebResponseHeadersDe
};

filter.onstop = (_event: Event) => {
let outputDoc = "";

try {
const jsonObj = JSON.parse(safeStringEncodeNums(content));
outputDoc = jsonToHTML(jsonObj, details.url);
} catch (e: any) {
outputDoc = errorPage(e, content, details.url);
}

const outputDoc = `<!DOCTYPE html><html><body><pre>${content}</pre></body></html>`;
filter.write(enc.encode(outputDoc));

filter.disconnect();
};
}
@@ -57,11 +46,10 @@ function detectJSON(event: chrome.webRequest.WebResponseHeadersDetails) {
header.value &&
jsonContentType.test(header.value)
) {
jsonUrls.add(event.url);
if (typeof browser !== "undefined" && "filterResponseData" in browser.webRequest) {
header.value = "text/html";
transformResponseToJSON(event);
} else {
jsonUrls.add(event.url);
}
}
}
@@ -77,16 +65,15 @@ chrome.webRequest.onHeadersReceived.addListener(
["blocking", "responseHeaders"]
);

// Listen for a message from the content script to decide whether to
// operate on the page. This is only necessary when the browser does
// not support filterResponseData. Calls sendResponse with a boolean
// that's true if the content script should run, and false otherwise.
chrome.runtime.onMessage.addListener((_message, sender, sendResponse) => {
if (sender.url?.startsWith("file://") && sender.url.endsWith(".json")) {
sendResponse(true);
return;
}
// If we support this API, we don't need to invoke the content script.
if ("filterResponseData" in chrome.webRequest) {
sendResponse(false);
return;
}
sendResponse(sender.url && jsonUrls.has(sender.url));
if (sender.url) {
jsonUrls.delete(sender.url);
1 change: 0 additions & 1 deletion src/jsonformatter.ts
Original file line number Diff line number Diff line change
@@ -215,7 +215,6 @@ function toHTML(content: string, title: string) {
<html><head><title>${htmlEncode(title)}</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="${chrome.runtime.getURL("viewer.css")}">
<script type="text/javascript" src="${chrome.runtime.getURL("viewer.js")}"></script>
</head><body>
${content}
</body></html>`;

0 comments on commit 249c496

Please sign in to comment.