Skip to content

Commit

Permalink
feat: Improve URL shortening logic in htmlBuilder.js; remove unused s…
Browse files Browse the repository at this point in the history
…hort URL handling in index.js
  • Loading branch information
7Sageer committed Jan 10, 2025
1 parent e60f7d2 commit 5f783d3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/htmlBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ const generateForm = () => `

const generateSubscribeLinks = (xrayUrl, singboxUrl, clashUrl, surgeUrl, baseUrl) => `
<div class="mt-5">
<h2 class="mb-4">Your subscribe links:</h2>
${generateLinkInput('Xray Link:', 'xrayLink', xrayUrl)}
${generateLinkInput('Xray Link (Base64):', 'xrayLink', xrayUrl)}
${generateLinkInput('SingBox Link:', 'singboxLink', singboxUrl)}
${generateLinkInput('Clash Link:', 'clashLink', clashUrl)}
${generateLinkInput('Surge Link:', 'surgeLink', surgeUrl)}
<div class="mb-3">
<label for="customShortCode" class="form-label">Custom Path (optional):</label>
<label for="customShortCode" class="form-label">Custom Path (Optional):</label>
<div class="input-group flex-nowrap">
<span class="input-group-text text-truncate" style="max-width: 400px;" title="${baseUrl}/s/">
${baseUrl}/s/
Expand Down Expand Up @@ -274,7 +273,7 @@ const copyToClipboardFunction = () => `
`;

const shortenAllUrlsFunction = () => `
let isShortening = false; // Add flag to track shortening status
let isShortening = false;
async function shortenUrl(url, customShortCode) {
saveCustomPath();
Expand All @@ -287,7 +286,6 @@ const shortenAllUrlsFunction = () => `
}
async function shortenAllUrls() {
// Prevent multiple clicks
if (isShortening) {
return;
}
Expand All @@ -302,7 +300,6 @@ const shortenAllUrlsFunction = () => `
const singboxLink = document.getElementById('singboxLink');
const customShortCode = document.getElementById('customShortCode').value;
// Check if links are already shortened
if (singboxLink.value.includes('/b/')) {
alert('Links are already shortened!');
return;
Expand All @@ -312,10 +309,12 @@ const shortenAllUrlsFunction = () => `
const xrayLink = document.getElementById('xrayLink');
const clashLink = document.getElementById('clashLink');
const surgeLink = document.getElementById('surgeLink');
xrayLink.value = window.location.origin + '/x/' + shortCode;
singboxLink.value = window.location.origin + '/b/' + shortCode;
clashLink.value = window.location.origin + '/c/' + shortCode;
surgeLink.value = window.location.origin + '/s/' + shortCode;
} catch (error) {
console.error('Error:', error);
alert('Failed to shorten URLs. Please try again.');
Expand Down
9 changes: 0 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,6 @@ async function handleRequest(request) {
headers: { 'Content-Type': 'text/plain' }
});

} else if (url.pathname.startsWith('/s/')) {
const shortCode = url.pathname.split('/')[2];
const originalUrl = await SUBLINK_KV.get(shortCode);

if (originalUrl === null) {
return new Response('Short URL not found', { status: 404 });
}

return Response.redirect(originalUrl, 302);
} else if (url.pathname.startsWith('/b/') || url.pathname.startsWith('/c/') || url.pathname.startsWith('/x/') || url.pathname.startsWith('/s/')) {
const shortCode = url.pathname.split('/')[2];
const originalParam = await SUBLINK_KV.get(shortCode);
Expand Down

0 comments on commit 5f783d3

Please sign in to comment.