Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: detect windows 11 #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var http = require('http')
, useragent = require('express-useragent');

var srv = http.createServer(function (req, res) {
var source = req.headers['user-agent'],
// to detect windows 11 by header
res.header( 'Accept-CH','Sec-CH-UA-Platform-Version')
var source = req.headers;
ua = useragent.parse(source);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(ua));
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ module.exports = new UserAgent();
module.exports.UserAgent = UserAgent;
module.exports.express = function () {
return function (req, res, next) {
// to detected windows 11 by header
res.header( 'Accept-CH','Sec-CH-UA-Platform-Version');

var source = req.headers['user-agent'] || '';
if (req.headers['x-ucbrowser-ua']) { //special case of UC Browser
source = req.headers['x-ucbrowser-ua'];
}
var ua = new UserAgent();
if (typeof source === 'undefined') {
source = "unknown";
source = 'unknown';
}
ua.Agent.source = source.replace(/^\s*/, '').replace(/\s*$/, '');
ua.Agent.os = ua.getOS(ua.Agent.source);
ua.Agent.os = ua.detectOsByHeader(req.headers,ua.getOS(ua.Agent.source));
ua.Agent.platform = ua.getPlatform(ua.Agent.source);
ua.Agent.browser = ua.getBrowser(ua.Agent.source);
ua.Agent.version = ua.getBrowserVersion(ua.Agent.source);
Expand All @@ -47,4 +50,4 @@ module.exports.express = function () {
}
next();
};
};
};
26 changes: 16 additions & 10 deletions lib/express-useragent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright 2011-2020 Aleksejs Gordejevs
* Licensed under MIT (https://github.com/biggora/express-useragent/blob/master/README.md#license)
*/
(function(exports) {
(function (exports) {
'use strict';

var BOTS = [
Expand Down Expand Up @@ -250,8 +250,8 @@
isSilk: false,
isCaptive: false,
isSmartTV: false,
isUC : false,
isFacebook : false,
isUC: false,
isFacebook: false,
isAlamoFire: false,
isElectron: false,
silkAccelerated: false,
Expand Down Expand Up @@ -373,7 +373,7 @@
break;
case 'Opera':
if (this._Versions.Opera.test(string)) {
return RegExp.$1 ? RegExp.$1: RegExp.$2;
return RegExp.$1 ? RegExp.$1 : RegExp.$2;
}
break;
case 'Firefox':
Expand Down Expand Up @@ -468,7 +468,13 @@

return '';
};
this.detectOsByHeader = function (array, string) {

if (string === 'Windows 10.0' && 'windows' === array['sec-ch-ua-platform'].replaceAll('"', '').toLowerCase() && array['sec-ch-ua-platform-version'] && +array['sec-ch-ua-platform-version'].replaceAll('"', '').split('.')[0] >= 13) {
return 'Windows 11.0';
}
return string;
};
this.getOS = function (string) {
switch (true) {
case this._OS.WindowsVista.test(string):
Expand Down Expand Up @@ -676,7 +682,7 @@
}

if (/Silk-Accelerated=true/gi.test(ua.Agent.source)) {
this.Agent.SilkAccelerated = true;
this.Agent.SilkAccelerated = true;
}
return this.Agent.isSilk ? 'Silk' : false;
};
Expand Down Expand Up @@ -816,7 +822,7 @@

this.testSmartTV = function testBot() {
var ua = this;
ua.Agent.isSmartTV = new RegExp('smart-tv|smarttv|googletv|appletv|hbbtv|pov_tv|netcast.tv','gi').test(ua.Agent.source.toLowerCase());
ua.Agent.isSmartTV = new RegExp('smart-tv|smarttv|googletv|appletv|hbbtv|pov_tv|netcast.tv', 'gi').test(ua.Agent.source.toLowerCase());
};

this.testAndroidTablet = function testAndroidTablet() {
Expand All @@ -837,16 +843,16 @@
this.testWechat = function testWechat() {
var ua = this;

if(/micromessenger/i.test(ua.Agent.source)) {
if (/micromessenger/i.test(ua.Agent.source)) {
ua.Agent.isWechat = true;
ua.Agent.version = this.getWechatVersion(ua.Agent.source);
}
};

this.parse = function parse(source) {
this.parse = function parse(array) {
var ua = new UserAgent();
ua.Agent.source = source.replace(/^\s*/, '').replace(/\s*$/, '');
ua.Agent.os = ua.getOS(ua.Agent.source);
ua.Agent.source = array['user-agent'].replace(/^\s*/, '').replace(/\s*$/, '');
ua.Agent.os = ua.detectOsByHeader(array, ua.getOS(ua.Agent.source));
ua.Agent.platform = ua.getPlatform(ua.Agent.source);
ua.Agent.browser = ua.getBrowser(ua.Agent.source);
ua.Agent.version = ua.getBrowserVersion(ua.Agent.source);
Expand Down
Loading