-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
240 lines (202 loc) · 7.16 KB
/
script.js
File metadata and controls
240 lines (202 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Global variables
let html5QrCode;
let webAppUrl = null;
let isScanning = false;
// DOM Elements
const connectBtn = document.getElementById('connect-btn');
const webappUrlInput = document.getElementById('webapp-url');
const welcomeSection = document.getElementById('welcome-section');
const scannerSection = document.getElementById('scanner-section');
const stopScanBtn = document.getElementById('stop-scan-btn');
const changeSheetBtn = document.getElementById('change-sheet-btn');
const successPage = document.getElementById('success-page');
const alreadyCheckedInPage = document.getElementById('already-checked-in-page');
const notFoundPage = document.getElementById('not-found-page');
// Event Listeners
connectBtn.addEventListener('click', connectToWebApp);
stopScanBtn.addEventListener('click', stopScanner);
changeSheetBtn.addEventListener('click', changeWebApp);
// Load saved Web App URL on page load
window.addEventListener('DOMContentLoaded', () => {
const savedUrl = localStorage.getItem('webAppUrl');
if (savedUrl && webappUrlInput) {
webappUrlInput.value = savedUrl;
}
});
// Show full-screen status page
function showStatusPage(statusType) {
// Hide scanner
scannerSection.style.display = 'none';
// Stop the camera temporarily
if (html5QrCode && html5QrCode.isScanning) {
html5QrCode.pause(true);
}
// Show appropriate status page
if (statusType === 'success') {
successPage.style.display = 'flex';
} else if (statusType === 'already_checked_in') {
alreadyCheckedInPage.style.display = 'flex';
} else if (statusType === 'not_found') {
notFoundPage.style.display = 'flex';
}
// Return to scanner after 4 seconds
setTimeout(() => {
// Hide all status pages
successPage.style.display = 'none';
alreadyCheckedInPage.style.display = 'none';
notFoundPage.style.display = 'none';
// Show scanner again
scannerSection.style.display = 'flex';
// Resume scanning
if (html5QrCode && html5QrCode.isScanning) {
html5QrCode.resume();
}
// Allow next scan
isScanning = false;
}, 4000);
}
// Connect to Web App
async function connectToWebApp() {
const url = webappUrlInput.value.trim();
if (!url) {
alert('Please enter a Web App URL');
return;
}
if (!url.includes('script.google.com/macros')) {
alert('Invalid Google Apps Script Web App URL');
return;
}
webAppUrl = url;
localStorage.setItem('webAppUrl', url);
connectBtn.disabled = true;
connectBtn.innerHTML = '<span class="loading"></span> Connecting...';
try {
// Test connection by trying to read the sheet
const testResult = await fetchSheetData();
if (testResult && testResult.length > 0) {
welcomeSection.style.display = 'none';
scannerSection.style.display = 'flex';
startScanner();
} else {
throw new Error('Cannot fetch data from Web App');
}
} catch (error) {
alert('Failed to connect. Check the Web App URL and deployment settings.');
console.error('Connection error:', error);
webAppUrl = null;
} finally {
connectBtn.disabled = false;
connectBtn.textContent = 'Connect';
}
}
// Fetch data from Google Sheet via Apps Script
async function fetchSheetData() {
if (!webAppUrl) return null;
try {
const response = await fetch(`${webAppUrl}?action=getData`, {
method: 'GET',
redirect: 'follow'
});
if (!response.ok) {
throw new Error('Failed to fetch sheet data');
}
const result = await response.json();
if (result.success && result.data) {
return result.data;
} else {
throw new Error(result.message || 'Failed to retrieve data');
}
} catch (error) {
console.error('Error fetching sheet data:', error);
throw error;
}
}
// Check-in via Google Apps Script Web App
async function checkInViaScript(qrCode) {
if (!webAppUrl) {
throw new Error('WEB_APP_NOT_CONFIGURED');
}
try {
// Use GET request with URL parameters to avoid CORS preflight
const url = `${webAppUrl}?action=checkIn&qrCode=${encodeURIComponent(qrCode)}`;
const response = await fetch(url, {
method: 'GET',
redirect: 'follow'
});
// Get the response text first to see what we're actually receiving
const responseText = await response.text();
console.log('Response text:', responseText);
// Try to parse as JSON
const result = JSON.parse(responseText);
return result;
} catch (error) {
console.error('Detailed error:', error);
console.error('Error type:', error.name);
console.error('Error message:', error.message);
throw error;
}
}
// Process scanned QR code
async function processQRCode(qrCodeData) {
if (isScanning) {
return; // Prevent multiple scans
}
isScanning = true;
try {
// Use Apps Script backend for check-in
const result = await checkInViaScript(qrCodeData);
if (result.success) {
if (result.status === 'checked_in') {
showStatusPage('success');
} else if (result.status === 'already_checked_in') {
showStatusPage('already_checked_in');
} else if (result.status === 'not_registered') {
showStatusPage('not_found');
}
} else {
showStatusPage('not_found');
}
} catch (error) {
console.error('Error processing QR code:', error);
showStatusPage('not_found');
}
}
// Start QR Scanner
function startScanner() {
html5QrCode = new Html5Qrcode("reader");
const config = {
fps: 10,
qrbox: { width: 250, height: 250 },
aspectRatio: 1.0
};
html5QrCode.start(
{ facingMode: "environment" },
config,
(decodedText, decodedResult) => {
processQRCode(decodedText);
},
(errorMessage) => {
// Ignore scan errors (they happen frequently while scanning)
}
).catch((err) => {
console.error('Error starting scanner:', err);
alert('Could not start camera. Please check permissions.');
});
}
// Stop QR Scanner
function stopScanner() {
if (html5QrCode) {
html5QrCode.stop().then(() => {
alert('Scanner stopped');
}).catch((err) => {
console.error('Error stopping scanner:', err);
});
}
}
// Change Web App
function changeWebApp() {
stopScanner();
webAppUrl = null;
scannerSection.style.display = 'none';
welcomeSection.style.display = 'flex';
}