413 lines
13 KiB
HTML
413 lines
13 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>URL 编解码 · 功能工具箱</title>
|
|
<link rel="icon" href="assets/favicon.svg" type="image/svg+xml" />
|
|
<link rel="stylesheet" href="assets/styles.css" />
|
|
<script src="assets/common.js"></script>
|
|
<style>
|
|
.toolbar-label-inline {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
font-size: 0.78rem;
|
|
color: var(--text-secondary);
|
|
font-weight: 550;
|
|
}
|
|
|
|
.toolbar-label-inline select {
|
|
width: auto;
|
|
min-width: 150px;
|
|
padding: 5px 30px 5px 9px;
|
|
font-size: 0.78rem;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.editor-head .head-right {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.editor-head .copy-btn {
|
|
padding: 3px 8px;
|
|
font-size: 0.7rem;
|
|
}
|
|
|
|
#urlOutput {
|
|
white-space: pre-wrap;
|
|
word-break: break-all;
|
|
}
|
|
|
|
.status-error {
|
|
color: var(--danger);
|
|
}
|
|
|
|
#errorBox {
|
|
flex: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="editor-app">
|
|
<header class="tool-header">
|
|
<div class="tool-icon">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<path d="M10.5 13.5a5 5 0 0 0 7.1 0l2-2a5 5 0 0 0-7.1-7.1l-1.1 1.1" />
|
|
<path d="M13.5 10.5a5 5 0 0 0-7.1 0l-2 2a5 5 0 0 0 7.1 7.1l1.1-1.1" />
|
|
</svg>
|
|
</div>
|
|
<div>
|
|
<h1>URL 编解码</h1>
|
|
<p>在 URL、表单、Base64 与原文之间快速转换,输入即时处理。</p>
|
|
</div>
|
|
<div class="header-actions">
|
|
<button
|
|
class="btn btn--icon btn--ghost"
|
|
data-theme-toggle
|
|
title="切换主题"
|
|
aria-label="切换主题"
|
|
>
|
|
<svg class="icon-moon" viewBox="0 0 24 24" aria-hidden="true">
|
|
<path d="M21 12.8A8.5 8.5 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z" />
|
|
</svg>
|
|
<svg class="icon-sun" viewBox="0 0 24 24" aria-hidden="true">
|
|
<circle cx="12" cy="12" r="4" />
|
|
<path
|
|
d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="editor-toolbar">
|
|
<div class="toolbar-group">
|
|
<div class="segmented" role="radiogroup" aria-label="编码或解码">
|
|
<label class="seg">
|
|
<input type="radio" name="mode" value="encode" checked />
|
|
<span>编码</span>
|
|
</label>
|
|
<label class="seg">
|
|
<input type="radio" name="mode" value="decode" />
|
|
<span>解码</span>
|
|
</label>
|
|
</div>
|
|
|
|
<label class="toolbar-label-inline" for="encodingType">
|
|
类型
|
|
<select id="encodingType">
|
|
<option value="component" selected>URIComponent</option>
|
|
<option value="full">完整 URL</option>
|
|
<option value="form">表单编码</option>
|
|
<option value="base64">Base64</option>
|
|
</select>
|
|
</label>
|
|
</div>
|
|
|
|
<span class="spacer"></span>
|
|
|
|
<div class="toolbar-group">
|
|
<button class="btn" id="swapBtn" title="把结果放回输入并切换模式">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<path d="M7 3 3 7l4 4M3 7h13a4 4 0 0 1 4 4M17 21l4-4-4-4M21 17H8a4 4 0 0 1-4-4" />
|
|
</svg>
|
|
交换
|
|
</button>
|
|
<button class="btn btn--soft" id="copyBtn">复制结果</button>
|
|
<button class="btn btn--ghost" id="clearBtn">清空</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="alert alert--error hidden" id="errorBox" role="alert">
|
|
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
<circle cx="12" cy="12" r="9" />
|
|
<path d="M12 8v5M12 16.5v.01" />
|
|
</svg>
|
|
<span id="errorText"></span>
|
|
</div>
|
|
|
|
<div class="editor-split">
|
|
<section class="editor-pane">
|
|
<div class="editor-head">
|
|
<span id="inputTitle">原文</span>
|
|
<span class="count" id="inputCount">0 字符</span>
|
|
</div>
|
|
<div class="editor-body">
|
|
<textarea
|
|
id="urlInput"
|
|
class="editor-textarea"
|
|
wrap="off"
|
|
spellcheck="false"
|
|
placeholder="在此输入要编码或解码的文本,例如:
|
|
https://example.com/search?q=测试参数&lang=zh-CN"
|
|
></textarea>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="editor-pane">
|
|
<div class="editor-head">
|
|
<span id="outputTitle">编码结果</span>
|
|
<span class="head-right">
|
|
<button class="copy-btn" id="copyOutputBtn">复制</button>
|
|
<span class="count" id="outputCount">0 字符</span>
|
|
</span>
|
|
</div>
|
|
<div class="editor-body">
|
|
<pre class="code-output" id="urlOutput"><span class="placeholder">结果将显示在这里…</span></pre>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
|
|
<div class="editor-status">
|
|
<span class="status-text" id="status">就绪</span>
|
|
<span class="spacer"></span>
|
|
<span>
|
|
<span class="kbd">Ctrl/⌘</span> + <span class="kbd">Enter</span>
|
|
处理 ·
|
|
<span class="kbd">Ctrl/⌘</span> + <span class="kbd">Shift</span> +
|
|
<span class="kbd">Enter</span> 切换模式
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
(function () {
|
|
"use strict";
|
|
|
|
var input = document.getElementById("urlInput");
|
|
var output = document.getElementById("urlOutput");
|
|
var inputCount = document.getElementById("inputCount");
|
|
var outputCount = document.getElementById("outputCount");
|
|
var status = document.getElementById("status");
|
|
var errorBox = document.getElementById("errorBox");
|
|
var errorText = document.getElementById("errorText");
|
|
var typeSelect = document.getElementById("encodingType");
|
|
var inputTitle = document.getElementById("inputTitle");
|
|
var outputTitle = document.getElementById("outputTitle");
|
|
|
|
var EMPTY_OUTPUT = '<span class="placeholder">结果将显示在这里…</span>';
|
|
|
|
var TYPE_NAMES = {
|
|
component: "URIComponent",
|
|
full: "完整 URL",
|
|
form: "表单编码",
|
|
base64: "Base64",
|
|
};
|
|
|
|
function currentMode() {
|
|
return document.querySelector('input[name="mode"]:checked').value;
|
|
}
|
|
|
|
function setStatus(text, isError) {
|
|
status.textContent = text;
|
|
status.classList.toggle("status-error", !!isError);
|
|
}
|
|
|
|
function showError(message) {
|
|
errorText.textContent = message;
|
|
errorBox.classList.remove("hidden");
|
|
}
|
|
|
|
function clearError() {
|
|
errorBox.classList.add("hidden");
|
|
}
|
|
|
|
function updateCounts() {
|
|
inputCount.textContent = input.value.length + " 字符";
|
|
var isEmpty = !!output.querySelector(".placeholder");
|
|
outputCount.textContent =
|
|
(isEmpty ? 0 : output.textContent.length) + " 字符";
|
|
}
|
|
|
|
function updateLabels() {
|
|
var encoding = currentMode() === "encode";
|
|
inputTitle.textContent = encoding ? "原文" : "待解码内容";
|
|
outputTitle.textContent = encoding ? "编码结果" : "解码结果";
|
|
}
|
|
|
|
/* ---------- Base64(支持中文) ---------- */
|
|
|
|
function encodeBase64(text) {
|
|
var bytes = new TextEncoder().encode(text);
|
|
var binary = "";
|
|
bytes.forEach(function (byte) {
|
|
binary += String.fromCharCode(byte);
|
|
});
|
|
return btoa(binary);
|
|
}
|
|
|
|
function decodeBase64(text) {
|
|
var binary = atob(text.replace(/\s+/g, ""));
|
|
var bytes = new Uint8Array(binary.length);
|
|
for (var i = 0; i < binary.length; i += 1) {
|
|
bytes[i] = binary.charCodeAt(i);
|
|
}
|
|
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
}
|
|
|
|
function formEncode(text) {
|
|
return encodeURIComponent(text).replace(/%20/g, "+");
|
|
}
|
|
|
|
function formDecode(text) {
|
|
return decodeURIComponent(text.replace(/\+/g, "%20"));
|
|
}
|
|
|
|
function processInput(options) {
|
|
options = options || {};
|
|
var raw = input.value;
|
|
var type = typeSelect.value;
|
|
var mode = currentMode();
|
|
|
|
updateLabels();
|
|
|
|
if (!raw) {
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
clearError();
|
|
setStatus("就绪");
|
|
updateCounts();
|
|
return;
|
|
}
|
|
|
|
// 首尾空白通常不是 URL 内容的一部分
|
|
var text = raw.trim();
|
|
var result;
|
|
|
|
try {
|
|
if (mode === "encode") {
|
|
if (type === "component") result = encodeURIComponent(text);
|
|
else if (type === "full") result = encodeURI(text);
|
|
else if (type === "form") result = formEncode(text);
|
|
else result = encodeBase64(text);
|
|
} else {
|
|
if (type === "component") result = decodeURIComponent(text);
|
|
else if (type === "full") result = decodeURI(text);
|
|
else if (type === "form") result = formDecode(text);
|
|
else result = decodeBase64(text);
|
|
}
|
|
} catch (error) {
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
showError(
|
|
(mode === "encode" ? "编码失败:" : "解码失败:") +
|
|
(error && error.message ? error.message : "输入内容格式不正确。"),
|
|
);
|
|
setStatus("处理失败", true);
|
|
updateCounts();
|
|
return;
|
|
}
|
|
|
|
output.textContent = result;
|
|
clearError();
|
|
setStatus(
|
|
(mode === "encode" ? "已编码" : "已解码") +
|
|
" · " +
|
|
TYPE_NAMES[type] +
|
|
" · " +
|
|
result.length +
|
|
" 字符",
|
|
);
|
|
updateCounts();
|
|
|
|
if (options.notify) {
|
|
Toolbox.toast(
|
|
mode === "encode" ? "编码完成" : "解码完成",
|
|
"success",
|
|
);
|
|
}
|
|
}
|
|
|
|
var autoProcess = Toolbox.debounce(processInput, 260);
|
|
|
|
input.addEventListener("input", function () {
|
|
updateCounts();
|
|
autoProcess();
|
|
});
|
|
|
|
document
|
|
.querySelectorAll('input[name="mode"]')
|
|
.forEach(function (radio) {
|
|
radio.addEventListener("change", function () {
|
|
processInput({ notify: false });
|
|
});
|
|
});
|
|
|
|
typeSelect.addEventListener("change", function () {
|
|
processInput({ notify: false });
|
|
});
|
|
|
|
function copyResult() {
|
|
var text = output.textContent;
|
|
if (!text || text === "结果将显示在这里…") {
|
|
Toolbox.toast("没有可复制的内容", "error");
|
|
return;
|
|
}
|
|
Toolbox.copyWithToast(text, "结果已复制");
|
|
}
|
|
|
|
document.getElementById("copyBtn").addEventListener("click", copyResult);
|
|
document
|
|
.getElementById("copyOutputBtn")
|
|
.addEventListener("click", copyResult);
|
|
|
|
document.getElementById("clearBtn").addEventListener("click", function () {
|
|
input.value = "";
|
|
output.innerHTML = EMPTY_OUTPUT;
|
|
clearError();
|
|
setStatus("就绪");
|
|
updateCounts();
|
|
input.focus();
|
|
});
|
|
|
|
document.getElementById("swapBtn").addEventListener("click", function () {
|
|
var text = output.textContent;
|
|
if (!text || text === "结果将显示在这里…") {
|
|
Toolbox.toast("还没有可交换的结果", "error");
|
|
return;
|
|
}
|
|
input.value = text;
|
|
document.querySelector(
|
|
'input[name="mode"][value="' +
|
|
(currentMode() === "encode" ? "decode" : "encode") +
|
|
'"]',
|
|
).checked = true;
|
|
processInput({ notify: false });
|
|
});
|
|
|
|
document.addEventListener("keydown", function (event) {
|
|
var modifier = event.metaKey || event.ctrlKey;
|
|
|
|
if (modifier && event.shiftKey && event.key === "Enter") {
|
|
event.preventDefault();
|
|
document.querySelector(
|
|
'input[name="mode"][value="' +
|
|
(currentMode() === "encode" ? "decode" : "encode") +
|
|
'"]',
|
|
).checked = true;
|
|
processInput({ notify: true });
|
|
return;
|
|
}
|
|
|
|
if (modifier && event.key === "Enter") {
|
|
event.preventDefault();
|
|
processInput({ notify: true });
|
|
return;
|
|
}
|
|
|
|
if (modifier && event.shiftKey && event.key.toLowerCase() === "c") {
|
|
event.preventDefault();
|
|
copyResult();
|
|
}
|
|
});
|
|
|
|
// 初始化
|
|
updateLabels();
|
|
updateCounts();
|
|
input.focus();
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|