I need get screen resolution with node.js, but the following code don't work.
我需要使用node.js获取屏幕分辨率,但以下代码不起作用。
var w = screen.width;
var h = screen.height;
This also don't work.
这也行不通。
var w = window.screen.width;
var h = window.screen.height;
Someone know how to get screen resolution with node.js ? Thank you.
有人知道如何使用node.js获得屏幕分辨率吗?谢谢。
4 个解决方案
#1
2
There is now a screenres module via npm:
现在有一个通过npm的screenres模块:
https://www.npmjs.com/package/screenres
https://www.npmjs.com/package/screenres
Currently OS X only, but the author has instructions for adding cross-platform support should anybody be so inclined.
目前仅限OS X,但如果有人愿意,作者会提供添加跨平台支持的说明。
#2
0
You should retrieve the window.screen.width
and window.screen.height
in your client-side JavaScript code and send that information to your server-side Node.js application via AJAX, WebSocket, as form data, etc.
您应该在客户端JavaScript代码中检索window.screen.width和window.screen.height,并通过AJAX,WebSocket,表单数据等将该信息发送到服务器端Node.js应用程序。
For example:
例如:
(function(w) {
$.ajax({
type: 'POST',
url: '/echo/json',
data: {
w: w.screen.width,
h: w.screen.height
},
success: function() {
console.log(arguments);
},
dataType: 'json'
});
})(window);
This is common and is not related to the server-side technology. Doesn't matter what you use at the back-end (Node.js, Python, Ruby, PHP, Java, ASP.NET, etc.), you should send a data from client to server since the server doesn't deal with the user's browser directly.
这很常见,与服务器端技术无关。无论你在后端使用什么(Node.js,Python,Ruby,PHP,Java,ASP.NET等),你应该从客户端发送数据到服务器,因为服务器没有处理用户的浏览器直接。
#3
0
I solved the problem with phantomJS
.
我用phantomJS解决了这个问题。
Install phantomJS
:
安装phantomJS:
sudo apt-get install phantomjs
Create app.js file:
创建app.js文件:
var w = screen.width;
var h = screen.height;
console.log(w+"x"+h);
phantom.exit();
Execute:
执行:
phantomjs app.js
Return:
返回:
1366x768
#4
0
This is quite an overkill, but the node module I found doesn't work anymore and it doesn't handle multiple displays anyway:
这是一个相当的矫枉过正,但我发现的节点模块不再起作用了,它无论如何都不能处理多个显示:
npm install nightmare
And then if you want for exampnle the size and coordinates of the external monitor if available:
然后,如果您想要外部监视器的大小和坐标(如果可用):
const getBounds = async () => {
const nm = new Nightmare({
webPreferences: {
nodeIntegration: true,
},
});
const bounds = nm
.goto('about:blank')
.evaluate(() => {
const electron = require('electron');
const displays = electron.screen.getAllDisplays()
const display = displays.find((d) => d.bounds.x !== 0 || d.bounds.y !== 0) || displays[0];
return display.bounds;
}).end();
return bounds;
};
#1
2
There is now a screenres module via npm:
现在有一个通过npm的screenres模块:
https://www.npmjs.com/package/screenres
https://www.npmjs.com/package/screenres
Currently OS X only, but the author has instructions for adding cross-platform support should anybody be so inclined.
目前仅限OS X,但如果有人愿意,作者会提供添加跨平台支持的说明。
#2
0
You should retrieve the window.screen.width
and window.screen.height
in your client-side JavaScript code and send that information to your server-side Node.js application via AJAX, WebSocket, as form data, etc.
您应该在客户端JavaScript代码中检索window.screen.width和window.screen.height,并通过AJAX,WebSocket,表单数据等将该信息发送到服务器端Node.js应用程序。
For example:
例如:
(function(w) {
$.ajax({
type: 'POST',
url: '/echo/json',
data: {
w: w.screen.width,
h: w.screen.height
},
success: function() {
console.log(arguments);
},
dataType: 'json'
});
})(window);
This is common and is not related to the server-side technology. Doesn't matter what you use at the back-end (Node.js, Python, Ruby, PHP, Java, ASP.NET, etc.), you should send a data from client to server since the server doesn't deal with the user's browser directly.
这很常见,与服务器端技术无关。无论你在后端使用什么(Node.js,Python,Ruby,PHP,Java,ASP.NET等),你应该从客户端发送数据到服务器,因为服务器没有处理用户的浏览器直接。
#3
0
I solved the problem with phantomJS
.
我用phantomJS解决了这个问题。
Install phantomJS
:
安装phantomJS:
sudo apt-get install phantomjs
Create app.js file:
创建app.js文件:
var w = screen.width;
var h = screen.height;
console.log(w+"x"+h);
phantom.exit();
Execute:
执行:
phantomjs app.js
Return:
返回:
1366x768
#4
0
This is quite an overkill, but the node module I found doesn't work anymore and it doesn't handle multiple displays anyway:
这是一个相当的矫枉过正,但我发现的节点模块不再起作用了,它无论如何都不能处理多个显示:
npm install nightmare
And then if you want for exampnle the size and coordinates of the external monitor if available:
然后,如果您想要外部监视器的大小和坐标(如果可用):
const getBounds = async () => {
const nm = new Nightmare({
webPreferences: {
nodeIntegration: true,
},
});
const bounds = nm
.goto('about:blank')
.evaluate(() => {
const electron = require('electron');
const displays = electron.screen.getAllDisplays()
const display = displays.find((d) => d.bounds.x !== 0 || d.bounds.y !== 0) || displays[0];
return display.bounds;
}).end();
return bounds;
};