Skip to content

Commit

Permalink
feat: add QR Code Generating for subscribe links
Browse files Browse the repository at this point in the history
  • Loading branch information
7Sageer committed Sep 10, 2024
1 parent 4d24151 commit 1f86f48
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转

- 2024-09-10
- ([#25](https://github.com/7Sageer/sublink-worker/issues/25)) 修复了Base64无法转换多个HTTP链接的问题
- 现在为生成的链接提供二维码

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

Expand Down
1 change: 1 addition & 0 deletions doc/update-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2024-09-10

- ([#25](https://github.com/7Sageer/sublink-worker/issues/25)) 修复了Base64无法转换多个HTTP的问题
- 现在为生成的链接提供二维码

## 2024-09-09

Expand Down
115 changes: 114 additions & 1 deletion src/htmlBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const generateHead = () => `
<title>Sublink Worker</title>
<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>
<style>
${generateStyles()}
</style>
Expand Down Expand Up @@ -320,6 +321,52 @@ const generateStyles = () => `
margin-right: 10px;
}
.qr-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
z-index: 1000;
}
.qr-modal.show {
opacity: 1;
visibility: visible;
}
.qr-card {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
transform: scale(0.9);
transition: transform 0.3s ease;
}
.qr-modal.show .qr-card {
transform: scale(1);
}
.qr-card img {
max-width: 100%;
height: auto;
}
.qr-card p {
margin-top: 10px;
color: #333;
font-size: 16px;
}
`;

const generateBody = (xrayUrl, singboxUrl, clashUrl) => `
Expand Down Expand Up @@ -406,6 +453,9 @@ const generateLinkInput = (label, id, value) => `
<button class="btn btn-outline-secondary" type="button" onclick="copyToClipboard('${id}')">
<i class="fas fa-copy"></i>
</button>
<button class="btn btn-outline-secondary" type="button" onclick="generateQRCode('${id}')">
<i class="fas fa-qrcode"></i>
</button>
</div>
</div>
`;
Expand All @@ -420,6 +470,7 @@ const generateScripts = () => `
${tooltipFunction()}
${submitFormFunction()}
${customRuleFunctions}
${generateQRCodeFunction()}
</script>
`;

Expand Down Expand Up @@ -763,4 +814,66 @@ const customRuleFunctions = `
customRuleCount--;
}
}
`;
`;

const generateQRCodeFunction = () => `
function generateQRCode(id) {
const input = document.getElementById(id);
const text = input.value;
if (!text) {
alert('No link provided!');
return;
}
try {
const qr = qrcode(0, 'M');
qr.addData(text);
qr.make();
const moduleCount = qr.getModuleCount();
const cellSize = Math.max(2, Math.min(8, Math.floor(300 / moduleCount)));
const margin = Math.floor(cellSize * 0.5);
const qrImage = qr.createDataURL(cellSize, margin);
const modal = document.createElement('div');
modal.className = 'qr-modal';
modal.innerHTML = \`
<div class="qr-card">
<img src="\${qrImage}" alt="QR Code">
<p>Scan QR Code</p>
</div>
\`;
document.body.appendChild(modal);
modal.addEventListener('click', (e) => {
if (e.target === modal) {
closeQRModal();
}
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeQRModal();
}
});
requestAnimationFrame(() => {
modal.classList.add('show');
});
} catch (error) {
console.error('Error in generating:', error);
alert('Try to use short links!');
}
}
function closeQRModal() {
const modal = document.querySelector('.qr-modal');
if (modal) {
modal.classList.remove('show');
modal.addEventListener('transitionend', () => {
document.body.removeChild(modal);
}, { once: true });
}
}
`;
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ async function handleRequest(request) {
const text = await response.text();
let decodedText;
decodedText = decodeBase64(text.trim());
console.log(decodedText);
// Check if the decoded text needs URL decoding
if (decodedText.includes('%')) {
decodedText = decodeURIComponent(decodedText);
Expand Down

0 comments on commit 1f86f48

Please sign in to comment.