Skip to content

Commit

Permalink
feat: custom path management
Browse files Browse the repository at this point in the history
  • Loading branch information
7Sageer committed Oct 3, 2024
1 parent f85cb8c commit 1c1931a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions doc/update-log.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
79 changes: 75 additions & 4 deletions src/htmlBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ const generateHead = () => `
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Sublink Worker - 一个强大的订阅链接转换工具,支持多种代理协议和自定义规则">
<meta name="keywords" content="Sublink, Worker, 订阅链接, 代理, Xray, SingBox, Clash, V2Ray, 自定义规则, 在线转换, 机场, 节点">
<title>Sublink Worker - 轻量高效的订阅链接转换工具</title>
<meta name="description" content="Sublink Worker是一款强大的在线订阅链接转换工具,支持V2Ray/Xray、SingBox、Clash等多种客户端,提供自定义规则和高效转换,帮助您轻松管理和优化代理节点。">
<meta name="keywords" content="Sublink, Worker, 订阅链接, 代理, Xray, SingBox, Clash, V2Ray, 自定义规则, 在线, 订阅转换, 机场订阅, 节点管理, 节点解析">
<title>Sublink Worker - 轻量高效的订阅转换工具 | 支持V2Ray/Xray、SingBox、Clash</title>
<meta property="og:title" content="Sublink Worker - 轻量高效的订阅链接转换工具">
<meta property="og:description" content="强大的在线订阅链接转换工具,支持多种代理协议和自定义规则">
<meta property="og:type" content="website">
<meta property="og:url" content="https://sublink-worker.sageer.me/">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/qrcode.min.js"></script>
Expand Down Expand Up @@ -468,10 +472,16 @@ const generateSubscribeLinks = (xrayUrl, singboxUrl, clashUrl, baseUrl) => `
<div class="mb-3">
<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: 200px;" title="${baseUrl}/s/">
<span class="input-group-text text-truncate" style="max-width: 400px;" title="${baseUrl}/s/">
${baseUrl}/s/
</span>
<input type="text" class="form-control" id="customShortCode" placeholder="e.g. my-custom-link">
<select id="savedCustomPaths" class="form-select" style="max-width: 200px;">
<option value="">Saved paths</option>
</select>
<button class="btn btn-outline-danger" type="button" onclick="deleteSelectedPath()">
<i class="fas fa-trash-alt"></i>
</button>
</div>
</div>
<div class="d-grid">
Expand Down Expand Up @@ -509,9 +519,61 @@ const generateScripts = () => `
${submitFormFunction()}
${customRuleFunctions}
${generateQRCodeFunction()}
${customPathFunctions()}
</script>
`;

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 = '<option value="">Saved paths</option>';
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');
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 1c1931a

Please sign in to comment.