Elias207 commited on
Commit
8c06ea5
·
verified ·
1 Parent(s): f286d72

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +28 -41
index.html CHANGED
@@ -3,7 +3,7 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>سازنده و خواننده QR Code</title>
7
  <style>
8
  body {
9
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
@@ -113,7 +113,7 @@
113
  </div>
114
 
115
  <script>
116
- // *** تغییر کلیدی در این خط است ***
117
  // آدرس API صحیح برای اسپیس Elias207/QRcode
118
  const API_URL = "https://elias207-qrcode.hf.space/api/predict/";
119
 
@@ -125,11 +125,6 @@
125
  const generateResultBox = document.getElementById('generate-result-box');
126
  const readResultP = document.getElementById('read-result');
127
 
128
- // تابع برای تولید یک رشته تصادفی برای session_hash
129
- function generateSessionHash() {
130
- return Math.random().toString(36).substring(2, 15);
131
- }
132
-
133
  // تابع برای ساخت QR Code
134
  async function generateQRCode() {
135
  const text = qrTextInput.value.trim();
@@ -141,20 +136,14 @@
141
  generateBtn.disabled = true;
142
  generateResultBox.innerHTML = '<span class="loader">در حال ساخت... لطفاً صبر کنید.</span>';
143
 
144
- const payload = {
145
- "fn_index": 0,
146
- "data": [
147
- text,
148
- null
149
- ],
150
- "session_hash": generateSessionHash()
151
- };
152
-
153
  try {
154
  const response = await fetch(API_URL, {
155
- method: 'POST',
156
- headers: { 'Content-Type': 'application/json' },
157
- body: JSON.stringify(payload)
 
 
 
158
  });
159
 
160
  if (!response.ok) {
@@ -162,9 +151,13 @@
162
  }
163
 
164
  const result = await response.json();
165
- const base64Image = result.data[0];
166
 
167
- generateResultBox.innerHTML = `<img id="qr-image" src="${base64Image}" alt="Generated QR Code">`;
 
 
 
 
168
 
169
  } catch (error) {
170
  console.error('Error generating QR code:', error);
@@ -189,28 +182,22 @@
189
  const reader = new FileReader();
190
  reader.readAsDataURL(file);
191
  reader.onload = async () => {
192
- const base64Image = reader.result;
193
-
194
- const payload = {
195
- "fn_index": 1,
196
- "data": [
197
- null,
198
- base64Image
199
- ],
200
- "session_hash": generateSessionHash()
201
- };
202
 
203
  try {
204
  const response = await fetch(API_URL, {
205
- method: 'POST',
206
- headers: { 'Content-Type': 'application/json' },
207
- body: JSON.stringify(payload)
 
 
 
208
  });
209
 
210
  if (!response.ok) {
211
  throw new Error(`خطای شبکه: ${response.status}`);
212
  }
213
-
214
  const result = await response.json();
215
  const decodedText = result.data[0];
216
 
@@ -219,18 +206,18 @@
219
 
220
  } catch (error) {
221
  console.error('Error reading QR code:', error);
222
- readResultP.textContent = `خطا: ${error.message}`;
223
  readResultP.style.color = 'red';
224
  } finally {
225
  readBtn.disabled = false;
226
  }
227
  };
228
 
229
- reader.onerror = (error) => {
230
- console.error('Error reading file:', error);
231
- readResultP.textContent = 'خطا در خواندن فایل.';
232
- readResultP.style.color = 'red';
233
- readBtn.disabled = false;
234
  };
235
  }
236
  </script>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>سازنده و خواننده QR Code (نسخه اصلاح شده)</title>
7
  <style>
8
  body {
9
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
 
113
  </div>
114
 
115
  <script>
116
+ // *** خط زیر اصلاح شد ***
117
  // آدرس API صحیح برای اسپیس Elias207/QRcode
118
  const API_URL = "https://elias207-qrcode.hf.space/api/predict/";
119
 
 
125
  const generateResultBox = document.getElementById('generate-result-box');
126
  const readResultP = document.getElementById('read-result');
127
 
 
 
 
 
 
128
  // تابع برای ساخت QR Code
129
  async function generateQRCode() {
130
  const text = qrTextInput.value.trim();
 
136
  generateBtn.disabled = true;
137
  generateResultBox.innerHTML = '<span class="loader">در حال ساخت... لطفاً صبر کنید.</span>';
138
 
 
 
 
 
 
 
 
 
 
139
  try {
140
  const response = await fetch(API_URL, {
141
+ method: "POST",
142
+ headers: { "Content-Type": "application/json" },
143
+ body: JSON.stringify({
144
+ "fn_index": 0, // ایندکس تابع ساخت QR
145
+ "data": [text]
146
+ })
147
  });
148
 
149
  if (!response.ok) {
 
151
  }
152
 
153
  const result = await response.json();
154
+ const imageData = result.data[0];
155
 
156
+ if (imageData) {
157
+ generateResultBox.innerHTML = `<img id="qr-image" src="${imageData}" alt="Generated QR Code">`;
158
+ } else {
159
+ throw new Error("پاسخ دریافتی از سرور، شامل تصویر نبود.");
160
+ }
161
 
162
  } catch (error) {
163
  console.error('Error generating QR code:', error);
 
182
  const reader = new FileReader();
183
  reader.readAsDataURL(file);
184
  reader.onload = async () => {
185
+ const fileDataUrl = reader.result;
 
 
 
 
 
 
 
 
 
186
 
187
  try {
188
  const response = await fetch(API_URL, {
189
+ method: "POST",
190
+ headers: { "Content-Type": "application/json" },
191
+ body: JSON.stringify({
192
+ "fn_index": 1, // ایندکس تابع خواندن QR
193
+ "data": [fileDataUrl]
194
+ })
195
  });
196
 
197
  if (!response.ok) {
198
  throw new Error(`خطای شبکه: ${response.status}`);
199
  }
200
+
201
  const result = await response.json();
202
  const decodedText = result.data[0];
203
 
 
206
 
207
  } catch (error) {
208
  console.error('Error reading QR code:', error);
209
+ readResultP.textContent = `${error.message}`;
210
  readResultP.style.color = 'red';
211
  } finally {
212
  readBtn.disabled = false;
213
  }
214
  };
215
 
216
+ reader.onerror = error => {
217
+ console.error("Error converting file to Base64", error);
218
+ readResultP.textContent = "خطا در خواندن فایل.";
219
+ readResultP.style.color = 'red';
220
+ readBtn.disabled = false;
221
  };
222
  }
223
  </script>