broadfield-dev commited on
Commit
5c66633
·
verified ·
1 Parent(s): 73623b8

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +29 -113
index.html CHANGED
@@ -4,156 +4,72 @@
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>API Client for Secure Decoder Plugin</title>
7
- <style>
8
- :root {
9
- --primary-color: #3b82f6;
10
- --primary-hover: #2563eb;
11
- --border-color: #d1d5db;
12
- --background-color: #f9fafb;
13
- --text-color: #1f2937;
14
- --error-color: #ef4444;
15
- --success-color: #22c55e;
16
- }
17
- body {
18
- font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
19
- line-height: 1.6;
20
- max-width: 700px;
21
- margin: 2rem auto;
22
- padding: 0 1rem;
23
- color: var(--text-color);
24
- }
25
- main {
26
- display: flex;
27
- flex-direction: column;
28
- gap: 1.5rem;
29
- border: 1px solid var(--border-color);
30
- padding: 1.5rem;
31
- border-radius: 0.5rem;
32
- background-color: white;
33
- }
34
- h1 {
35
- color: #111827;
36
- text-align: center;
37
- }
38
- section {
39
- display: flex;
40
- flex-direction: column;
41
- gap: 0.5rem;
42
- }
43
- label {
44
- font-weight: 600;
45
- }
46
- input[type="text"], input[type="file"] {
47
- font-size: 1rem;
48
- padding: 0.5rem 0.75rem;
49
- border: 1px solid var(--border-color);
50
- border-radius: 0.375rem;
51
- }
52
- button {
53
- font-size: 1rem;
54
- font-weight: 600;
55
- padding: 0.75rem 1rem;
56
- cursor: pointer;
57
- border: none;
58
- border-radius: 0.375rem;
59
- background-color: var(--primary-color);
60
- color: white;
61
- transition: background-color 0.2s;
62
- }
63
- button:hover:not(:disabled) {
64
- background-color: var(--primary-hover);
65
- }
66
- button:disabled {
67
- background-color: #9ca3af;
68
- cursor: not-allowed;
69
- }
70
- #result-output {
71
- background: var(--background-color);
72
- padding: 1rem;
73
- border-radius: 5px;
74
- white-space: pre-wrap;
75
- word-wrap: break-word;
76
- font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
77
- min-height: 60px;
78
- border: 1px solid var(--border-color);
79
- }
80
- .result-error { color: var(--error-color); }
81
- .result-success { color: var(--success-color); }
82
- </style>
83
  </head>
84
  <body>
85
  <h1>Secure Decoder Plugin Client</h1>
86
- <p style="text-align: center;">Use this page to test your deployed Hugging Face API. It sends an encrypted image to your secure server endpoint for decoding.</p>
87
-
88
  <main>
89
  <section>
90
  <label for="api-url">1. Your Deployed API Endpoint URL:</label>
91
- <input type="text" id="api-url" placeholder="https://broadfield-dev-keylock-rsa-js.static.hf.space/api/decode">
92
  </section>
93
-
94
  <section>
95
- <label for="image-upload">2. Upload Image (Encrypted with Plugin's Public Key):</label>
96
  <input type="file" id="image-upload" accept="image/png">
97
  </section>
98
-
99
  <button id="submit-button">Send to Plugin for Decoding</button>
100
-
101
  <section>
102
- <h3>Result from Server:</h3>
103
  <div id="result-output">Awaiting input...</div>
104
  </section>
105
  </main>
106
-
107
  <script>
108
  const apiUrlInput = document.getElementById('api-url');
109
  const imageInput = document.getElementById('image-upload');
110
  const submitButton = document.getElementById('submit-button');
111
  const resultOutput = document.getElementById('result-output');
 
112
  submitButton.addEventListener('click', async () => {
113
  const file = imageInput.files[0];
114
  const apiUrl = apiUrlInput.value.trim();
115
- // --- Input Validation ---
116
- if (!apiUrl) {
117
- resultOutput.textContent = 'Error: Please provide the API endpoint URL.';
118
- resultOutput.className = 'result-error';
119
- return;
120
- }
121
- if (!file) {
122
- resultOutput.textContent = 'Error: Please select an image file to upload.';
123
  resultOutput.className = 'result-error';
124
  return;
125
  }
126
- // --- UI Feedback for Loading State ---
127
  resultOutput.textContent = 'Uploading and decoding...';
128
  resultOutput.className = '';
129
  submitButton.disabled = true;
130
- submitButton.textContent = 'Processing...';
131
- // --- Prepare and Send the Request ---
132
  const formData = new FormData();
133
- formData.append('authImage', file); // 'authImage' must match the server's expected field name
 
134
  try {
135
- const response = await fetch(apiUrl, {
136
- method: 'POST',
137
- body: formData,
138
- });
139
- const result = await response.json();
140
- if (!response.ok) {
141
- // If the server returns an error (like 400 or 500), throw it
142
- throw new Error(result.error || `Server responded with status ${response.status}`);
143
- }
144
 
145
- // --- Handle Success ---
146
- resultOutput.className = 'result-success';
147
- resultOutput.textContent = `✅ Success! Decoded Data:\n\n${JSON.stringify(result.data, null, 2)}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  } catch (error) {
149
- // --- Handle Failure (Network error or server error) ---
150
  resultOutput.className = 'result-error';
151
  resultOutput.textContent = `❌ API Request Failed:\n\n${error.message}`;
152
- console.error('API Error:', error);
153
  } finally {
154
- // --- Reset UI ---
155
  submitButton.disabled = false;
156
- submitButton.textContent = 'Send to Plugin for Decoding';
157
  }
158
  });
159
  </script>
 
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>API Client for Secure Decoder Plugin</title>
7
+ <style>/* ... same styles as the previous index.html ... */</style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  </head>
9
  <body>
10
  <h1>Secure Decoder Plugin Client</h1>
 
 
11
  <main>
12
  <section>
13
  <label for="api-url">1. Your Deployed API Endpoint URL:</label>
14
+ <input type="text" id="api-url" placeholder="https://your-space-name.hf.space/api/decode">
15
  </section>
 
16
  <section>
17
+ <label for="image-upload">2. Upload Image:</label>
18
  <input type="file" id="image-upload" accept="image/png">
19
  </section>
 
20
  <button id="submit-button">Send to Plugin for Decoding</button>
 
21
  <section>
22
+ <h3>Result:</h3>
23
  <div id="result-output">Awaiting input...</div>
24
  </section>
25
  </main>
 
26
  <script>
27
  const apiUrlInput = document.getElementById('api-url');
28
  const imageInput = document.getElementById('image-upload');
29
  const submitButton = document.getElementById('submit-button');
30
  const resultOutput = document.getElementById('result-output');
31
+
32
  submitButton.addEventListener('click', async () => {
33
  const file = imageInput.files[0];
34
  const apiUrl = apiUrlInput.value.trim();
35
+
36
+ if (!apiUrl || !file) {
37
+ resultOutput.textContent = 'Error: Please provide both an API URL and an image file.';
 
 
 
 
 
38
  resultOutput.className = 'result-error';
39
  return;
40
  }
41
+
42
  resultOutput.textContent = 'Uploading and decoding...';
43
  resultOutput.className = '';
44
  submitButton.disabled = true;
45
+
 
46
  const formData = new FormData();
47
+ formData.append('authImage', file);
48
+
49
  try {
50
+ const response = await fetch(apiUrl, { method: 'POST', body: formData });
 
 
 
 
 
 
 
 
51
 
52
+ // --- BETTER ERROR HANDLING ---
53
+ const contentType = response.headers.get("content-type");
54
+ if (contentType && contentType.indexOf("application/json") !== -1) {
55
+ // It's JSON, process it
56
+ const result = await response.json();
57
+ if (!response.ok) {
58
+ throw new Error(result.error || `Server responded with status ${response.status}`);
59
+ }
60
+ resultOutput.className = 'result-success';
61
+ resultOutput.textContent = `✅ Success! Decoded Data:\n\n${JSON.stringify(result.data, null, 2)}`;
62
+ } else {
63
+ // It's NOT JSON, it's probably an HTML error page from the server
64
+ const textResponse = await response.text();
65
+ throw new Error(`Server did not return JSON. This usually means the API endpoint is wrong or the server crashed. Server response: ${textResponse.substring(0, 100)}...`);
66
+ }
67
+
68
  } catch (error) {
 
69
  resultOutput.className = 'result-error';
70
  resultOutput.textContent = `❌ API Request Failed:\n\n${error.message}`;
 
71
  } finally {
 
72
  submitButton.disabled = false;
 
73
  }
74
  });
75
  </script>