Update index.html
Browse files- index.html +63 -59
index.html
CHANGED
|
@@ -1,73 +1,77 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
-
|
| 5 |
-
<style>
|
| 6 |
-
body {
|
| 7 |
-
font-family: sans-serif;
|
| 8 |
-
}
|
| 9 |
-
#status {
|
| 10 |
-
margin-top: 10px;
|
| 11 |
-
font-weight: bold;
|
| 12 |
-
}
|
| 13 |
-
#nfcData {
|
| 14 |
-
margin-top: 10px;
|
| 15 |
-
border: 1px solid #ccc;
|
| 16 |
-
padding: 10px;
|
| 17 |
-
white-space: pre-wrap;
|
| 18 |
-
}
|
| 19 |
-
</style>
|
| 20 |
</head>
|
| 21 |
<body>
|
| 22 |
-
<h1>Web NFC Reader</h1>
|
| 23 |
-
<p>Поднесите NFC-карту к устройству.</p>
|
| 24 |
-
<button id="startButton">Начать сканирование NFC</button>
|
| 25 |
-
<p id="status"></p>
|
| 26 |
-
<div id="nfcData"></div>
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
try {
|
| 39 |
-
const ndef = new NDEFReader();
|
| 40 |
-
await ndef.scan();
|
| 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 |
-
} else {
|
| 68 |
-
statusDiv.textContent = 'Web NFC API не поддерживается в вашем браузере.';
|
| 69 |
}
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
</body>
|
| 73 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
+
<title>NFC Reader</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
</head>
|
| 6 |
<body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
<div id="nfc-data"></div>
|
| 9 |
+
|
| 10 |
+
<script>
|
| 11 |
+
const nfcDataDisplay = document.getElementById('nfc-data');
|
| 12 |
|
| 13 |
+
if (!window.NFC) {
|
| 14 |
+
nfcDataDisplay.textContent = "NFC API not supported by your browser.";
|
| 15 |
+
console.error("NFC API not supported.");
|
| 16 |
+
return;
|
| 17 |
+
}
|
| 18 |
|
| 19 |
+
const reader = new window.NFC();
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
reader.onreading = (event) => {
|
| 22 |
+
const tag = event.tag;
|
| 23 |
+
nfcDataDisplay.textContent = ''; // Clear previous data
|
| 24 |
|
| 25 |
+
if (tag) {
|
| 26 |
+
if (tag.ndefMessage) {
|
| 27 |
+
try {
|
| 28 |
+
let dataString = "";
|
| 29 |
|
| 30 |
+
for (let i = 0; i < tag.ndefMessage.length; i++) {
|
| 31 |
+
const record = tag.ndefMessage[i];
|
| 32 |
+
if(record.type === 'application'){
|
| 33 |
+
dataString += "Application record found: " + record.id + "\n";
|
| 34 |
+
}
|
| 35 |
+
if (record.type === 'text') {
|
| 36 |
+
|
| 37 |
+
const text = new TextDecoder('utf-8').decode(record.payload);
|
| 38 |
+
dataString += `Text record: ${text}\n`;
|
| 39 |
+
}
|
| 40 |
+
if (record.type === 'mime') {
|
| 41 |
+
dataString += `MIME record: ${record.mime} ${new TextDecoder('utf-8').decode(record.payload)}\n`;
|
| 42 |
+
}
|
| 43 |
+
if (record.type === 'uri') {
|
| 44 |
+
dataString += `URI record: ${new TextDecoder('utf-8').decode(record.payload)}\n`;
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
+
// Add more cases for other types as needed
|
| 47 |
+
}
|
| 48 |
+
nfcDataDisplay.textContent = dataString || "No readable data.";
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
} catch (error) {
|
| 52 |
+
console.error("Error processing data:", error);
|
| 53 |
+
nfcDataDisplay.textContent = "Error reading data";
|
| 54 |
+
}
|
| 55 |
+
} else {
|
| 56 |
+
nfcDataDisplay.textContent = "No NDEF message found.";
|
| 57 |
+
}
|
| 58 |
+
} else {
|
| 59 |
+
nfcDataDisplay.textContent = "No tag detected.";
|
| 60 |
+
}
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
reader.ondisconnect = () => {
|
| 64 |
+
nfcDataDisplay.textContent = "NFC tag removed.";
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
reader.onerror = (error) => {
|
| 68 |
+
nfcDataDisplay.textContent = "Error: " + error.message;
|
| 69 |
+
console.error("NFC error:", error);
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
reader.start(); // Start listening for NFC tags
|
| 74 |
+
</script>
|
| 75 |
+
|
| 76 |
</body>
|
| 77 |
</html>
|