mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-11-19 00:42:28 +03:00
Initial work for docs using VitePress
This commit is contained in:
9
docs/v2/intro/quick-start/quick-start.md
Normal file
9
docs/v2/intro/quick-start/quick-start.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Quick Start
|
||||
|
||||
Choose your ~~fighter~~ development strategy:
|
||||
|
||||
- [HTML ↗](/intro/quick-start/using-html)
|
||||
- [Node.js ↗](/intro/quick-start/using-node-js)
|
||||
- [ES Modules ↗](/intro/quick-start/using-es-modules)
|
||||
- [TypeScript ↗](/intro/quick-start/using-typescript)
|
||||
- [jQuery ↗](/intro/quick-start/using-jquery)
|
||||
18
docs/v2/intro/quick-start/using-es-modules.md
Normal file
18
docs/v2/intro/quick-start/using-es-modules.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Using ES Modules
|
||||
|
||||
```sh
|
||||
$ npm install ua-parser-js
|
||||
```
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
import { UAParser } from 'ua-parser-js';
|
||||
|
||||
const { browser, cpu, device } = UAParser('Mozilla/5.0 (X11; U; Linux armv7l; en-GB; rv:1.9.2a1pre) Gecko/20090928 Firefox/3.5 Maemo Browser 1.4.1.22 RX-51 N900');
|
||||
|
||||
console.log(browser.name); // Maemo Browser
|
||||
console.log(cpu.is('arm')); // true
|
||||
console.log(device.is('mobile')); // true
|
||||
console.log(device.model); // N900
|
||||
```
|
||||
78
docs/v2/intro/quick-start/using-html.md
Normal file
78
docs/v2/intro/quick-start/using-html.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# Using HTML
|
||||
---
|
||||
- Download minified file locally from [GitHub ↗](https://github.com/faisalman/ua-parser-js/blob/master/dist/ua-parser.pack.js)
|
||||
- Or use CDN for extra cache performance
|
||||
- [jsDelivr ↗](https://cdn.jsdelivr.net/npm/ua-parser-js/src/ua-parser.min.js)
|
||||
- [cdnjs ↗](https://cdnjs.com/libraries/UAParser.js)
|
||||
|
||||
## Code Example
|
||||
|
||||
```html
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="ua-parser.min.js"></script>
|
||||
<script>
|
||||
|
||||
var uap = new UAParser();
|
||||
console.log(uap.getResult());
|
||||
/*
|
||||
/// This will print an object structured like this:
|
||||
{
|
||||
ua: "",
|
||||
browser: {
|
||||
name: "",
|
||||
version: "",
|
||||
major: ""
|
||||
},
|
||||
engine: {
|
||||
name: "",
|
||||
version: ""
|
||||
},
|
||||
os: {
|
||||
name: "",
|
||||
version: ""
|
||||
},
|
||||
device: {
|
||||
model: "",
|
||||
type: "",
|
||||
vendor: ""
|
||||
},
|
||||
cpu: {
|
||||
architecture: ""
|
||||
}
|
||||
}
|
||||
*/
|
||||
// Default result depends on current window.navigator.userAgent value
|
||||
|
||||
// Now let's try a custom user-agent string as an example
|
||||
var uastring1 = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";
|
||||
uap.setUA(uastring1);
|
||||
var result = uap.getResult();
|
||||
// You can also use UAParser constructor directly without having to create an instance:
|
||||
// var ua = UAParser(uastring1);
|
||||
|
||||
console.log(result.browser); // {name: "Chromium", version: "15.0.874.106"}
|
||||
console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
|
||||
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
|
||||
console.log(result.os.version); // "11.10"
|
||||
console.log(result.engine.name); // "WebKit"
|
||||
console.log(result.cpu.architecture); // "amd64"
|
||||
|
||||
// Do some other tests
|
||||
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
|
||||
console.log(uap.setUA(uastring2).getBrowser().name); // "Konqueror"
|
||||
console.log(uap.getOS()); // {name: "OpenBSD", version: undefined}
|
||||
console.log(uap.getEngine()); // {name: "KHTML", version: "4.1.4"}
|
||||
|
||||
var uastring3 = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
|
||||
console.log(uap.setUA(uastring3).getDevice().model); // "PlayBook"
|
||||
console.log(uap.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
||||
console.log(uap.getBrowser().name); // "Safari"
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
43
docs/v2/intro/quick-start/using-jquery.md
Normal file
43
docs/v2/intro/quick-start/using-jquery.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Using jQuery/Zepto ($.ua)
|
||||
|
||||
Although written in vanilla js, this library will automatically detect if jQuery/Zepto is present and create `$.ua` object (with values based on its User-Agent) along with `window.UAParser` constructor. To get/set user-agent you can use:
|
||||
|
||||
## `$.ua.get():string`
|
||||
|
||||
Get user-agent string
|
||||
|
||||
## `$.ua.set(ua:string)`
|
||||
|
||||
Set user-agent string
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
// Say we are in a browser which has default user-agent:
|
||||
// "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"
|
||||
|
||||
// Get the details
|
||||
console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
|
||||
console.log($.ua.os); // {name: "Android", version: "2.3.4"}
|
||||
console.log($.ua.os.name); // "Android"
|
||||
console.log($.ua.get()); // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"
|
||||
|
||||
// Now lets try to reset to another custom user-agent
|
||||
$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13');
|
||||
|
||||
// Test again
|
||||
console.log($.ua.browser.name); // "Safari"
|
||||
console.log($.ua.engine.name); // "Webkit"
|
||||
console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
|
||||
console.log($.ua.browser.version); // "4.0"
|
||||
console.log($.ua.browser.major); // "4"
|
||||
|
||||
// Add class to <body> tag
|
||||
// <body class="ua-browser-safari ua-devicetype-tablet">
|
||||
$('body')
|
||||
.addClass(
|
||||
'ua-browser-' +
|
||||
$.ua.browser.name +
|
||||
' ua-devicetype-' +
|
||||
$.ua.device.type);
|
||||
```
|
||||
35
docs/v2/intro/quick-start/using-node-js.md
Normal file
35
docs/v2/intro/quick-start/using-node-js.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Using Node.js
|
||||
|
||||
```sh
|
||||
$ npm install ua-parser-js
|
||||
```
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
var http = require('http');
|
||||
var uap = require('ua-parser-js');
|
||||
|
||||
http.createServer(function (req, res) {
|
||||
// get user-agent header
|
||||
var ua = uap(req.headers['user-agent']);
|
||||
|
||||
/* // BEGIN since@2.0 - you can also pass client-hints data to UAParser
|
||||
|
||||
// note: only works in secure context (https:// or localhost or file://)
|
||||
|
||||
var getHighEntropyValues = 'Sec-CH-UA-Full-Version-List, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version, Sec-CH-UA-Arch, Sec-CH-UA-Bitness';
|
||||
res.setHeader('Accept-CH', getHighEntropyValues);
|
||||
res.setHeader('Critical-CH', getHighEntropyValues);
|
||||
|
||||
var ua = uap(req.headers).withClientHints();
|
||||
|
||||
// END since@2.0 */
|
||||
|
||||
// write the result as response
|
||||
res.end(JSON.stringify(ua, null, ' '));
|
||||
})
|
||||
.listen(1337, '127.0.0.1');
|
||||
|
||||
console.log('Server running at http://127.0.0.1:1337/');
|
||||
```
|
||||
19
docs/v2/intro/quick-start/using-typescript.md
Normal file
19
docs/v2/intro/quick-start/using-typescript.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Using TypeScript
|
||||
|
||||
```sh
|
||||
$ npm install --save ua-parser-js
|
||||
# Install ua-parser-js
|
||||
|
||||
$ npm install --save-dev @types/ua-parser-js
|
||||
# Download type definition from DefinitelyTyped repository
|
||||
# https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/ua-parser-js
|
||||
```
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
import { UAParser } from 'ua-parser-js';
|
||||
|
||||
const parser = new UAParser();
|
||||
console.log(parser.getResult());
|
||||
```
|
||||
Reference in New Issue
Block a user