Skip to content

Commit

Permalink
fix: prevent duplicate link shortening when clicking 'Shorten Links' …
Browse files Browse the repository at this point in the history
…button multiple times
  • Loading branch information
7Sageer committed Nov 23, 2024
1 parent 45855d2 commit 1d42564
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@

## 最近更新

- 2024-11-20
- 修复sing-box配置初次下载过慢的问题
- 2024-11-23
- 修复重复点击生成按钮时,可能导致无法访问短链的问题

[查看更新日志](/docs/update-log.md)

Expand Down
5 changes: 5 additions & 0 deletions docs/update-log.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 更新日志

## 2024-11-23

- Bug修复:
- 重复点击生成按钮时,可能导致无法访问短链

## 2024-11-20

- 修复sing-box配置初次下载过慢的问题
Expand Down
27 changes: 22 additions & 5 deletions src/htmlBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '')}\`);
Expand All @@ -882,25 +884,40 @@ const shortenAllUrlsFunction = () => `
}
async function shortenAllUrls() {
const shortenButton = document.querySelector('button[onclick="shortenAllUrls()"]');
shortenButton.disabled = true;
shortenButton.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>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 = '<i class="fas fa-spinner fa-spin me-2"></i>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;
} catch (error) {
console.error('Error:', error);
alert('Failed to shorten URLs. Please try again.');
} finally {
isShortening = false;
shortenButton.disabled = false;
shortenButton.innerHTML = '<i class="fas fa-compress-alt me-2"></i>Shorten Links';
}
Expand Down

0 comments on commit 1d42564

Please sign in to comment.