|
| 1 | +(function () { |
| 2 | + /* ---- Elements ---- */ |
| 3 | + var telIntegrity = document.getElementById('telIntegrity'); |
| 4 | + var telEngine = document.getElementById('telEngine'); |
| 5 | + var telTime = document.getElementById('telTime'); |
| 6 | + var dOS = document.getElementById('dOS'); |
| 7 | + var dBrowser = document.getElementById('dBrowser'); |
| 8 | + var dCPU = document.getElementById('dCPU'); |
| 9 | + var dScreen = document.getElementById('dScreen'); |
| 10 | + var dViewport = document.getElementById('dViewport'); |
| 11 | + var dDPR = document.getElementById('dDPR'); |
| 12 | + var dColor = document.getElementById('dColor'); |
| 13 | + var dOrientation = document.getElementById('dOrientation'); |
| 14 | + var bLevel = document.getElementById('bLevel'); |
| 15 | + var bFill = document.getElementById('bFill'); |
| 16 | + var bCharging = document.getElementById('bCharging'); |
| 17 | + var bDischarge = document.getElementById('bDischarge'); |
| 18 | + var dNetType = document.getElementById('dNetType'); |
| 19 | + var dNetSpeed = document.getElementById('dNetSpeed'); |
| 20 | + var dArch = document.getElementById('dArch'); |
| 21 | + var refreshBtn = document.getElementById('refreshBtn'); |
| 22 | + var copyBtn = document.getElementById('copyBtn'); |
| 23 | + var toast = document.getElementById('toast'); |
| 24 | + |
| 25 | + /* ---- Clock ---- */ |
| 26 | + function updateClock() { |
| 27 | + telTime.textContent = new Date().toLocaleTimeString(); |
| 28 | + } |
| 29 | + |
| 30 | + /* ---- UA parsing ---- */ |
| 31 | + function parseOS(ua) { |
| 32 | + ua = ua.toLowerCase(); |
| 33 | + if (ua.indexOf('windows nt 10') !== -1) return 'Windows 10/11'; |
| 34 | + if (ua.indexOf('windows nt 6.3') !== -1) return 'Windows 8.1'; |
| 35 | + if (ua.indexOf('windows nt 6.1') !== -1) return 'Windows 7'; |
| 36 | + if (ua.indexOf('windows') !== -1) return 'Windows'; |
| 37 | + if (ua.indexOf('mac os x') !== -1) { |
| 38 | + var m = ua.match(/mac os x ([\d_]+)/); |
| 39 | + return 'macOS ' + (m ? m[1].replace(/_/g, '.') : ''); |
| 40 | + } |
| 41 | + if (ua.indexOf('android') !== -1) return 'Android'; |
| 42 | + if (ua.indexOf('linux') !== -1) return 'Linux'; |
| 43 | + if (ua.indexOf('iphone') !== -1 || ua.indexOf('ipad') !== -1) return 'iOS'; |
| 44 | + return 'Unknown'; |
| 45 | + } |
| 46 | + |
| 47 | + function parseBrowser(ua) { |
| 48 | + ua = ua.toLowerCase(); |
| 49 | + if (ua.indexOf('edg/') !== -1 || ua.indexOf('edge/') !== -1) return 'Edge'; |
| 50 | + if (ua.indexOf('opr/') !== -1 || ua.indexOf('opera') !== -1) return 'Opera'; |
| 51 | + if (ua.indexOf('chrome/') !== -1 && ua.indexOf('safari') !== -1) return 'Chrome'; |
| 52 | + if (ua.indexOf('firefox/') !== -1) return 'Firefox'; |
| 53 | + if (ua.indexOf('safari/') !== -1 && ua.indexOf('chrome') === -1) return 'Safari'; |
| 54 | + return 'Unknown'; |
| 55 | + } |
| 56 | + |
| 57 | + function getEngine() { |
| 58 | + var ua = navigator.userAgent.toLowerCase(); |
| 59 | + if (ua.indexOf('edg/') !== -1) return 'EdgeHTML/Blink'; |
| 60 | + if (ua.indexOf('chrome/') !== -1) return 'Blink/V8'; |
| 61 | + if (ua.indexOf('firefox/') !== -1) return 'Gecko/SpiderMonkey'; |
| 62 | + if (ua.indexOf('safari/') !== -1 && ua.indexOf('chrome') === -1) return 'WebKit/JavaScriptCore'; |
| 63 | + return 'Unknown'; |
| 64 | + } |
| 65 | + |
| 66 | + function getArch() { |
| 67 | + var ua = navigator.userAgent.toLowerCase(); |
| 68 | + if (ua.indexOf('x64') !== -1 || ua.indexOf('wow64') !== -1) return 'x86-64'; |
| 69 | + if (ua.indexOf('arm64') !== -1 || ua.indexOf('aarch64') !== -1) return 'ARM64'; |
| 70 | + if (ua.indexOf('arm') !== -1) return 'ARM'; |
| 71 | + if (navigator.platform) { |
| 72 | + if (navigator.platform.indexOf('Win64') !== -1) return 'x86-64'; |
| 73 | + if (navigator.platform.indexOf('Win32') !== -1) return 'x86'; |
| 74 | + } |
| 75 | + return 'Unknown'; |
| 76 | + } |
| 77 | + |
| 78 | + /* ---- Screen ---- */ |
| 79 | + function updateScreen() { |
| 80 | + dScreen.textContent = screen.width + '\u00D7' + screen.height; |
| 81 | + dViewport.textContent = window.innerWidth + '\u00D7' + window.innerHeight; |
| 82 | + dDPR.textContent = window.devicePixelRatio.toFixed(2); |
| 83 | + dColor.textContent = screen.colorDepth + '-bit'; |
| 84 | + var orient = screen.orientation ? screen.orientation.type : (window.innerWidth > window.innerHeight ? 'landscape' : 'portrait'); |
| 85 | + dOrientation.textContent = orient; |
| 86 | + } |
| 87 | + |
| 88 | + /* ---- Battery ---- */ |
| 89 | + function setupBattery() { |
| 90 | + if (!navigator.getBattery) { |
| 91 | + bLevel.textContent = 'N/A'; bFill.style.width = '0%'; |
| 92 | + bCharging.textContent = 'API unavailable'; return; |
| 93 | + } |
| 94 | + navigator.getBattery().then(function (bat) { |
| 95 | + function update() { |
| 96 | + var lvl = Math.round(bat.level * 100); |
| 97 | + bLevel.textContent = lvl + '%'; |
| 98 | + bFill.style.width = lvl + '%'; |
| 99 | + bFill.style.background = lvl < 20 ? '#ef4444' : lvl < 50 ? '#f59e0b' : '#10b981'; |
| 100 | + bCharging.textContent = bat.charging ? '\u26A1 Charging' : '\uD83D\uDD0B Discharging'; |
| 101 | + bDischarge.textContent = bat.charging |
| 102 | + ? (bat.chargingTime === Infinity ? 'calculating\u2026' : Math.round(bat.chargingTime / 60) + ' min to full') |
| 103 | + : (bat.dischargingTime === Infinity ? 'not discharging' : Math.round(bat.dischargingTime / 60) + ' min remaining'); |
| 104 | + } |
| 105 | + update(); |
| 106 | + bat.addEventListener('chargingchange', update); |
| 107 | + bat.addEventListener('levelchange', update); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + /* ---- Network ---- */ |
| 112 | + function updateNetwork() { |
| 113 | + var conn = navigator.connection || navigator.mozConnection || navigator.webkitConnection; |
| 114 | + if (conn) { |
| 115 | + dNetType.textContent = conn.effectiveType ? conn.effectiveType.toUpperCase() : 'Unknown'; |
| 116 | + dNetSpeed.textContent = (conn.downlink || '?') + ' Mbps'; |
| 117 | + conn.addEventListener('change', function () { |
| 118 | + dNetType.textContent = conn.effectiveType ? conn.effectiveType.toUpperCase() : 'Unknown'; |
| 119 | + dNetSpeed.textContent = (conn.downlink || '?') + ' Mbps'; |
| 120 | + }); |
| 121 | + } else { |
| 122 | + dNetType.textContent = 'API unavailable'; |
| 123 | + dNetSpeed.textContent = ''; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /* ---- Full probe ---- */ |
| 128 | + function probe() { |
| 129 | + var ua = navigator.userAgent; |
| 130 | + dOS.textContent = parseOS(ua); |
| 131 | + dBrowser.textContent = parseBrowser(ua); |
| 132 | + dCPU.textContent = (navigator.hardwareConcurrency || '?') + ' logical cores'; |
| 133 | + dArch.textContent = getArch(); |
| 134 | + telEngine.textContent = getEngine(); |
| 135 | + updateScreen(); |
| 136 | + updateNetwork(); |
| 137 | + updateClock(); |
| 138 | + telIntegrity.textContent = 'Nominal'; |
| 139 | + telIntegrity.className = 'tel-value status-ok'; |
| 140 | + } |
| 141 | + |
| 142 | + /* ---- Copy JSON ---- */ |
| 143 | + function copyDigest() { |
| 144 | + var digest = { |
| 145 | + os: dOS.textContent, |
| 146 | + browser: dBrowser.textContent, |
| 147 | + engine: telEngine.textContent, |
| 148 | + cpu: dCPU.textContent, |
| 149 | + architecture: dArch.textContent, |
| 150 | + screen: dScreen.textContent, |
| 151 | + viewport: dViewport.textContent, |
| 152 | + pixelRatio: dDPR.textContent, |
| 153 | + colorDepth: dColor.textContent, |
| 154 | + orientation: dOrientation.textContent, |
| 155 | + battery: bLevel.textContent, |
| 156 | + charging: bCharging.textContent, |
| 157 | + networkType: dNetType.textContent, |
| 158 | + networkSpeed: dNetSpeed.textContent, |
| 159 | + timestamp: new Date().toISOString(), |
| 160 | + }; |
| 161 | + |
| 162 | + var text = JSON.stringify(digest, null, 2); |
| 163 | + if (navigator.clipboard && navigator.clipboard.writeText) { |
| 164 | + navigator.clipboard.writeText(text).then(function () { |
| 165 | + showToast('\u2705 Diagnostic JSON copied to clipboard'); |
| 166 | + }).catch(function () { |
| 167 | + fallbackCopy(text); |
| 168 | + }); |
| 169 | + } else { |
| 170 | + fallbackCopy(text); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + function fallbackCopy(text) { |
| 175 | + var ta = document.createElement('textarea'); |
| 176 | + ta.value = text; ta.style.position = 'fixed'; ta.style.opacity = '0'; |
| 177 | + document.body.appendChild(ta); ta.select(); |
| 178 | + try { document.execCommand('copy'); showToast('\u2705 Diagnostic JSON copied to clipboard'); } catch (e) { showToast('\u274C Failed to copy'); } |
| 179 | + document.body.removeChild(ta); |
| 180 | + } |
| 181 | + |
| 182 | + function showToast(msg) { |
| 183 | + toast.textContent = msg; |
| 184 | + toast.className = 'show'; |
| 185 | + setTimeout(function () { toast.className = 'hidden'; }, 2500); |
| 186 | + } |
| 187 | + |
| 188 | + /* ---- Events ---- */ |
| 189 | + window.addEventListener('resize', function () { |
| 190 | + updateScreen(); |
| 191 | + /* subtle flash: add/remove a class */ |
| 192 | + dViewport.style.transition = 'color 0.15s'; |
| 193 | + dViewport.style.color = '#ff2a5f'; |
| 194 | + setTimeout(function () { dViewport.style.color = ''; }, 200); |
| 195 | + }); |
| 196 | + |
| 197 | + window.addEventListener('orientationchange', function () { |
| 198 | + setTimeout(updateScreen, 300); |
| 199 | + }); |
| 200 | + |
| 201 | + refreshBtn.addEventListener('click', function () { |
| 202 | + probe(); |
| 203 | + setupBattery(); |
| 204 | + }); |
| 205 | + |
| 206 | + copyBtn.addEventListener('click', copyDigest); |
| 207 | + |
| 208 | + /* ---- Boot ---- */ |
| 209 | + probe(); |
| 210 | + setupBattery(); |
| 211 | + setInterval(updateClock, 1000); |
| 212 | +})(); |
0 commit comments