Created
September 27, 2019 10:50
-
-
Save Ferk/035ba7e1a7c92a1caee915e47ca10b6e to your computer and use it in GitHub Desktop.
Utility HTML page
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html><head><title>Misc Tools</title><meta charset="utf-8"/> | |
| <script> | |
| var actions = [ | |
| { name: "Remove newlines", | |
| call: function(input) { | |
| return input.replace(/(\r\n|\n|\r)/gm, ""); | |
| } | |
| }, | |
| { name: "Beautify XML", | |
| call: function(input) { | |
| var xmlDoc = new DOMParser().parseFromString(input, 'text/xml'); | |
| var xsltDoc = new DOMParser().parseFromString([ | |
| // describes how we want to modify the XML - indent everything | |
| '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">', | |
| ' <xsl:strip-space elements="*"/>', | |
| ' <xsl:template match="para[content-style][not(text())]">', // change to just text() to strip space in text nodes | |
| ' <xsl:value-of select="normalize-space(.)"/>', | |
| ' </xsl:template>', | |
| ' <xsl:template match="node()|@*">', | |
| ' <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>', | |
| ' </xsl:template>', | |
| ' <xsl:output 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; | |
| } | |
| }, | |
| { name: "Beautify JSON", | |
| call: function(input) { | |
| var data = JSON.parse(input); | |
| return JSON.stringify(data, null, 2); | |
| } | |
| }, | |
| { name: "UTF8 -> Base64", | |
| call: function(input) { | |
| return btoa(input); | |
| } | |
| }, | |
| { name: "Base64 -> UTF8", | |
| call: function(input) { | |
| return atob(input); | |
| } | |
| }, | |
| { name: 'Split CSV line', | |
| call: function(input) { | |
| return input.replace(/(\r\n|\n|\r)/gm, "").replace(/,/gm, ",\n"); | |
| } | |
| }, | |
| { name: "Clear", call: function(input) { return ""; } }, | |
| ]; | |
| /////////// | |
| function initActions(ul) | |
| { | |
| for (var i in actions) { | |
| var li = document.createElement("li"); | |
| li.innerText = actions[i].name; | |
| li.onclick = callAction.bind(actions[i]); | |
| ul.appendChild(li); | |
| } | |
| } | |
| function callAction() | |
| { | |
| setStatus("Calling: "+this.name, "success"); | |
| var target = document.getElementById('target'); | |
| try { | |
| var result = this.call(target.value); | |
| target.value = result; | |
| } | |
| catch(err) { | |
| setStatus(err.name + ": " + err.message, "error", err.stack); | |
| } | |
| } | |
| function setStatus(text, styleclass, titletext) | |
| { | |
| var status = document.getElementById('status'); | |
| status.innerText = text; | |
| status.className = styleclass; | |
| status.title = titletext; | |
| } | |
| /////////// | |
| function beautifyXml(xml) { | |
| var formatted = ''; | |
| var reg = /(>)(<)(\/*)/g; | |
| xml = xml.replace(reg, '$1\r\n$2$3'); | |
| var pad = 0; | |
| jQuery.each(xml.split('\r\n'), function(index, node) { | |
| var indent = 0; | |
| if (node.match( /.+<\/\w[^>]*>$/ )) { | |
| indent = 0; | |
| } else if (node.match( /^<\/\w/ )) { | |
| if (pad != 0) { | |
| pad -= 1; | |
| } | |
| } else if (node.match( /^<\w([^>]*[^\/])?>.*$/ )) { | |
| indent = 1; | |
| } else { | |
| indent = 0; | |
| } | |
| var padding = ''; | |
| for (var i = 0; i < pad; i++) { | |
| padding += ' '; | |
| } | |
| formatted += padding + node + '\r\n'; | |
| pad += indent; | |
| }); | |
| return formatted; | |
| } | |
| </script> | |
| <style> | |
| body{margin:0 1em;font-family: Tahoma, Verdana, Arial, sans-serif;} | |
| #actions{list-style-type: none;} | |
| #actions li {float:left;width:200px;border-style:outset;cursor:pointer;background-color:#ddd;font-size:large;margin:0.3em;} | |
| #target{padding:0.2em;font-size:small;width:100%;height:40em;} | |
| .error{background-color:#fdd;font-weight:bold;padding:5px;} | |
| .success{background-color:#dfd} | |
| </style> | |
| </head> | |
| <body onload="initActions(document.getElementById('actions'));"> | |
| <h2>Tools</h2> | |
| <div id="status">Paste the desired text in the textarea and click one of the actions to transform it.</div> | |
| <ul id="actions"></ul> | |
| <textarea id="target"></textarea> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment