-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathauthenticity.js
More file actions
103 lines (88 loc) · 3.43 KB
/
Copy pathauthenticity.js
File metadata and controls
103 lines (88 loc) · 3.43 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
// Authenticity Web NFC Logic
/* global NDEFReader */
document.addEventListener('DOMContentLoaded', () => {
const startScanBtn = document.getElementById('start-scan-btn');
const nfcStatus = document.getElementById('nfc-status');
const nfcMessage = document.getElementById('nfc-message');
const nfcUnsupported = document.getElementById('nfc-unsupported');
const authResult = document.getElementById('auth-result');
const resProductId = document.getElementById('res-product-id');
const resDate = document.getElementById('res-date');
// Feature detection
if (!('NDEFReader' in window)) {
startScanBtn.disabled = true;
startScanBtn.classList.add('hidden');
nfcUnsupported.classList.remove('hidden');
nfcStatus.classList.add('hidden');
return;
}
startScanBtn.addEventListener('click', async () => {
try {
// Create a new NDEFReader instance
const ndef = new NDEFReader();
// Update UI for scanning state
startScanBtn.innerHTML =
'<i class="ri-loader-4-line fa-spin"></i> Scanning...';
startScanBtn.disabled = true;
nfcStatus.classList.add('scanning');
nfcMessage.textContent =
'Scanning... Please hold your device near the garment tag.';
// Start scanning
await ndef.scan();
// Listen for reading errors
ndef.addEventListener('readingerror', () => {
nfcMessage.textContent = 'Error reading tag. Please try again.';
nfcMessage.style.color = 'var(--auth-error)';
setTimeout(() => resetScanningUI(), 3000);
});
// Handle successful reading
ndef.addEventListener('reading', ({ message, serialNumber }) => {
let payload = 'Unknown';
// Extract data from the first record if available
if (message.records && message.records.length > 0) {
const record = message.records[0];
if (record.recordType === 'text') {
const textDecoder = new TextDecoder(record.encoding || 'utf-8');
payload = textDecoder.decode(record.data);
} else if (record.recordType === 'url') {
const textDecoder = new TextDecoder();
payload = textDecoder.decode(record.data);
}
}
showSuccessResult(serialNumber, payload);
});
} catch (error) {
console.error('Error starting NFC scan:', error);
nfcMessage.textContent =
'Permission denied or NFC error: ' + error.message;
nfcMessage.style.color = 'var(--auth-error)';
resetScanningUI();
}
});
function resetScanningUI() {
startScanBtn.innerHTML = '<i class="ri-scan-2-line"></i> Start Scan';
startScanBtn.disabled = false;
nfcStatus.classList.remove('scanning');
}
function showSuccessResult(serialNumber, payload) {
// Hide scanning UI
nfcStatus.classList.add('hidden');
startScanBtn.classList.add('hidden');
// Populate data
// Fallback to random if serial is missing, simulating a tag
const id =
serialNumber ||
'CR-' + Math.random().toString(36).substring(2, 10).toUpperCase();
resProductId.textContent = id;
// Set a random recent date for manufacture just for demo purposes
const date = new Date();
date.setMonth(date.getMonth() - Math.floor(Math.random() * 6));
resDate.textContent = date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
// Show result
authResult.classList.remove('hidden');
}
});