gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js text editor maximum chars copy share "#/"
- User
html css js text editor maximum chars copy share "#/"
- Assistant
html css js text editor maximum chars copy share "#/"
Sure! Here's a simple web-based text editor with the following features:
#).<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Text Editor with Max Chars, Copy & Share</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
max-width: 600px;
margin: auto;
}
textarea {
width: 100%;
height: 200px;
font-size: 16px;
padding: 10px;
box-sizing: border-box;
resize: vertical;
}
#charCount {
margin-top: 5px;
font-size: 14px;
color: #555;
text-align: right;
}
button {
margin-top: 10px;
margin-right: 10px;
font-size: 16px;
padding: 8px 15px;
cursor: pointer;
}
#message {
margin-top: 10px;
font-size: 14px;
color: green;
}
</style>
</head>
<body>
<h2>Simple Text Editor</h2>
<textarea id="editor" maxlength="500" placeholder="Type here..."></textarea>
<div id="charCount">0 / 500</div>
<button id="copyBtn">Copy</button>
<button id="shareBtn">Share</button>
<div id="message"></div>
<script>
const textarea = document.getElementById('editor');
const charCount = document.getElementById('charCount');
const copyBtn = document.getElementById('copyBtn');
const shareBtn = document.getElementById('shareBtn');
const message = document.getElementById('message');
const maxChars = 500;
// Update char count
function updateCharCount() {
const len = textarea.value.length;
charCount.textContent = `${len} / ${maxChars}`;
}
// Load text from URL hash if present
function loadFromHash() {
if (location.hash.startsWith("#/")) {
try {
// Decode after '#/' and decodeURIComponent
let encodedText = location.hash.slice(2);
let decodedText = decodeURIComponent(encodedText);
// Limit text if longer than maxChars
if (decodedText.length > maxChars) {
decodedText = decodedText.substring(0, maxChars);
}
textarea.value = decodedText;
updateCharCount();
} catch (e) {
console.error("Invalid text in URL hash");
}
}
}
// Copy text to clipboard
function copyText() {
navigator.clipboard.writeText(textarea.value).then(() => {
message.textContent = "Copied to clipboard!";
setTimeout(() => { message.textContent = ""; }, 2000);
}).catch(() => {
message.textContent = "Failed to copy.";
setTimeout(() => { message.textContent = ""; }, 2000);
});
}
// Share text via URL hash
function shareText() {
// Encode text safely for URL
const encodedText = encodeURIComponent(textarea.value);
const shareURL = location.origin + location.pathname + "#/" + encodedText;
// Use the Web Share API if available
if (navigator.share) {
navigator.share({
title: "Shared Text",
url: shareURL
}).catch(() => {
// If user cancels sharing or error, fallback to copying
copyShareURL(shareURL);
});
} else {
// If no share API, copy URL to clipboard instead
copyShareURL(shareURL);
}
}
function copyShareURL(url) {
navigator.clipboard.writeText(url).then(() => {
message.textContent = "Share URL copied to clipboard!";
setTimeout(() => { message.textContent = ""; }, 3000);
}).catch(() => {
message.textContent = "Failed to copy share URL.";
setTimeout(() => { message.textContent = ""; }, 3000);
});
}
// Event listeners
textarea.addEventListener('input', updateCharCount);
copyBtn.addEventListener('click', copyText);
shareBtn.addEventListener('click', shareText);
// Initialize
loadFromHash();
updateCharCount();
// Optional: Warn user if navigating away with unsaved changes or text present
/*
window.addEventListener('beforeunload', function(e) {
if (textarea.value.length > 0) {
e.preventDefault();
e.returnValue = '';
}
});
*/
</script>
</body>
</html>
<textarea> has a maxlength="500" attribute restricting input to 500 characters.#/.
#/ it reads and decodes that text and loads it into the editor.encodeURIComponent / decodeURIComponent to handle special chars safely.Just copy this full code into a .html file and open it in your browser. You can type text, copy it, share it, and revisit your saved/share URL to see the text restored.
Not yet included in a published release.