-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#13] Add prettyfier based on content-type
[#13] Fix admin.js [#13] Fixed admin button [#13] Remove unnecessary views and urls [#13] Remove message in quickstart.rst [#13] Black
- Loading branch information
1 parent
988e2c9
commit aca1430
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
log_outgoing_requests/static/log_outgoing_requests/js/admin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const link = document.querySelector(".prettify-toggle-link"); | ||
if (link) { | ||
link.addEventListener("click", function (event) { | ||
event.preventDefault(); | ||
const textArea = document.querySelector(".prettify-output"); | ||
const contentType = textArea.getAttribute("content-type"); | ||
const responseBody = textArea.value; | ||
if (textArea.style.display === "none") { | ||
let outputValue = ''; | ||
if (contentType.includes('json')) { | ||
try { | ||
outputValue = JSON.stringify(JSON.parse(responseBody), null, 3); | ||
} catch (error) { | ||
outputValue = "<br><span style='color: red;'>Invalid JSON format</span>"; | ||
} | ||
} else if (contentType.includes('xml')) { | ||
try { | ||
outputValue = prettifyXml(responseBody); | ||
} catch (xmlError) { | ||
outputValue = "<br><span style='color: red;'>Invalid XML format</span>"; | ||
} | ||
} | ||
textArea.value = outputValue; | ||
textArea.style.display = "inline"; | ||
} else { | ||
textArea.style.display = "none"; | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
var prettifyXml = function(sourceXml) { | ||
var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml'); | ||
var xsltDoc = new DOMParser().parseFromString([ | ||
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">', | ||
' <xsl:strip-space elements="*"/>', | ||
' <xsl:template match="node()|@*">', | ||
' <xsl:copy>', | ||
' <xsl:apply-templates select="node()|@*"/>', | ||
' </xsl:copy>', | ||
' </xsl:template>', | ||
' <xsl:output method="xml" indent="yes"/>', | ||
'</xsl:stylesheet>', | ||
].join('\n'), 'application/xml'); | ||
|
||
var xsltProcessor = new XSLTProcessor(); | ||
xsltProcessor.importStylesheet(xsltDoc); | ||
var resultDoc = xsltProcessor.transformToDocument(xmlDoc); | ||
var resultXml = new XMLSerializer().serializeToString(resultDoc); | ||
return resultXml; | ||
}; | ||
|