10.4 系统信息
在讲解Chrome扩展时我们提到过获取CPU、内存和存储设备信息的方法,具体可以参见5.4节。Chrome应用也可以获取到系统信息,并且与Chrome扩展类似。
Chrome应用可以获取到的系统信息包括CPU、内存、存储设备、显示器和网卡。要获取信息,需要在Manifest中声明相应权限:
"permissions": [
"system.cpu",
"system.memory",
"system.storage",
"system.display",
"system.network"
]
由于CPU、内存和存储设备相关的内容已经在5.4节讲过了,所以本节只讲解显示器和网卡的内容。
通过chrome.system.display.getInfo
方法可以获取到显示器相关信息:
chrome.system.display.getInfo(function(displayInfoArray){
//do something with displayInfoArray
});
displayInfoArray
是一个包含多个displayInfo
对象的数组。displayInfo
对象的完整结构如下:
{
id: 显示器的唯一id,
name: 显示器名称,如HP LCD monitor,
mirroringSourceId: 镜像的显示器id,目前只支持Chrome OS平台,
isPrimary: 是否为主显示器,
isInternal: 是否为笔记本的自带显示器,
isEnabled: 是否被启用,
dpiX: 显示器水平DPI,
dpiY: 显示器垂直DPI,
rotation: 显示器旋转角度,目前只支持Chrome OS平台,
bounds: {
left: 显示器逻辑范围左上角横坐标,
top: 显示器逻辑范围左上角纵坐标,
width: 显示器逻辑范围像素宽度,
height: 显示器逻辑范围像素高度
},
overscan: {
left: 显示范围距左边框的距离,
top: 显示范围距上边框的距离,
right: 显示范围距右边框的距离,
bottom: 显示范围距下边框的距离
},
workArea: {
left: 工作区范围左上角横坐标,
top: 工作区范围左上角纵坐标,
width: 工作区范围像素宽度,
height: 工作区范围像素高度
}
}
其中overscan
属性目前只在Chrome OS平台有效,workArea
的范围不包括系统占用部分,如任务栏等。
通过chrome.system.display.setDisplayProperties
方法可以更改显示器设置,支持更改的属性包括mirroringSourceId
、isPrimary
、overscan
、rotation
、boundsOriginX
和boundsOriginY
。boundsOriginX
和boundsOriginY
对应于bounds.left
和bounds.top
,即显示器逻辑范围的原点坐标。
chrome.system.display.setDisplayProperties(id, info, function(){
//do something after set display properties
});
通过chrome.system.display.onDisplayChanged
监听显示器设置更改事件:
chrome.system.display.onDisplayChanged.addListener(function(){
//do something after display properties are changed
});
通过chrome.system.network.getNetworkInterfaces
方法可以获取到网卡信息:
chrome.system.network.getNetworkInterfaces(function(networkInterfaces){
//do something with networkInterfaces
console.log(networkInterfaces);
});
networkInterfaces
是一个包含多个networkInterface
对象的数组,networkInterface
对象包含3个属性,分别是name
、address
和prefixLength
。name
为网卡的名称,在*nix系统上通常是eth0或wlan0等。address
为网卡可用的IPv4或IPv6地址。prefixLength
为前缀长度。