在线文本提取链接自动转换二维码工具

发布于 1 天前  64 次阅读


本文于 2025年7月25日 11:04 更新,注意查看最新内容

======2025.07.25 23:00 更新======

原有提取链接的正则表达式不够严谨,自己使用时提取出现了问题,现进行修复。

// 使用更精确的正则表达式
const urlRegex = /(https?|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;

参考:https://www.cnblogs.com/speeding/p/5097790.html

======2025.07.25原文======

因为个人需求,经常需要从文本中提取链接转换成二维码使用,于是用AI写了一个小工具。

核心功能:提取文本中的单个链接转换成二维码

线上地址:https://lab.lklog.cn/QRcode/

代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>链接转二维码</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
        }
        h1 {
            margin: 20px 0;
            color: #333;
        }
        textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 24px;
            text-align: center;
            text-decoration: none;
            display: block;
            font-size: 16px;
            margin: 10px auto;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        #qrcode {
            margin: 30px auto;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            display: none;
        }
        .result-url {
            margin-top: 20px;
            word-break: break-all;
            color: #666;
            font-size: 14px;
        }
        .error-message {
            color: red;
            margin-top: 10px;
            display: none;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/qrcode-generator@1.4.4/qrcode.min.js"></script>
</head>
<body>
    <h1>链接转二维码</h1>
    <textarea id="textInput" placeholder="请粘贴包含单个链接的文本"></textarea>
    <button onclick="generateQRCode()">生成二维码</button>
    <div class="error-message" id="errorMessage"></div>
    <div id="qrcode"></div>
    <div class="result-url" id="resultUrl"></div>

    <script>
        function generateQRCode() {
            const textInput = document.getElementById('textInput').value.trim();
            const urlRegex = /(https?:\/\/[^\s]+)/g;
            const matches = textInput.match(urlRegex);
            
            // 隐藏之前的结果和错误信息
            document.getElementById('qrcode').style.display = 'none';
            document.getElementById('resultUrl').textContent = '';
            document.getElementById('errorMessage').style.display = 'none';
            
            if (!textInput) {
                showError('请输入内容');
                return;
            }
            
            if (!matches) {
                showError('未找到有效的HTTP/HTTPS链接');
                return;
            }
            
            if (matches.length > 1) {
                showError('找到多个链接,请只输入一个链接');
                return;
            }
            
            const link = matches[0];
            
            // 显示提取的链接
            document.getElementById('resultUrl').textContent = `提取的链接: ${link}`;
            
            // 生成二维码
            const qr = qrcode(4, 'L');
            qr.addData(link);
            qr.make();
            
            const qrcodeDiv = document.getElementById('qrcode');
            // 调整二维码大小为原来的60%
            qrcodeDiv.innerHTML = qr.createImgTag(6, 0);
            qrcodeDiv.style.display = 'inline-block';
        }
        
        function showError(message) {
            const errorMessage = document.getElementById('errorMessage');
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }
    </script>
</body>
</html>

 


这短短的一生,我们最终都会失去。