QRcode / index.html
Elias207's picture
Update index.html
6be7f17 verified
raw
history blame
13.9 kB
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>سازنده و خواننده QR Code (نسخه نهایی)</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #f0f2f5;
color: #1c1e21;
display: flex;
justify-content: center;
align-items: flex-start;
padding: 20px;
gap: 20px;
flex-wrap: wrap;
}
.container {
background-color: #ffffff;
padding: 25px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 450px;
display: flex;
flex-direction: column;
gap: 15px;
}
h2 {
text-align: center;
color: #0d6efd;
margin-top: 0;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
}
label, button, input {
font-size: 16px;
}
textarea, input[type="file"] {
width: 100%;
padding: 10px;
border-radius: 6px;
border: 1px solid #ddd;
box-sizing: border-box;
resize: vertical;
}
button {
background-color: #0d6efd;
color: white;
border: none;
padding: 12px 20px;
border-radius: 6px;
cursor: pointer;
transition: background-color 0.3s;
width: 100%;
}
button:hover {
background-color: #0b5ed7;
}
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.result-box {
margin-top: 15px;
padding: 15px;
border: 1px dashed #ccc;
border-radius: 6px;
min-height: 50px;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
position: relative;
}
#qr-image {
max-width: 100%;
height: auto;
border-radius: 4px;
}
.loader {
font-style: italic;
color: #666;
}
#read-result {
word-break: break-all;
white-space: pre-wrap;
}
.copy-btn {
position: absolute;
top: 8px;
left: 8px;
background-color: #6c757d;
color: white;
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
cursor: pointer;
width: auto;
}
.copy-btn:hover {
background-color: #5a6268;
}
/* بخش دیباگ */
.debug-section {
margin-top: 15px;
border: 1px solid #e0e0e0;
border-radius: 6px;
}
.debug-section summary {
padding: 10px;
background-color: #f8f9fa;
cursor: pointer;
font-weight: bold;
}
.debug-content {
padding: 10px;
background-color: #fff;
}
.debug-content h4 {
margin-top: 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.debug-content pre {
background-color: #e9ecef;
padding: 10px;
border-radius: 4px;
white-space: pre-wrap;
word-break: break-all;
font-size: 13px;
max-height: 150px;
overflow-y: auto;
}
.copy-json-btn {
background-color: #198754;
padding: 2px 8px;
font-size: 12px;
width: auto;
border-radius: 4px;
}
</style>
</head>
<body>
<!-- بخش ساخت QR Code -->
<div class="container">
<h2>ساخت QR Code</h2>
<label for="qr-text">متن مورد نظر را وارد کنید:</label>
<textarea id="qr-text" rows="4" placeholder="مثال: https://google.com">سلام دنیا</textarea>
<button id="generate-btn" onclick="generateQRCode()">ساخت QR Code</button>
<div id="generate-result-box" class="result-box">
<span class="loader">تصویر QR Code اینجا نمایش داده می‌شود.</span>
</div>
<!-- بخش دیباگ برای ساخت -->
<details class="debug-section">
<summary>اطلاعات دیباگ (ساخت کد)</summary>
<div class="debug-content">
<h4>
<span>Request Payload (JSON ارسالی)</span>
<button class="copy-json-btn" onclick="copyToClipboard(document.getElementById('generate-req-payload').textContent)">کپی</button>
</h4>
<pre id="generate-req-payload">هنوز درخواستی ارسال نشده است.</pre>
<h4>
<span>Server Response (پاسخ دریافتی)</span>
<button class="copy-json-btn" onclick="copyToClipboard(document.getElementById('generate-res-payload').textContent)">کپی</button>
</h4>
<pre id="generate-res-payload">هنوز پاسخی دریافت نشده است.</pre>
</div>
</details>
</div>
<!-- بخش خواندن QR Code -->
<div class="container">
<h2>خواندن QR Code</h2>
<label for="qr-file">فایل تصویر QR Code را انتخاب کنید:</label>
<input type="file" id="qr-file" accept="image/*">
<button id="read-btn" onclick="readQRCode()">خواندن از تصویر</button>
<div id="read-result-box" class="result-box">
<p id="read-result" class="loader">متن خوانده شده اینجا نمایش داده می‌شود.</p>
</div>
<!-- بخش دیباگ برای خواندن -->
<details class="debug-section">
<summary>اطلاعات دیباگ (خواندن کد)</summary>
<div class="debug-content">
<h4>
<span>Request Payload (JSON ارسالی)</span>
<button class="copy-json-btn" onclick="copyToClipboard(document.getElementById('read-req-payload').textContent)">کپی</button>
</h4>
<pre id="read-req-payload">هنوز درخواستی ارسال نشده است.</pre>
<h4>
<span>Server Response (پاسخ دریافتی)</span>
<button class="copy-json-btn" onclick="copyToClipboard(document.getElementById('read-res-payload').textContent)">کپی</button>
</h4>
<pre id="read-res-payload">هنوز پاسخی دریافت نشده است.</pre>
</div>
</details>
</div>
<script>
// *** خط زیر اصلاح شد ***
// آدرس API صحیح برای اسپیس Elias207/QRcode
const API_URL = "https://elias207-qrcode.hf.space/run/predict/";
// گرفتن ارجاع به عناصر HTML
const generateBtn = document.getElementById('generate-btn');
const readBtn = document.getElementById('read-btn');
const qrTextInput = document.getElementById('qr-text');
const qrFileInput = document.getElementById('qr-file');
const generateResultBox = document.getElementById('generate-result-box');
const readResultBox = document.getElementById('read-result-box');
const readResultP = document.getElementById('read-result');
// عناصر دیباگ
const genReqPayload = document.getElementById('generate-req-payload');
const genResPayload = document.getElementById('generate-res-payload');
const readReqPayload = document.getElementById('read-req-payload');
const readResPayload = document.getElementById('read-res-payload');
// تابع کمکی برای کپی در کلیپ‌بورد
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
alert("متن کپی شد!");
}).catch(err => {
console.error('Failed to copy: ', err);
alert("کپی کردن با خطا مواجه شد.");
});
}
// تابع برای ساخت QR Code
async function generateQRCode() {
const text = qrTextInput.value.trim();
if (!text) {
alert("لطفاً متنی را برای ساخت QR Code وارد کنید.");
return;
}
generateBtn.disabled = true;
generateResultBox.innerHTML = '<span class="loader">در حال ساخت... لطفاً صبر کنید.</span>';
const payload = {
"fn_index": 0,
"data": [text]
};
genReqPayload.textContent = JSON.stringify(payload, null, 2);
genResPayload.textContent = "در حال دریافت پاسخ...";
try {
const response = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
const result = await response.json();
genResPayload.textContent = JSON.stringify(result, null, 2);
if (!response.ok) {
throw new Error(`خطای سرور: ${response.status} - ${result.error || 'Unknown Error'}`);
}
const imageData = result.data[0];
if (imageData) {
generateResultBox.innerHTML = `<img id="qr-image" src="${imageData}" alt="Generated QR Code">`;
} else {
throw new Error("پاسخ دریافتی از سرور، شامل تصویر نبود.");
}
} catch (error) {
console.error('Error generating QR code:', error);
generateResultBox.innerHTML = `<span style="color:red;">خطا: ${error.message}</span>`;
genResPayload.textContent = `Error: ${error.message}`;
} finally {
generateBtn.disabled = false;
}
}
// تابع برای خواندن QR Code
async function readQRCode() {
const file = qrFileInput.files[0];
if (!file) {
alert("لطفاً یک فایل تصویر را انتخاب کنید.");
return;
}
readBtn.disabled = true;
readResultP.textContent = 'در حال خواندن تصویر...';
readResultP.style.color = 'inherit';
readResultP.classList.add('loader');
const oldCopyBtn = readResultBox.querySelector('.copy-btn');
if(oldCopyBtn) oldCopyBtn.remove();
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = async () => {
const fileDataUrl = reader.result;
const payload = {
"fn_index": 1,
"data": [fileDataUrl]
};
readReqPayload.textContent = JSON.stringify({...payload, data: [fileDataUrl.substring(0, 50) + '...'] }, null, 2);
readResPayload.textContent = "در حال دریافت پاسخ...";
try {
const response = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
const result = await response.json();
readResPayload.textContent = JSON.stringify(result, null, 2);
if (!response.ok) {
throw new Error(`خطای سرور: ${response.status} - ${result.error || 'Unknown Error'}`);
}
const decodedText = result.data[0];
readResultP.textContent = decodedText || "متنی در QR Code یافت نشد.";
readResultP.classList.remove('loader');
if (decodedText) {
const copyBtn = document.createElement('button');
copyBtn.textContent = 'کپی متن';
copyBtn.className = 'copy-btn';
copyBtn.onclick = () => copyToClipboard(decodedText);
readResultBox.appendChild(copyBtn);
}
} catch (error) {
console.error('Error reading QR code:', error);
readResultP.textContent = `خطا: ${error.message}`;
readResultP.style.color = 'red';
readResPayload.textContent = `Error: ${error.message}`;
} finally {
readBtn.disabled = false;
}
};
reader.onerror = error => {
console.error("Error converting file to Base64", error);
readResultP.textContent = "خطا در خواندن فایل.";
readResultP.style.color = 'red';
readBtn.disabled = false;
};
}
</script>
</body>
</html>