我想在我的网页中显示客户端系统的电池状态和小部件中的时钟如果它是笔记本电脑.
如果是台式机,我不想显示电池状态.
时钟小部件工作正常.
此外,我可以通过使用获取电池详细信息navigator.getBattery().
但如果它是桌面,我不想显示小部件.
那么,如何使用JavaScript检测客户端是使用桌面还是笔记本电脑?
以下是内容,navigator但没有详细信息来检测它是笔记本电脑还是台式机.
console.log(navigator);Run Code Online (Sandbox Code Playgroud)
{
"vendorSub": "",
"productSub": "20030107",
"vendor": "Google Inc.",
"maxTouchPoints": 0,
"hardwareConcurrency": 4,
"appCodeName": "Mozilla",
"appName": "Netscape",
"appVersion": "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"platform": "Win32",
"product": "Gecko",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"language": "en-GB",
"languages": [
"en-US",
"en"
],
"onLine": true,
"cookieEnabled": true,
"doNotTrack": null,
"geolocation": {
"getCurrentPosition": function getCurrentPosition() { [native code] },
"watchPosition": function watchPosition() { [native code] },
"clearWatch": function clearWatch() { [native code] }
},
"mediaDevices": {
"enumerateDevices": function enumerateDevices() { [native code] },
"getSupportedConstraints": function getSupportedConstraints() { [native code] },
"getUserMedia": function getUserMedia() { [native code] },
"addEventListener": function addEventListener() { [native code] },
"removeEventListener": function removeEventListener() { [native code] },
"dispatchEvent": function dispatchEvent() { [native code] }
},
"plugins": {
"0": [object Plugin],
"1": [object Plugin],
"2": [object Plugin],
"3": [object Plugin],
"4": [object Plugin],
"length": 5,
"item": function item() { [native code] },
"namedItem": function namedItem() { [native code] },
"refresh": function refresh() { [native code] }
},
"mimeTypes": {
"0": [object MimeType],
"1": [object MimeType],
"2": [object MimeType],
"3": [object MimeType],
"4": [object MimeType],
"5": [object MimeType],
"6": [object MimeType],
"length": 7,
"item": function item() { [native code] },
"namedItem": function namedItem() { [native code] }
},
"webkitTemporaryStorage": {
"queryUsageAndQuota": /**id:16**/ function queryUsageAndQuota() { [native code] },
"requestQuota": /**id:17**/ function requestQuota() { [native code] }
},
"webkitPersistentStorage": {
"queryUsageAndQuota": /**ref:16**/,
"requestQuota": /**ref:17**/
},
"serviceWorker": /**error accessing property**/,
"getBattery": function getBattery() { [native code] },
"sendBeacon": function sendBeacon() { [native code] },
"requestMediaKeySystemAccess": function requestMediaKeySystemAccess() { [native code] },
"getGamepads": function getGamepads() { [native code] },
"webkitGetUserMedia": function webkitGetUserMedia() { [native code] },
"javaEnabled": function javaEnabled() { [native code] },
"vibrate": function vibrate() { [native code] },
"requestMIDIAccess": function requestMIDIAccess() { [native code] },
"credentials": {
"get": function get() { [native code] },
"store": function store() { [native code] },
"requireUserMediation": function requireUserMediation() { [native code] }
},
"storage": {
"persisted": function persisted() { [native code] },
"persist": function () { [native code] }
},
"permissions": {
"query": function query() { [native code] }
},
"presentation": {
"defaultRequest": null
},
"getUserMedia": function getUserMedia() { [native code] },
"registerProtocolHandler": function registerProtocolHandler() { [native code] },
"unregisterProtocolHandler": function unregisterProtocolHandler() { [native code] }
}
Run Code Online (Sandbox Code Playgroud)
如评论中所述 - 可以从Battery API 获得充电状态(真/假)battery.charging和充电时间(以秒为单位)battery.chargingTime
在桌面上充电总是true充电,但充电时间总是如此0
这允许我们确定它是桌面
这是一个快速的片段来测试桌面是否......
navigator.getBattery().then(function(battery) {
if (battery.charging && battery.chargingTime === 0) {
console.log("I'm a desktop")
} else {
console.log("I'm not a desktop")
}
});Run Code Online (Sandbox Code Playgroud)
注意
这不是一门精确的科学.正如@MatheusAvellar指出的那样.我的猜测是,如果你是完全充电,虽然battery.charging可能是真的,battery.chargingTime然后可能会去0 - 没有访问笔记本电脑尝试