From 1d42564ded4eee65f539f1342b76bf127102c2da Mon Sep 17 00:00:00 2001 From: 7Sageer <125936732+7Sageer@users.noreply.github.com> Date: Sat, 23 Nov 2024 20:49:57 +0800 Subject: [PATCH] fix: prevent duplicate link shortening when clicking 'Shorten Links' button multiple times --- README.md | 4 ++-- docs/update-log.md | 5 +++++ src/htmlBuilder.js | 27 ++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 67296bc2..f4840765 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,8 @@ ## 最近更新 -- 2024-11-20 - - 修复sing-box配置初次下载过慢的问题 +- 2024-11-23 + - 修复重复点击生成按钮时,可能导致无法访问短链的问题 [查看更新日志](/docs/update-log.md) diff --git a/docs/update-log.md b/docs/update-log.md index ef23444c..6dfd6483 100644 --- a/docs/update-log.md +++ b/docs/update-log.md @@ -1,5 +1,10 @@ # 更新日志 +## 2024-11-23 + +- Bug修复: + - 重复点击生成按钮时,可能导致无法访问短链 + ## 2024-11-20 - 修复sing-box配置初次下载过慢的问题 diff --git a/src/htmlBuilder.js b/src/htmlBuilder.js index 60c97715..1fe41819 100644 --- a/src/htmlBuilder.js +++ b/src/htmlBuilder.js @@ -871,6 +871,8 @@ const copyToClipboardFunction = () => ` `; const shortenAllUrlsFunction = () => ` + let isShortening = false; // Add flag to track shortening status + async function shortenUrl(url, customShortCode) { saveCustomPath(); const response = await fetch(\`/shorten-v2?url=\${encodeURIComponent(url)}&shortCode=\${encodeURIComponent(customShortCode || '')}\`); @@ -882,18 +884,32 @@ const shortenAllUrlsFunction = () => ` } async function shortenAllUrls() { - const shortenButton = document.querySelector('button[onclick="shortenAllUrls()"]'); - shortenButton.disabled = true; - shortenButton.innerHTML = 'Shortening...'; + // Prevent multiple clicks + if (isShortening) { + return; + } + const shortenButton = document.querySelector('button[onclick="shortenAllUrls()"]'); + try { - const xrayLink = document.getElementById('xrayLink'); + isShortening = true; + shortenButton.disabled = true; + shortenButton.innerHTML = 'Shortening...'; + const singboxLink = document.getElementById('singboxLink'); - const clashLink = document.getElementById('clashLink'); const customShortCode = document.getElementById('customShortCode').value; + // Check if links are already shortened + if (singboxLink.value.includes('/b/')) { + alert('Links are already shortened!'); + return; + } + const shortCode = await shortenUrl(singboxLink.value, customShortCode); + const xrayLink = document.getElementById('xrayLink'); + const clashLink = document.getElementById('clashLink'); + xrayLink.value = window.location.origin + '/x/' + shortCode; singboxLink.value = window.location.origin + '/b/' + shortCode; clashLink.value = window.location.origin + '/c/' + shortCode; @@ -901,6 +917,7 @@ const shortenAllUrlsFunction = () => ` console.error('Error:', error); alert('Failed to shorten URLs. Please try again.'); } finally { + isShortening = false; shortenButton.disabled = false; shortenButton.innerHTML = 'Shorten Links'; }