Skip to content

Instantly share code, notes, and snippets.

@nananek
Last active June 26, 2024 22:47
Show Gist options
  • Select an option

  • Save nananek/58ff8886906fd1c75209b268efff2204 to your computer and use it in GitHub Desktop.

Select an option

Save nananek/58ff8886906fd1c75209b268efff2204 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Search Application</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/encoding-japanese@1.0.30/encoding.min.js"></script>
<style>
.result-button {
display: block;
width: 100%;
margin-bottom: 5px;
text-align: left;
cursor: pointer;
}
.result-button:hover {
background-color: #f0f0f0;
}
</style>
</head>
<body>
<div class="container mt-5">
<h2 class="mb-4">Text Search Application</h2>
<div class="form-group" id="fileUploadSection">
<label for="fileInput">Upload Text File:</label>
<input type="file" class="form-control-file" id="fileInput">
</div>
<div class="form-group">
<label for="queryInput">Enter Query:</label>
<input type="text" class="form-control" id="queryInput" placeholder="Enter query">
</div>
<h3 class="mt-4">Results:</h3>
<div id="results" class="border p-3"></div>
</div>
<script>
$(document).ready(function() {
let fileContent = "";
function escapeHtml(text) {
return text.replace(/[&<>"']/g, function (match) {
const escapeChars = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;'
};
return escapeChars[match];
});
}
$("#fileInput").change(function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const result = new Uint8Array(e.target.result);
const unicodeString = Encoding.convert(result, {
to: 'UNICODE',
from: 'SJIS',
type: 'string'
});
fileContent = unicodeString.replace(/\r\n/g, '\n');
$("#fileUploadSection").hide();
$("#queryInput").focus();
};
reader.readAsArrayBuffer(file);
}
});
$("#queryInput").on("input", function() {
const query = $(this).val().toLowerCase();
const lines = fileContent.split('\n');
const results = lines.filter(line => line.toLowerCase().includes(query));
const resultsHtml = results.map(line => `<button class="btn btn-light result-button">${escapeHtml(line)}</button>`).join('');
$("#results").html(resultsHtml);
$(".result-button").click(function() {
const text = $(this).text();
navigator.clipboard.writeText(text).then(function() {
alert("Copied to clipboard: " + text);
});
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment