gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js text editor long chars copy share "#/"
- User
html css js text editor long chars copy share "#/"
- Assistant
html css js text editor long chars copy share "#/"
Certainly! Below is a basic example of a web-based text editor using HTML, CSS, and JavaScript. It supports:
#/<encoded-text>).This example encodes the text in the URL hash to allow sharing long text without problems with special characters.
<!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 Share</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 1rem;
max-width: 800px;
margin: auto;
}
textarea {
width: 100%;
height: 300px;
font-family: monospace;
font-size: 1rem;
padding: 0.5rem;
box-sizing: border-box;
resize: vertical;
}
button {
margin-top: 0.5rem;
margin-right: 0.5rem;
padding: 0.5rem 1rem;
font-size: 1rem;
cursor: pointer;
}
.msg {
margin-top: 0.5rem;
color: green;
}
</style>
</head>
<body>
<h1>Simple Text Editor</h1>
<textarea id="editor" placeholder="Type or paste your text here..."></textarea>
<br />
<button id="copyBtn">Copy to Clipboard</button>
<button id="shareBtn">Share Text via URL</button>
<div class="msg" id="message"></div>
<script>
const editor = document.getElementById('editor');
const copyBtn = document.getElementById('copyBtn');
const shareBtn = document.getElementById('shareBtn');
const message = document.getElementById('message');
/**
* Encode text for URL.
* Using encodeURIComponent after btoa will limit supported characters.
* Using Base64 with UTF-8 encoding is better:
* Use this helper to b64 encode properly Unicode strings.
*/
function b64EncodeUnicode(str) {
// First encode string as UTF-8, then base64 encode.
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
(match, p1) => String.fromCharCode('0x' + p1)));
}
function b64DecodeUnicode(str) {
// Decode base64 to UTF-8 string
return decodeURIComponent(atob(str).split('').map(c => {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
// Load text from URL hash if exists, format: #/<base64encodedtext>
function loadFromHash() {
if (location.hash.startsWith('#/')) {
try {
const encodedText = location.hash.slice(2);
const decodedText = b64DecodeUnicode(encodedText);
editor.value = decodedText;
message.textContent = 'Loaded text from URL.';
} catch (e) {
message.textContent = 'Could not decode text from URL.';
message.style.color = 'red';
}
}
}
// Copy text to clipboard
copyBtn.onclick = () => {
editor.select();
try {
if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard api method'
navigator.clipboard.writeText(editor.value).then(() => {
message.textContent = 'Text copied to clipboard!';
message.style.color = 'green';
}, () => {
message.textContent = 'Failed to copy text.';
message.style.color = 'red';
});
} else {
// text area method
document.execCommand('copy');
message.textContent = 'Text copied to clipboard!';
message.style.color = 'green';
}
} catch (err) {
message.textContent = 'Failed to copy text.';
message.style.color = 'red';
}
};
// Update URL hash with encoded text for sharing
shareBtn.onclick = () => {
if (editor.value.trim().length === 0) {
message.textContent = 'Text area is empty.';
message.style.color = 'red';
return;
}
try {
const encoded = b64EncodeUnicode(editor.value);
const newUrl = location.origin + location.pathname + '#/' + encoded;
history.replaceState(null, null, newUrl);
message.textContent = 'URL updated! You can copy the URL from the address bar to share.';
message.style.color = 'green';
} catch (e) {
message.textContent = 'Failed to encode text for sharing.';
message.style.color = 'red';
}
};
// Load text on page load from hash
loadFromHash();
</script>
</body>
</html>
#/ followed by encoded text, it will load that text back into the editor.This solution handles long texts, supports Unicode characters, and uses the URL hash for sharing without page reload.
If you want me to add more features (like saving to a file, syntax highlighting, etc.), let me know!