Merge remote-tracking branch 'origin/main'

main
Lei OT 3 months ago
commit 89e6560b15

@ -52,9 +52,35 @@ const useShortUrlChange = () => {
return { base64Url: '', extracted2: '' };
}
};
const normalizeUrl = (longUrl) => {
const trimmed = longUrl.trim();
// 排除纯数字、纯字母、纯文字(中文、日文等)以及它们的组合
if (/^[\p{L}\p{N}]+$/u.test(trimmed)) {
return '';
}
if (!trimmed) return '';
// 1. 已是 http/https 开头,直接验证合法性
if (/^https?:\/\//i.test(trimmed)) {
try {
const urlObj = new URL(trimmed);
return urlObj.href;
} catch (e) {
return '';
}
}
// 2. www 开头/纯域名/IP+端口,补 https:// 后验证
const fullUrl = `https://${trimmed}`;
try {
const urlObj = new URL(fullUrl);
return urlObj.href;
} catch (e) {
return '';
}
};
const convertUrl = useCallback(async (longUrl) => {
if (!longUrl.trim()) {
const normalizedUrl = normalizeUrl(longUrl);
if (!normalizedUrl) {
setGlobalNotify([{
key: Date.now().toString(),
title: '错误',
@ -64,7 +90,7 @@ const useShortUrlChange = () => {
return null;
}
const { base64Url, extracted2 } = urlBase64(longUrl);
const { base64Url, extracted2 } = urlBase64(normalizedUrl);
if (base64Url) {
const apiUrl = apiPrefix[extracted2] || apiPrefix["chinahighlights.com"];

@ -1,6 +1,6 @@
import { Input, Button, Space, Typography } from 'antd'
import { LinkOutlined } from '@ant-design/icons'
import useShortUrlChange from '@/components/Shorturlchange'
import useShortUrlChange from '@/hooks/useShorturlchange'
import { useState } from 'react'
const { Title, Text, Paragraph } = Typography
@ -25,6 +25,7 @@ function ShorturlConversion() {
placeholder="输入需要转换的长链接"
value={longUrl}
onChange={(e) => setLongUrl(e.target.value)}
onPressEnter={handleConvert}
prefix={<LinkOutlined />}
size="large"
/>
@ -46,9 +47,19 @@ function ShorturlConversion() {
tooltips: ['点击复制', '已复制!'],
} : false}
style={{ fontSize: '17px' }}
>
<a
href={shortUrl}
target="_blank"
rel="noopener noreferrer"
style={{ textDecoration: 'underline' }}
>
{shortUrl}
</a>
</Paragraph>
<Text type="secondary" style={{ fontSize: '14px', marginTop: '4px', display: 'block' }}>
说明使用短链接前可点击测试短链接是否可用
</Text>
</div>)}
</Space>
);

Loading…
Cancel
Save