From 1c1931a0377544926450e792e1068b20c4eab3f4 Mon Sep 17 00:00:00 2001
From: 7Sageer <125936732+7Sageer@users.noreply.github.com>
Date: Thu, 3 Oct 2024 15:39:51 +0800
Subject: [PATCH] feat: custom path management
---
README.md | 6 ++--
doc/update-log.md | 4 +++
src/htmlBuilder.js | 79 +++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 81 insertions(+), 8 deletions(-)
diff --git a/README.md b/README.md
index f83c736a..9a0ec46c 100644
--- a/README.md
+++ b/README.md
@@ -36,10 +36,8 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上,小而美的订阅
## 最近更新
-- 2024-09-28
- - ([#41](https://github.com/7Sageer/sublink-worker/pull/41)) (by [@Wikeolf](https://github.com/Wikeolf))
- - 添加自定义域名关键词支持
- - 现在可以决定自定义规则的顺序
+- 2024-10-3
+ - 现在可以保存并管理自定义短链接
[查看更新日志](/doc/update-log.md)
diff --git a/doc/update-log.md b/doc/update-log.md
index 6141e5ca..1d5cc69b 100644
--- a/doc/update-log.md
+++ b/doc/update-log.md
@@ -1,5 +1,9 @@
# 更新日志
+## 2024-10-03
+
+- 现在可以保存并管理自定义短链接
+
## 2024-09-28
- ([#41](https://github.com/7Sageer/sublink-worker/pull/41)) (by [@Wikeolf](https://github.com/Wikeolf))
diff --git a/src/htmlBuilder.js b/src/htmlBuilder.js
index d8f930de..1b5cdaf4 100644
--- a/src/htmlBuilder.js
+++ b/src/htmlBuilder.js
@@ -14,9 +14,13 @@ const generateHead = () => `
-
-
- Sublink Worker - 轻量高效的订阅链接转换工具
+
+
+ Sublink Worker - 轻量高效的订阅转换工具 | 支持V2Ray/Xray、SingBox、Clash
+
+
+
+
@@ -468,10 +472,16 @@ const generateSubscribeLinks = (xrayUrl, singboxUrl, clashUrl, baseUrl) => `
@@ -509,9 +519,61 @@ const generateScripts = () => `
${submitFormFunction()}
${customRuleFunctions}
${generateQRCodeFunction()}
+ ${customPathFunctions()}
`;
+const customPathFunctions = () => `
+ function saveCustomPath() {
+ const customPath = document.getElementById('customShortCode').value;
+ if (customPath) {
+ let savedPaths = JSON.parse(localStorage.getItem('savedCustomPaths') || '[]');
+ if (!savedPaths.includes(customPath)) {
+ savedPaths.push(customPath);
+ localStorage.setItem('savedCustomPaths', JSON.stringify(savedPaths));
+ updateSavedPathsDropdown();
+ }
+ }
+ }
+
+ function updateSavedPathsDropdown() {
+ const savedPaths = JSON.parse(localStorage.getItem('savedCustomPaths') || '[]');
+ const dropdown = document.getElementById('savedCustomPaths');
+ dropdown.innerHTML = '';
+ savedPaths.forEach(path => {
+ const option = document.createElement('option');
+ option.value = path;
+ option.textContent = path;
+ dropdown.appendChild(option);
+ });
+ }
+
+ function loadSavedCustomPath() {
+ const dropdown = document.getElementById('savedCustomPaths');
+ const customShortCode = document.getElementById('customShortCode');
+ if (dropdown.value) {
+ customShortCode.value = dropdown.value;
+ }
+ }
+
+ function deleteSelectedPath() {
+ const dropdown = document.getElementById('savedCustomPaths');
+ const selectedPath = dropdown.value;
+ if (selectedPath) {
+ let savedPaths = JSON.parse(localStorage.getItem('savedCustomPaths') || '[]');
+ savedPaths = savedPaths.filter(path => path !== selectedPath);
+ localStorage.setItem('savedCustomPaths', JSON.stringify(savedPaths));
+ updateSavedPathsDropdown();
+ document.getElementById('customShortCode').value = '';
+ }
+ }
+
+ document.addEventListener('DOMContentLoaded', function() {
+ updateSavedPathsDropdown();
+ document.getElementById('savedCustomPaths').addEventListener('change', loadSavedCustomPath);
+ });
+`;
+
const advancedOptionsToggleFunction = () => `
document.getElementById('advancedToggle').addEventListener('change', function() {
const advancedOptions = document.getElementById('advancedOptions');
@@ -544,6 +606,7 @@ const copyToClipboardFunction = () => `
const shortenAllUrlsFunction = () => `
async function shortenUrl(url, customShortCode) {
+ saveCustomPath();
const response = await fetch(\`/shorten-v2?url=\${encodeURIComponent(url)}&shortCode=\${encodeURIComponent(customShortCode || '')}\`);
if (response.ok) {
const data = await response.text();
@@ -765,6 +828,11 @@ const submitFormFunction = () => `
document.getElementById('advancedOptions').classList.add('show');
}
}
+
+ const savedCustomPath = localStorage.getItem('customPath');
+ if (savedCustomPath) {
+ document.getElementById('customShortCode').value = savedCustomPath;
+ }
loadSelectedRules();
}
@@ -806,6 +874,9 @@ const submitFormFunction = () => `
document.getElementById('predefinedRules').value = 'custom';
document.getElementById('crpinToggle').checked = false;
+ localStorage.removeItem('customPath');
+ document.getElementById('customShortCode').value = '';
+
const subscribeLinksContainer = document.getElementById('subscribeLinksContainer');
subscribeLinksContainer.classList.remove('show');
subscribeLinksContainer.classList.add('hide');