Skip to content

Instantly share code, notes, and snippets.

@lonelycompiler
Last active March 23, 2023 06:19
Show Gist options
  • Select an option

  • Save lonelycompiler/a2c4fb1583763702eeed90649bac821c to your computer and use it in GitHub Desktop.

Select an option

Save lonelycompiler/a2c4fb1583763702eeed90649bac821c to your computer and use it in GitHub Desktop.
<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