mirror of
https://github.com/justhtmls/html-tools.git
synced 2026-11-22 23:18:09 +08:00
410 lines
12 KiB
HTML
410 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<link rel="icon" href="../../favicon.svg">
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Base64 编码解码</title>
|
|
<link rel="canonical" href="https://www.htmls.dev/tools/base64-encode/app.html">
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
|
color: #eee;
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
.header {
|
|
text-align: center;
|
|
margin-bottom: 30px;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 2rem;
|
|
margin-bottom: 8px;
|
|
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
|
-webkit-background-clip: text;
|
|
-webkit-text-fill-color: transparent;
|
|
background-clip: text;
|
|
}
|
|
|
|
.mode-toggle {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 10px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.mode-btn {
|
|
padding: 10px 20px;
|
|
background: rgba(255,255,255,0.1);
|
|
border: 1px solid rgba(255,255,255,0.2);
|
|
color: #fff;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
}
|
|
|
|
.mode-btn.active {
|
|
background: linear-gradient(135deg, #f093fb, #f5576c);
|
|
border-color: transparent;
|
|
}
|
|
|
|
.editor-container {
|
|
display: grid;
|
|
grid-template-columns: 1fr 60px 1fr;
|
|
gap: 20px;
|
|
height: calc(100vh - 200px);
|
|
min-height: 400px;
|
|
}
|
|
|
|
.panel {
|
|
background: rgba(30, 30, 50, 0.8);
|
|
border-radius: 12px;
|
|
overflow: hidden;
|
|
display: flex;
|
|
flex-direction: column;
|
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
|
}
|
|
|
|
.panel-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
padding: 12px 16px;
|
|
background: rgba(0, 0, 0, 0.3);
|
|
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
|
}
|
|
|
|
.panel-title {
|
|
font-weight: 600;
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.copy-btn {
|
|
background: rgba(240, 147, 251, 0.2);
|
|
border: 1px solid rgba(240, 147, 251, 0.4);
|
|
color: #f093fb;
|
|
padding: 6px 12px;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 0.85rem;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.copy-btn:hover {
|
|
background: rgba(240, 147, 251, 0.3);
|
|
}
|
|
|
|
.copy-btn.copied {
|
|
background: rgba(34, 197, 94, 0.2);
|
|
border-color: rgba(34, 197, 94, 0.4);
|
|
color: #22c55e;
|
|
}
|
|
|
|
textarea {
|
|
flex: 1;
|
|
background: transparent;
|
|
border: none;
|
|
color: #e0e0e0;
|
|
padding: 16px;
|
|
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
|
font-size: 13px;
|
|
line-height: 1.6;
|
|
resize: none;
|
|
outline: none;
|
|
}
|
|
|
|
textarea::placeholder {
|
|
color: #666;
|
|
}
|
|
|
|
.arrow {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 24px;
|
|
color: #f093fb;
|
|
}
|
|
|
|
.options {
|
|
display: flex;
|
|
justify-content: center;
|
|
gap: 20px;
|
|
margin: 20px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.option {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
color: #888;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.option input {
|
|
accent-color: #f093fb;
|
|
}
|
|
|
|
.back-link {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
color: #888;
|
|
text-decoration: none;
|
|
font-size: 0.85rem;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.back-link:hover {
|
|
color: #f093fb;
|
|
}
|
|
|
|
.file-drop {
|
|
border: 2px dashed rgba(240, 147, 251, 0.3);
|
|
border-radius: 8px;
|
|
padding: 20px;
|
|
text-align: center;
|
|
color: #888;
|
|
margin-bottom: 10px;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.file-drop:hover, .file-drop.dragover {
|
|
border-color: #f093fb;
|
|
background: rgba(240, 147, 251, 0.1);
|
|
}
|
|
|
|
.file-drop input {
|
|
display: none;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.editor-container {
|
|
grid-template-columns: 1fr;
|
|
grid-template-rows: 1fr auto 1fr;
|
|
}
|
|
|
|
.arrow {
|
|
transform: rotate(90deg);
|
|
}
|
|
}
|
|
|
|
.tool-doc-link,
|
|
.tool-home-link {
|
|
position: fixed;
|
|
top: 16px;
|
|
padding: 6px 12px;
|
|
border-radius: 999px;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
color: #0f172a;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
border: 1px solid rgba(15, 23, 42, 0.12);
|
|
text-decoration: none;
|
|
letter-spacing: 0.02em;
|
|
z-index: 9999;
|
|
box-shadow: 0 6px 16px rgba(15, 23, 42, 0.08);
|
|
transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
|
|
}
|
|
|
|
.tool-doc-link {
|
|
right: 16px;
|
|
}
|
|
|
|
.tool-home-link {
|
|
right: 74px;
|
|
}
|
|
|
|
.tool-doc-link:hover,
|
|
.tool-home-link:hover {
|
|
background: rgba(255, 255, 255, 1);
|
|
border-color: rgba(15, 23, 42, 0.2);
|
|
transform: translateY(-1px);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<a class="tool-home-link" href="../../index.html">首页</a>
|
|
<a class="tool-doc-link" href="index.html">说明</a>
|
|
<div class="container">
|
|
<a href="../.." class="back-link">
|
|
<span>←</span> 返回 JustHTMLs
|
|
</a>
|
|
|
|
<div class="header">
|
|
<h1>Base64 编码解码</h1>
|
|
</div>
|
|
|
|
<div class="mode-toggle">
|
|
<button class="mode-btn active" data-mode="encode">编码</button>
|
|
<button class="mode-btn" data-mode="decode">解码</button>
|
|
</div>
|
|
|
|
<div class="options">
|
|
<label class="option">
|
|
<input type="checkbox" id="url-safe"> URL 安全模式
|
|
</label>
|
|
</div>
|
|
|
|
<div class="file-drop" id="file-drop">
|
|
<input type="file" id="file-input">
|
|
<span>📁 拖拽文件到此处或点击选择文件</span>
|
|
</div>
|
|
|
|
<div class="editor-container">
|
|
<div class="panel">
|
|
<div class="panel-header">
|
|
<span class="panel-title" id="input-title">输入文本</span>
|
|
<button class="copy-btn" onclick="copyInput()">复制</button>
|
|
</div>
|
|
<textarea id="input-text" placeholder="输入要编码的文本..."></textarea>
|
|
</div>
|
|
|
|
<div class="arrow">→</div>
|
|
|
|
<div class="panel">
|
|
<div class="panel-header">
|
|
<span class="panel-title" id="output-title">Base64 输出</span>
|
|
<button class="copy-btn" id="copy-output" onclick="copyOutput()">复制</button>
|
|
</div>
|
|
<textarea id="output-text" readonly placeholder="Base64 结果将显示在这里..."></textarea>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const inputText = document.getElementById('input-text');
|
|
const outputText = document.getElementById('output-text');
|
|
const urlSafeCheckbox = document.getElementById('url-safe');
|
|
const modeBtns = document.querySelectorAll('.mode-btn');
|
|
const fileDrop = document.getElementById('file-drop');
|
|
const fileInput = document.getElementById('file-input');
|
|
|
|
let currentMode = 'encode';
|
|
|
|
function encode(str) {
|
|
try {
|
|
let result = btoa(unescape(encodeURIComponent(str)));
|
|
if (urlSafeCheckbox.checked) {
|
|
result = result.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
}
|
|
return result;
|
|
} catch (e) {
|
|
return '错误: ' + e.message;
|
|
}
|
|
}
|
|
|
|
function decode(str) {
|
|
try {
|
|
if (urlSafeCheckbox.checked) {
|
|
str = str.replace(/-/g, '+').replace(/_/g, '/');
|
|
while (str.length % 4) str += '=';
|
|
}
|
|
return decodeURIComponent(escape(atob(str)));
|
|
} catch (e) {
|
|
return '错误: 无效的 Base64 字符串';
|
|
}
|
|
}
|
|
|
|
function convert() {
|
|
const input = inputText.value;
|
|
if (!input) {
|
|
outputText.value = '';
|
|
return;
|
|
}
|
|
outputText.value = currentMode === 'encode' ? encode(input) : decode(input);
|
|
}
|
|
|
|
function setMode(mode) {
|
|
currentMode = mode;
|
|
modeBtns.forEach(btn => btn.classList.toggle('active', btn.dataset.mode === mode));
|
|
document.getElementById('input-title').textContent = mode === 'encode' ? '输入文本' : 'Base64 输入';
|
|
document.getElementById('output-title').textContent = mode === 'encode' ? 'Base64 输出' : '解码结果';
|
|
inputText.placeholder = mode === 'encode' ? '输入要编码的文本...' : '输入 Base64 字符串...';
|
|
outputText.placeholder = mode === 'encode' ? 'Base64 结果将显示在这里...' : '解码结果将显示在这里...';
|
|
convert();
|
|
}
|
|
|
|
function copyOutput() {
|
|
if (!outputText.value) return;
|
|
navigator.clipboard.writeText(outputText.value);
|
|
const btn = document.getElementById('copy-output');
|
|
btn.textContent = '已复制!';
|
|
btn.classList.add('copied');
|
|
setTimeout(() => {
|
|
btn.textContent = '复制';
|
|
btn.classList.remove('copied');
|
|
}, 2000);
|
|
}
|
|
|
|
function copyInput() {
|
|
if (!inputText.value) return;
|
|
navigator.clipboard.writeText(inputText.value);
|
|
event.target.textContent = '已复制!';
|
|
setTimeout(() => {
|
|
event.target.textContent = '复制';
|
|
}, 2000);
|
|
}
|
|
|
|
function handleFile(file) {
|
|
const reader = new FileReader();
|
|
reader.onload = (e) => {
|
|
if (currentMode === 'encode') {
|
|
inputText.value = e.target.result;
|
|
convert();
|
|
}
|
|
};
|
|
reader.readAsDataURL(file);
|
|
}
|
|
|
|
modeBtns.forEach(btn => {
|
|
btn.addEventListener('click', () => setMode(btn.dataset.mode));
|
|
});
|
|
|
|
inputText.addEventListener('input', convert);
|
|
urlSafeCheckbox.addEventListener('change', convert);
|
|
|
|
fileDrop.addEventListener('click', () => fileInput.click());
|
|
fileInput.addEventListener('change', (e) => {
|
|
if (e.target.files[0]) handleFile(e.target.files[0]);
|
|
});
|
|
|
|
fileDrop.addEventListener('dragover', (e) => {
|
|
e.preventDefault();
|
|
fileDrop.classList.add('dragover');
|
|
});
|
|
|
|
fileDrop.addEventListener('dragleave', () => {
|
|
fileDrop.classList.remove('dragover');
|
|
});
|
|
|
|
fileDrop.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
fileDrop.classList.remove('dragover');
|
|
if (e.dataTransfer.files[0]) handleFile(e.dataTransfer.files[0]);
|
|
});
|
|
|
|
// Demo text
|
|
inputText.value = 'Hello, JustHTMLs!';
|
|
convert();
|
|
</script>
|
|
|
|
<script src="../../assets/clicks.js" defer></script>
|
|
</body>
|
|
</html>
|