Last active
March 23, 2023 06:19
-
-
Save lonelycompiler/a2c4fb1583763702eeed90649bac821c to your computer and use it in GitHub Desktop.
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
| <html> | |
| <head> | |
| <title>strip end</title> | |
| <style> | |
| div, input { | |
| flex-direction: column; | |
| flex: 1; | |
| } | |
| </style> | |
| <script> | |
| /* | |
| This code will take in some input string | |
| And remove all whitespace at the end (other than a newline), | |
| I found that using trim and trimEnd doesn't work for some reason.. | |
| */ | |
| function download(text) { | |
| let element = document.createElement('a'); | |
| element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | |
| element.setAttribute('download', 'new_file.txt'); | |
| element.style.display = 'none'; | |
| document.body.appendChild(element); | |
| element.click(); | |
| // new Promise(resolve => setTimeout(resolve, 1000)) | |
| // document.body.removeChild(element); | |
| } | |
| function strip_string () { | |
| // input_str) { | |
| const input_file = document.getElementById("file").files[0]; | |
| let data = ''; | |
| Promise.resolve(input_file.text()).then(input_str => { | |
| input_str = input_str.replaceAll(/\s+\n/g, '\n') | |
| download(input_str.trim()); | |
| }) | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <table> | |
| <tr> | |
| <th>Form</th> | |
| </tr> | |
| <td> | |
| <form id="form" action="javascript:strip_string()"> | |
| <div> | |
| <input id="file" type="file"> | |
| <input id="submit" type="submit"> | |
| </div> | |
| </form> | |
| </td> | |
| </table> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment