├── .gitignore ├── .eslintrc.json ├── index.html ├── package.json ├── LICENSE ├── README.md └── detect-browser.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "google", 3 | "env": { 4 | "browser": true 5 | }, 6 | "rules": { 7 | "no-unused-vars": "error", 8 | "no-undef": "error", 9 | "indent": [ 10 | "error", 11 | "tab" 12 | ], 13 | "require-jsdoc": "off", 14 | "no-var": "off", 15 | "no-tabs": "off", 16 | "space-before-function-paren": "off", 17 | "new-cap": "off" 18 | } 19 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | at the top of your page. ",
5 | "main": "detect-browser.js",
6 | "devDependencies": {
7 | "eslint": "^4.18.2",
8 | "eslint-config-google": "^0.9.1"
9 | },
10 | "scripts": {
11 | "test": "mocha"
12 | },
13 | "repository": {
14 | "type": "git",
15 | "url": "git+https://github.com/Ahmdrza/detect-browser.git"
16 | },
17 | "author": "Ahmdrza",
18 | "license": "MIT",
19 | "bugs": {
20 | "url": "https://github.com/Ahmdrza/detect-browser/issues"
21 | },
22 | "homepage": "https://github.com/Ahmdrza/detect-browser#readme"
23 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Ahmad Raza
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # detect-browser
2 | A JavaScript library to detect browser properties like browser name, device, OS, referrer, timezone, screen resolution and much more.
3 | # How to use
4 | Add js file in src folder or wherever you like. in html file where you want to detect browser properties. after adding above code.
7 |
8 | It will print browser info in console
9 |
10 | # List of properties available
11 |
12 | info.language // browser language e.g en-US
13 | info.languages // Gives Array of languages
15 | info.user_agent // Gives User Agent
17 | info.browser // Gives Browser name e.g Chrome
19 | info.device // Gives Device e.g Mobile or Desktop
21 | info.referer // Gives Referer
23 | info.os // Gives User OS e.g windows 10 64-bit
25 | info.online // Gives user online status true or false
27 | info.timezone // Gives User Timezone
29 | info.screen_resolution // Gives Screen Resolution e.g 1366 x 768
31 | info.cookie_enabled // Gives Cookie enabled status true or false
33 |
--------------------------------------------------------------------------------
/detect-browser.js:
--------------------------------------------------------------------------------
1 | // @ts-nocheck
2 | 'use strict';
3 | // detect-browser.js v1.0.0
4 | // Get Browser Data
5 |
6 | // MIT License
7 |
8 | // Copyright (c) 2018 Ahmad Raza
9 |
10 | // Permission is hereby granted, free of charge, to any person obtaining a copy
11 | // of this software and associated documentation files (the "Software"), to deal
12 | // in the Software without restriction, including without limitation the rights
13 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | // copies of the Software, and to permit persons to whom the Software is
15 | // furnished to do so, subject to the following conditions:
16 |
17 | // The above copyright notice and this permission notice shall be included in all
18 | // copies or substantial portions of the Software.
19 |
20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 | // SOFTWARE.
27 |
28 |
29 | function isMobile() {
30 | return /Mobi/.test(navigator.userAgent);
31 | }
32 |
33 | function getBrowserName() {
34 | // Opera 8.0+
35 | if ((window.opr && window.opr.addons)
36 | || window.opera
37 | || navigator.userAgent.indexOf(' OPR/') >= 0) {
38 | return 'Opera';
39 | }
40 |
41 | // Firefox 1.0+
42 | if (typeof InstallTrigger !== 'undefined') {
43 | return 'Firefox';
44 | }
45 |
46 | // Safari 3.0+ "[object HTMLElementConstructor]"
47 | if (/constructor/i.test(window.HTMLElement) || (function (p) {
48 | return p.toString() === '[object SafariRemoteNotification]';
49 | })(!window['safari'])) {
50 | return 'Safari';
51 | }
52 |
53 | // Internet Explorer 6-11
54 | if (/* @cc_on!@*/false || document.documentMode) {
55 | return 'Internet Explorer';
56 | }
57 |
58 | // Edge 20+
59 | if (!(document.documentMode) && window.StyleMedia) {
60 | return 'Microsoft Edge';
61 | }
62 |
63 | // Chrome
64 | if (window.chrome) {
65 | return 'Chrome';
66 | }
67 | }
68 |
69 | function getOSName() {
70 | var os;
71 | if (isMobile()) {
72 | if (/Windows/.test(navigator.userAgent)) {
73 | os = 'Windows';
74 | if (/Phone 8.0/.test(navigator.userAgent)) {
75 | os += ' Phone 8.0';
76 | } else if (/Phone 10.0/.test(navigator.userAgent)) {
77 | os += ' Phone 10.0';
78 | }
79 | } else if (/Android/.test(navigator.userAgent)) {
80 | function androidVersion() {
81 | if (/Android/.test(navigator.appVersion)) {
82 | var v = (navigator.appVersion).match(/Android (\d+).(\d+)/);
83 | return v;
84 | }
85 | }
86 |
87 | var ver = androidVersion();
88 | os = ver[0];
89 | } else if (/iPhone;/.test(navigator.userAgent)) {
90 | function iOSversion() {
91 | if (/iP(hone|od|ad)/.test(navigator.appVersion)) {
92 | var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
93 | return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
94 | }
95 | }
96 |
97 | var ver = iOSversion();
98 | os = 'iOS ' + ver[0] + '.' + ver[1] + '.' + ver[2];
99 | } else if (/iPad;/.test(navigator.userAgent)) {
100 | function iOSversion() {
101 | if (/iP(hone|od|ad)/.test(navigator.appVersion)) {
102 | var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
103 | return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
104 | }
105 | }
106 |
107 | var ver = iOSversion();
108 | os = 'iOS ' + ver[0] + '.' + ver[1] + '.' + ver[2];
109 | } else if (/BBd*/.test(navigator.userAgent)) {
110 | os = 'BlackBerry';
111 | }
112 | } else {
113 | if (/Windows/.test(navigator.userAgent)) {
114 | os = 'Windows';
115 | if (/5.1;/.test(navigator.userAgent)) {
116 | os += ' XP';
117 | } else if (/6.0;/.test(navigator.userAgent)) {
118 | os += ' Vista';
119 | } else if (/6.1;/.test(navigator.userAgent)) {
120 | os += ' 7';
121 | } else if (/6.2/.test(navigator.userAgent)) {
122 | os += ' 8';
123 | } else if (/10.0;/.test(navigator.userAgent)) {
124 | os += ' 10';
125 | }
126 |
127 | if (/64/.test(navigator.userAgent)) {
128 | os += ' 64-bit';
129 | } else {
130 | os += ' 32-bit';
131 | }
132 | } else if (/Macintosh/.test(navigator.userAgent)) {
133 | os = 'Macintosh';
134 | if (/OS X/.test(navigator.userAgent)) {
135 | os += ' OS X';
136 | }
137 | }
138 | }
139 |
140 | return os;
141 | }
142 |
143 | function getBrowser() {
144 | return {
145 | os: getOSName(),
146 | browser: getBrowserName(),
147 | language: navigator.language,
148 | languages: navigator.languages,
149 | user_agent: navigator.userAgent,
150 | device: isMobile() ? 'Mobile' : 'Desktop',
151 | referrer: document.referrer || 'N/A',
152 | online: navigator.onLine,
153 | timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
154 | screen_resolution: screen.width + ' x ' + screen.height,
155 | cookie_enabled: navigator.cookieEnabled,
156 | };
157 | }
158 |
159 |
160 | // var xhr = new XMLHttpRequest();
161 | // xhr.open('GET', 'http://ip-api.com/json');
162 | // xhr.onreadystatechange = function () {
163 | // if (xhr.readyState == 4) {
164 | // if (xhr.status == 200) {
165 | // var IPdata = xhr.responseText;
166 | // jsonResponse = JSON.parse(IPdata);
167 | // }
168 | // }
169 | // };
170 | // xhr.send(null);
171 |
--------------------------------------------------------------------------------