2017-03-04 147 views
4

我想告诉客户系统的电池状态,并在我的网页时钟在小部件如果它是一台笔记本电脑
如果是桌面,我不想显示电池状态。
时钟部件工作正常。检测客户端系统是否是笔记本或台式机使用JavaScript

另外,我可以通过使用navigator.getBattery()获取电池详细信息。
但是,如果它是桌面,我不想显示小部件。

那么,如何检测使用JavaScript的客户端使用桌面还是笔记本电脑

以下是navigator的内容,但没有详细信息检测它是笔记本电脑还是台式机。

console.log(navigator);

{ 
    "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] } 
} 
+1

您可以使用[电池API](https://developer.mozilla .org/en-US/docs/Web/API/Battery_Status_API)来获取状态,但无法判断该设备是台式机还是笔记本电脑。 – Pointy

+4

没有办法将设备识别为笔记本电脑/台式机。 – BenM

+4

@Pointy - '从Firefox 52开始,电池状态API仅在Chrome /特权代码中可用 –

回答

7

正如在评论中提到 - 有可能获得来自电池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") 
 
    } 
 
});

注意

这是不是一门精确的科学。正如@MatheusAvellar指出的那样。我的猜测是,如果你充满电虽然battery.charging可能是真的,battery.chargingTime然后可能会去0 - 无法访问笔记本电脑来尝试

+0

非常感谢哥哥 –

+0

高兴它帮助队友 –

+0

这是很好的指出他不会_always_工作。我使用100%充电的笔记本电脑,代码表示我是台式机。无论你想要做什么,请记住,它不会总是准确的。 –

相关问题