From 249c49630bad78c7c8c1481e747f03a307e2fefc Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Wed, 3 May 2023 14:03:05 -0700 Subject: [PATCH] No longer special case Firefox --- CHANGELOG.md | 2 +- src/background.ts | 35 +++++++++++------------------------ src/jsonformatter.ts | 1 - 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86df046..a450640 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/background.ts b/src/background.ts index 330a3c0..7541f3a 100644 --- a/src/background.ts +++ b/src/background.ts @@ -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 = `
${content}
`; 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); diff --git a/src/jsonformatter.ts b/src/jsonformatter.ts index a906372..aa564f2 100644 --- a/src/jsonformatter.ts +++ b/src/jsonformatter.ts @@ -215,7 +215,6 @@ function toHTML(content: string, title: string) { ${htmlEncode(title)} - ${content} `;