@@ -17,21 +17,27 @@ function isRedirect(status: number) {
1717 return status >= 300 && status < 400 ;
1818}
1919
20- /** Use the filterResponseData API to transform a JSON document to HTML. */
20+ /**
21+ * Use the filterResponseData API to transform a JSON document to HTML. This
22+ * converts to the same HTML that Chrome does by default - it's only used in
23+ * Firefox.
24+ */
2125function transformResponseToJSON ( details : chrome . webRequest . WebResponseHeadersDetails ) {
2226 const filter = browser . webRequest . filterResponseData ( details . requestId ) ;
2327
2428 const dec = new TextDecoder ( "utf-8" ) ;
2529 const enc = new TextEncoder ( ) ;
26- let content = "" ;
30+
31+ filter . onstart = ( _event ) => {
32+ filter . write ( enc . encode ( "<!DOCTYPE html><html><body><pre>" ) ) ;
33+ } ;
2734
2835 filter . ondata = ( event ) => {
29- content = content + dec . decode ( event . data ) ;
36+ filter . write ( enc . encode ( dec . decode ( event . data ) ) ) ;
3037 } ;
3138
3239 filter . onstop = ( _event : Event ) => {
33- const outputDoc = `<!DOCTYPE html><html><body><pre>${ content } </pre></body></html>` ;
34- filter . write ( enc . encode ( outputDoc ) ) ;
40+ filter . write ( enc . encode ( "</pre></body></html>" ) ) ;
3541 filter . disconnect ( ) ;
3642 } ;
3743}
@@ -65,10 +71,9 @@ chrome.webRequest.onHeadersReceived.addListener(
6571 [ "blocking" , "responseHeaders" ]
6672) ;
6773
68- // Listen for a message from the content script to decide whether to
69- // operate on the page. This is only necessary when the browser does
70- // not support filterResponseData. Calls sendResponse with a boolean
71- // that's true if the content script should run, and false otherwise.
74+ // Listen for a message from the content script to decide whether to operate on
75+ // the page. Calls sendResponse with a boolean that's true if the content script
76+ // should run, and false otherwise.
7277chrome . runtime . onMessage . addListener ( ( _message , sender , sendResponse ) => {
7378 if ( sender . url ?. startsWith ( "file://" ) && sender . url . endsWith ( ".json" ) ) {
7479 sendResponse ( true ) ;
0 commit comments