跳到正文

个人记录

xx

yinchenen约 22 分钟
原文
文章信息与版本

文章信息

发布
2026年12月2日
收录
2026年12月2日
更新
2026年12月2日
篇幅
约 22 分钟

版本

  1. v1当前
C++
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>换行转斜杠 · 下载</title>
    <style>
        * {
            box-sizing: border-box;
            font-family: system-ui, 'Segoe UI', Roboto, sans-serif;
        }
        body {
            background: linear-gradient(145deg, #f6f9fc, #e6f0f5);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
            padding: 1.5rem;
        }
        .card {
            background: rgba(255, 255, 255, 0.92);
            backdrop-filter: blur(10px);
            border-radius: 2rem;
            box-shadow: 0 20px 40px -10px rgba(0, 40, 80, 0.25);
            border: 1px solid rgba(255, 255, 255, 0.6);
            padding: 2rem 2rem 2.2rem;
            max-width: 600px;
            width: 100%;
        }
        h1 {
            font-size: 1.7rem;
            font-weight: 600;
            color: #0b2b44;
            margin: 0 0 0.5rem;
            display: flex;
            align-items: center;
            gap: 0.6rem;
        }
        .subhead {
            color: #2a5779;
            background: #f0f7fe;
            border-left: 4px solid #3a8ec9;
            border-radius: 0 8px 8px 0;
            padding: 0.7rem 1rem;
            margin-bottom: 2rem;
            font-size: 0.95rem;
            line-height: 1.5;
        }
        .subhead code {
            background: #d9e8f5;
            padding: 0.15rem 0.5rem;
            border-radius: 20px;
            font-weight: 600;
            color: #0a3149;
            font-size: 0.9rem;
        }
        .file-area {
            display: flex;
            flex-wrap: wrap;
            gap: 1rem;
            align-items: center;
            margin-bottom: 2rem;
            background: #e9f2fa;
            padding: 0.8rem 1.4rem;
            border-radius: 60px;
            border: 1px solid #c5dcee;
        }
        .file-area label {
            background: #ffffff;
            padding: 0.55rem 1.3rem;
            border-radius: 40px;
            font-weight: 500;
            color: #1e5c8b;
            box-shadow: 0 2px 6px rgba(0, 30, 60, 0.08);
            cursor: pointer;
            transition: 0.15s;
            border: 1px solid #b9d3e8;
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 0.95rem;
            white-space: nowrap;
        }
        .file-area label:hover {
            background: #f5faff;
            border-color: #3a8ec9;
            transform: scale(1.01);
        }
        .file-area input[type="file"] {
            display: none;
        }
        #fileName {
            font-size: 0.95rem;
            color: #144a6e;
            background: #d4e6f5;
            padding: 0.4rem 1rem;
            border-radius: 30px;
            flex: 1;
            min-width: 140px;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            font-weight: 500;
            border: 1px solid transparent;
        }
        .btn-group {
            display: flex;
            flex-wrap: wrap;
            gap: 1rem;
        }
        .btn {
            border: none;
            background: white;
            padding: 0.8rem 2rem;
            border-radius: 60px;
            font-weight: 600;
            font-size: 1rem;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            cursor: pointer;
            transition: 0.15s;
            box-shadow: 0 2px 6px rgba(0, 20, 40, 0.06);
            border: 1px solid #c2d7ea;
            color: #1d4b6e;
            flex: 1 1 auto;
        }
        .btn-primary {
            background: #1e5c8b;
            color: white;
            border: 1px solid #1a4e75;
            box-shadow: 0 6px 14px -6px #1e5c8b80;
        }
        .btn-primary:hover:not(:disabled) {
            background: #164a70;
            transform: translateY(-1px);
            box-shadow: 0 10px 18px -8px #0f3a58b0;
        }
        .btn-primary:active:not(:disabled) {
            transform: translateY(1px);
        }
        .btn:disabled {
            opacity: 0.45;
            transform: none;
            pointer-events: none;
            box-shadow: none;
        }
        .footer-note {
            text-align: center;
            color: #537a99;
            font-size: 0.8rem;
            margin-top: 1.8rem;
            opacity: 0.85;
        }
        .status {
            margin-top: 1rem;
            font-size: 0.9rem;
            color: #0e4b73;
            min-height: 1.5rem;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 6px;
            background: #e6f2fb;
            padding: 0.5rem 1rem;
            border-radius: 40px;
            transition: 0.2s;
        }
        .status.success {
            background: #dbf0e3;
            color: #0d5a2c;
        }
        .status.error {
            background: #ffe6e6;
            color: #9e2b2b;
        }
        .icon {
            font-size: 1.2rem;
            line-height: 1;
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>
            <span class="icon">📄</span> 换行转斜杠
        </h1>
        <div class="subhead">
            选择 TXT 文件 → 自动将全部换行替换为 <code>/</code> → 立即下载结果。无预览,一步到位。
        </div>

        <!-- 文件选择 -->
        <div class="file-area">
            <label for="fileInput">
                <span class="icon">📁</span> 选择TXT文件
            </label>
            <input type="file" id="fileInput" accept=".txt,text/plain">
            <div id="fileName">未选择文件</div>
        </div>

        <!-- 操作按钮 -->
        <div class="btn-group">
            <button class="btn btn-primary" id="convertDownloadBtn" disabled>
                <span class="icon">⚡</span> 转换并下载
            </button>
        </div>

        <!-- 状态提示 -->
        <div id="statusMessage" class="status">
            <span class="icon">⏳</span> 等待选择文件...
        </div>

        <div class="footer-note">
            💡 所有操作均在浏览器本地完成,文件不会上传。
        </div>
    </div>

    <script>
        (function() {
            const fileInput = document.getElementById('fileInput');
            const fileNameDiv = document.getElementById('fileName');
            const convertDownloadBtn = document.getElementById('convertDownloadBtn');
            const statusMessage = document.getElementById('statusMessage');

            // 存储文件内容
            let currentFileContent = null;
            let currentFileName = '';

            // 更新状态提示
            function setStatus(text, type = '') {
                statusMessage.innerHTML = text;
                statusMessage.className = 'status' + (type ? ' ' + type : '');
            }

            // 重置状态(保留文件选择?不,完全重置)
            function resetAll() {
                fileInput.value = '';
                fileNameDiv.textContent = '未选择文件';
                currentFileContent = null;
                currentFileName = '';
                convertDownloadBtn.disabled = true;
                setStatus('⏳ 等待选择文件...');
            }

            // 读取文件文本 (Promise)
            function readFileAsText(file) {
                return new Promise((resolve, reject) => {
                    const reader = new FileReader();
                    reader.onload = (e) => resolve(e.target.result);
                    reader.onerror = (e) => reject(new Error('文件读取失败'));
                    reader.readAsText(file, 'UTF-8');
                });
            }

            // 处理文件选择
            async function handleFileSelect(event) {
                const file = event.target.files[0];
                if (!file) {
                    resetAll();
                    return;
                }

                // 检查是否为txt (宽松)
                const isTxt = file.name.toLowerCase().endsWith('.txt') || file.type === 'text/plain';
                if (!isTxt) {
                    alert('⚠️ 请选择 .txt 文件');
                    resetAll();
                    return;
                }

                // 显示文件名(截断)
                let displayName = file.name;
                if (displayName.length > 35) {
                    displayName = displayName.slice(0, 18) + '…' + displayName.slice(-12);
                }
                fileNameDiv.textContent = displayName;
                setStatus('📖 正在读取文件...');

                try {
                    const content = await readFileAsText(file);
                    currentFileContent = content;
                    // 保存文件名(不带扩展名,用于下载)
                    currentFileName = file.name.replace(/\.[^/.]+$/, '') || 'output';
                    convertDownloadBtn.disabled = false;
                    setStatus(`✅ 已加载 “${displayName}” ,点击按钮转换并下载`);
                } catch (error) {
                    alert('❌ 读取文件失败: ' + error.message);
                    resetAll();
                }
            }

            // 核心:转换并下载(一步完成)
            function convertAndDownload() {
                if (currentFileContent === null) {
                    alert('请先选择一个TXT文件');
                    return;
                }

                // 将换行 (\r\n, \r, \n) 全部替换为 "/"
                const converted = currentFileContent.replace(/\r\n|\r|\n/g, '/');

                // 如果转换后内容为空?也可以下载空文件,但给个提示
                if (converted.length === 0) {
                    setStatus('⚠️ 文件内容为空,将下载空文件', 'error');
                } else {
                    setStatus('⬇️ 正在下载转换后的文件...', 'success');
                }

                // 创建 Blob 并下载
                const blob = new Blob([converted], { type: 'text/plain;charset=utf-8' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                // 下载文件名: 原文件名 + "_换行转斜杠.txt"
                const downloadName = (currentFileName || 'file') + '_换行转斜杠.txt';
                a.download = downloadName;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);

                // 短暂延时后更新状态(避免闪烁)
                setTimeout(() => {
                    setStatus(`✅ 已下载 “${downloadName}”`, 'success');
                }, 200);
            }

            // 绑定事件
            fileInput.addEventListener('change', handleFileSelect);

            convertDownloadBtn.addEventListener('click', convertAndDownload);

            // 初始化按钮状态
            convertDownloadBtn.disabled = true;
            setStatus('⏳ 等待选择文件...');

            // 额外:如果用户点击了已选文件的取消按钮,fileInput的change事件可能不会触发,但文件名显示保留?
            // 不用处理太复杂,保持即可。
        })();
    </script>
</body>
</html>
原文