Spaces:
Running
Running
fix bug
Browse files- index.html +1 -1
- script.js +35 -13
- style.css +3 -0
index.html
CHANGED
|
@@ -8,7 +8,7 @@
|
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
<h1>NFC 讀卡機</h1>
|
| 11 |
-
<p id="status"
|
| 12 |
<button id="resetButton" style="display: none;">清除記錄</button>
|
| 13 |
<div id="output">
|
| 14 |
<h2>卡片資訊:</h2>
|
|
|
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
<h1>NFC 讀卡機</h1>
|
| 11 |
+
<p id="status">正在初始化...</p>
|
| 12 |
<button id="resetButton" style="display: none;">清除記錄</button>
|
| 13 |
<div id="output">
|
| 14 |
<h2>卡片資訊:</h2>
|
script.js
CHANGED
|
@@ -10,32 +10,48 @@ if (!('NDEFReader' in window)) {
|
|
| 10 |
status.textContent = '您的瀏覽器不支援 Web NFC。請在 Android 版 Chrome 中嘗試。';
|
| 11 |
status.style.color = 'red';
|
| 12 |
} else {
|
| 13 |
-
// 建立 NDEFReader
|
| 14 |
const ndef = new NDEFReader();
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
resetButton.addEventListener('click', () => {
|
| 20 |
log.textContent = '';
|
| 21 |
resetButton.style.display = 'none';
|
| 22 |
});
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try {
|
| 26 |
-
status.textContent = '正在啟動 NFC
|
| 27 |
status.style.color = 'orange';
|
| 28 |
|
| 29 |
await ndef.scan();
|
|
|
|
|
|
|
|
|
|
| 30 |
status.textContent = '請將 NFC 卡片靠近您的裝置';
|
| 31 |
status.style.color = 'green';
|
|
|
|
| 32 |
|
| 33 |
ndef.onreadingerror = () => {
|
| 34 |
status.textContent = '讀取 NFC 卡片時發生錯誤,請重新嘗試';
|
| 35 |
status.style.color = 'red';
|
| 36 |
setTimeout(() => {
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
}, 3000);
|
| 40 |
};
|
| 41 |
|
|
@@ -53,18 +69,18 @@ if (!('NDEFReader' in window)) {
|
|
| 53 |
output += `> 持卡人: ${data.holderName}\n`;
|
| 54 |
output += `> 其他資訊: ${data.otherInfo}\n`;
|
| 55 |
output += '─'.repeat(40) + '\n';
|
| 56 |
-
log.textContent = output + log.textContent;
|
| 57 |
})
|
| 58 |
.catch(error => {
|
| 59 |
console.error('API 錯誤:', error);
|
| 60 |
output += '> 無法從 API 獲取額外資訊。\n';
|
| 61 |
output += '─'.repeat(40) + '\n';
|
| 62 |
-
log.textContent = output + log.textContent;
|
| 63 |
});
|
| 64 |
} else {
|
| 65 |
// 當 useApi 為 false 時,只顯示 UID
|
| 66 |
output += '─'.repeat(40) + '\n';
|
| 67 |
-
log.textContent = output + log.textContent;
|
| 68 |
}
|
| 69 |
|
| 70 |
// 短暫顯示掃描成功狀態
|
|
@@ -78,8 +94,14 @@ if (!('NDEFReader' in window)) {
|
|
| 78 |
}, 2000);
|
| 79 |
};
|
| 80 |
} catch (error) {
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
}
|
| 85 |
}
|
|
|
|
| 10 |
status.textContent = '您的瀏覽器不支援 Web NFC。請在 Android 版 Chrome 中嘗試。';
|
| 11 |
status.style.color = 'red';
|
| 12 |
} else {
|
| 13 |
+
// 建立 NDEFReader 實例
|
| 14 |
const ndef = new NDEFReader();
|
| 15 |
+
let isScanning = false;
|
| 16 |
+
|
| 17 |
+
// 頁面載入後自動嘗試初始化
|
| 18 |
+
initNFC();
|
| 19 |
|
| 20 |
resetButton.addEventListener('click', () => {
|
| 21 |
log.textContent = '';
|
| 22 |
resetButton.style.display = 'none';
|
| 23 |
});
|
| 24 |
|
| 25 |
+
// 如果權限被拒絕,讓用戶點擊頁面任何地方重新嘗試
|
| 26 |
+
document.body.addEventListener('click', () => {
|
| 27 |
+
if (!isScanning) {
|
| 28 |
+
initNFC();
|
| 29 |
+
}
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
async function initNFC() {
|
| 33 |
+
if (isScanning) return;
|
| 34 |
+
|
| 35 |
try {
|
| 36 |
+
status.textContent = '正在啟動 NFC 掃描...';
|
| 37 |
status.style.color = 'orange';
|
| 38 |
|
| 39 |
await ndef.scan();
|
| 40 |
+
|
| 41 |
+
// 成功啟動掃描
|
| 42 |
+
isScanning = true;
|
| 43 |
status.textContent = '請將 NFC 卡片靠近您的裝置';
|
| 44 |
status.style.color = 'green';
|
| 45 |
+
document.body.style.cursor = 'default';
|
| 46 |
|
| 47 |
ndef.onreadingerror = () => {
|
| 48 |
status.textContent = '讀取 NFC 卡片時發生錯誤,請重新嘗試';
|
| 49 |
status.style.color = 'red';
|
| 50 |
setTimeout(() => {
|
| 51 |
+
if (isScanning) {
|
| 52 |
+
status.textContent = '請將 NFC 卡片靠近您的裝置';
|
| 53 |
+
status.style.color = 'green';
|
| 54 |
+
}
|
| 55 |
}, 3000);
|
| 56 |
};
|
| 57 |
|
|
|
|
| 69 |
output += `> 持卡人: ${data.holderName}\n`;
|
| 70 |
output += `> 其他資訊: ${data.otherInfo}\n`;
|
| 71 |
output += '─'.repeat(40) + '\n';
|
| 72 |
+
log.textContent = output + log.textContent;
|
| 73 |
})
|
| 74 |
.catch(error => {
|
| 75 |
console.error('API 錯誤:', error);
|
| 76 |
output += '> 無法從 API 獲取額外資訊。\n';
|
| 77 |
output += '─'.repeat(40) + '\n';
|
| 78 |
+
log.textContent = output + log.textContent;
|
| 79 |
});
|
| 80 |
} else {
|
| 81 |
// 當 useApi 為 false 時,只顯示 UID
|
| 82 |
output += '─'.repeat(40) + '\n';
|
| 83 |
+
log.textContent = output + log.textContent;
|
| 84 |
}
|
| 85 |
|
| 86 |
// 短暫顯示掃描成功狀態
|
|
|
|
| 94 |
}, 2000);
|
| 95 |
};
|
| 96 |
} catch (error) {
|
| 97 |
+
if (error.name === 'NotAllowedError') {
|
| 98 |
+
status.textContent = '⚠️ 需要 NFC 權限才能使用,請點擊頁面任何地方授權';
|
| 99 |
+
status.style.color = 'orange';
|
| 100 |
+
document.body.style.cursor = 'pointer';
|
| 101 |
+
} else {
|
| 102 |
+
status.textContent = `錯誤: ${error.message}`;
|
| 103 |
+
status.style.color = 'red';
|
| 104 |
+
}
|
| 105 |
}
|
| 106 |
}
|
| 107 |
}
|
style.css
CHANGED
|
@@ -6,6 +6,7 @@ body {
|
|
| 6 |
justify-content: center;
|
| 7 |
min-height: 100vh;
|
| 8 |
background-color: #f0f0f0;
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
h1 {
|
|
@@ -21,6 +22,8 @@ h1 {
|
|
| 21 |
border-radius: 5px;
|
| 22 |
background-color: #f8f9fa;
|
| 23 |
border: 2px solid #dee2e6;
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
#resetButton {
|
|
|
|
| 6 |
justify-content: center;
|
| 7 |
min-height: 100vh;
|
| 8 |
background-color: #f0f0f0;
|
| 9 |
+
transition: cursor 0.3s;
|
| 10 |
}
|
| 11 |
|
| 12 |
h1 {
|
|
|
|
| 22 |
border-radius: 5px;
|
| 23 |
background-color: #f8f9fa;
|
| 24 |
border: 2px solid #dee2e6;
|
| 25 |
+
text-align: center;
|
| 26 |
+
max-width: 500px;
|
| 27 |
}
|
| 28 |
|
| 29 |
#resetButton {
|