mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-11-18 15:50:36 +03:00
Initial work for docs using VitePress
This commit is contained in:
67
docs/v2/api/idata/is.md
Normal file
67
docs/v2/api/idata/is.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# is(value:string):boolean
|
||||
|
||||
This method returns `true` if the passed value matches with the value of one of the properties of current object, `false` otherwise.
|
||||
|
||||
::: info
|
||||
* `device` properties are checked in this particular order: `type`, `model`, `vendor`.
|
||||
* When checking for browser, any `Browser` suffix will be ignored.
|
||||
* When checking for OS, any `OS` suffix will be ignored.
|
||||
* The comparison is case-insensitive, thus `is("firefox") == is("Firefox")`.
|
||||
:::
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
// is() is just a shorthand comparison
|
||||
// so that instead of write it using `==` operator like this:
|
||||
|
||||
const ua = UAParser();
|
||||
const device = ua.device;
|
||||
const os = ua.os;
|
||||
|
||||
if (device.type == "mobile" && os.name != "iOS") {}
|
||||
if (device.type == "smarttv" || device.vendor == "Samsung") {}
|
||||
|
||||
// we can also write the comparison above into as follow:
|
||||
|
||||
if (device.is("mobile") && !os.is("iOS")) {}
|
||||
if (device.is("SmartTV") || device.is("SaMsUnG")) {}
|
||||
```
|
||||
|
||||
```js
|
||||
// Another examples:
|
||||
|
||||
const uap = new UAParser('Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 635) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537');
|
||||
|
||||
uap.getBrowser().name; // "IEMobile"
|
||||
uap.getBrowser().is("IEMobile"); // true
|
||||
uap.getCPU().is("ARM"); // true
|
||||
|
||||
uap.getOS().name; // "Windows Phone"
|
||||
uap.getOS().is("Windows Phone"); // true
|
||||
|
||||
uap.getDevice(); // { vendor: "Nokia", model: "Lumia 635", type: "mobile" }
|
||||
uap.getResult().device; // { vendor: "Nokia", model: "Lumia 635", type: "mobile" }
|
||||
|
||||
const device = uap.getDevice();
|
||||
device.is("mobile"); // true
|
||||
device.is("Lumia 635"); // true
|
||||
device.is("Nokia"); // true
|
||||
device.is("iPhone"); // false
|
||||
uap.getResult().device.is("Nokia"); // true
|
||||
uap.getResult().device.model; // "Lumia 635"
|
||||
|
||||
uap.setUA("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
|
||||
|
||||
const browser = uap.getBrowser();
|
||||
browser.is("IEMobile"); // false
|
||||
browser.is("Chrome"); // true
|
||||
|
||||
uap.getResult().browser.is("Edge"); // false
|
||||
uap.getResult().os.name // "Mac OS"
|
||||
uap.getResult().os.is("Mac OS"); // true
|
||||
uap.getResult().os.version; // "10.6.8"
|
||||
|
||||
const engine = uap.getEngine();
|
||||
engine.is("Blink"); // true
|
||||
```
|
||||
42
docs/v2/api/idata/to-string.md
Normal file
42
docs/v2/api/idata/to-string.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# `toString():string`
|
||||
|
||||
Retrieve full-name values as a string
|
||||
|
||||
::: info
|
||||
Values will be concatenated following this pattern:
|
||||
* browser : `name` + `version`
|
||||
* cpu : `architecture`
|
||||
* device : `vendor` + `model`
|
||||
* engine : `name` + `version`
|
||||
* os : `name` + `version`
|
||||
:::
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
// Usage examples
|
||||
|
||||
let uap = new UAParser('Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 635) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537');
|
||||
|
||||
uap.getDevice(); // {
|
||||
// vendor: "Nokia",
|
||||
// model: "Lumia 635",
|
||||
// type: "mobile"
|
||||
// }
|
||||
uap.getDevice().toString(); // "Nokia Lumia 635"
|
||||
|
||||
uap.getResult().os.name; // "Windows Phone"
|
||||
uap.getResult().os.version; // "8.1"
|
||||
uap.getResult().os.toString(); // "Windows Phone 8.1"
|
||||
|
||||
uap.setUA("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
|
||||
uap.getBrowser().name; // "Chrome"
|
||||
uap.getBrowser().version; // "28.0.1500.95"
|
||||
uap.getBrowser().major; // "28"
|
||||
uap.getBrowser().toString(); // "Chrome 28.0.1500.95"
|
||||
|
||||
let engine = uap.getEngine();
|
||||
engine.name; // "Blink"
|
||||
engine.version; // "28.0.1500.95"
|
||||
engine.toString(); // "Blink 28.0.1500.95"
|
||||
```
|
||||
74
docs/v2/api/idata/with-client-hints.md
Normal file
74
docs/v2/api/idata/with-client-hints.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# `withClientHints():Promise<IData>|Thenable<IData>|IData`
|
||||
|
||||
Recently, Chrome limits the information that exposed through user-agent and introduces a new experimental set of data called ["Client Hints"↗](https://developer.mozilla.org/en-US/docs/Web/API/User-Agent_Client_Hints_API).
|
||||
|
||||
Chrome also sends this client-hints data by default under `Sec-CH-UA-*` HTTP headers in each request, along with the legacy `User-Agent` HTTP header. In server-side development, you can capture this extra information by passing the `req.headers` to `UAParser()` (see examples below).
|
||||
|
||||
In browser-environment, obtaining the client-hints data via JavaScript must be done in an asynchronous way. You can chain the result object from `get*` method with `withClientHints()` to also read the client-hints data from the browser which will return the updated data in a `Promise`.
|
||||
|
||||
::: info
|
||||
In Node.js or in browser-environment without client-hints support (basically anything that's not Chromium-based), `withClientHints()` will return the updated data as a new object instead of as a `Promise`.
|
||||
:::
|
||||
|
||||
## Code Example
|
||||
|
||||
### Client-side Example
|
||||
|
||||
```js
|
||||
(async function () {
|
||||
const ua = new UAParser();
|
||||
|
||||
// get browser data from user-agent only :
|
||||
let browser = ua.getBrowser();
|
||||
console.log('Using User-Agent: ', browser);
|
||||
|
||||
// get browser data from client-hints
|
||||
// (with user-agent as a fallback) :
|
||||
browser = await ua.getBrowser().withClientHints();
|
||||
console.log('Using Client-Hints: ', browser);
|
||||
})();
|
||||
```
|
||||
```js
|
||||
// alternatively without async-await:
|
||||
const ua = new UAParser();
|
||||
|
||||
ua.getBrowser().withClientHints().then(function (browser) {
|
||||
console.log('Using Client-Hints: ', browser);
|
||||
});
|
||||
```
|
||||
|
||||
### Server-side Example
|
||||
|
||||
```js
|
||||
// Suppose we got a request having these HTTP headers:
|
||||
const request = {
|
||||
headers : {
|
||||
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
|
||||
|
||||
'sec-ch-ua-mobile' : '?1',
|
||||
'sec-ch-ua-model' : 'Galaxy S3 Marketing',
|
||||
'sec-ch-ua-platform' : 'Android'
|
||||
}
|
||||
};
|
||||
|
||||
// parse only "user-agent" header
|
||||
const result1 = UAParser(request.headers);
|
||||
|
||||
// also use "sec-ch-ua" headers, in addition to "user-agent"
|
||||
const result2 = UAParser(request.headers).withClientHints();
|
||||
|
||||
console.log(result1.os.name); // "Linux"
|
||||
console.log(result1.device.type); // undefined
|
||||
console.log(result1.device.model); // undefined
|
||||
|
||||
console.log(result2.os.name); // "Android"
|
||||
console.log(result2.device.type); // "mobile"
|
||||
console.log(result2.device.model); // "Galaxy S3 Marketing"
|
||||
|
||||
new UAParser(request.headers)
|
||||
.getBrowser()
|
||||
.withClientHints()
|
||||
.then((browser) => {
|
||||
console.log(browser.toString()); // Chrome 110.0.0.0
|
||||
});
|
||||
```
|
||||
19
docs/v2/api/idata/with-feature-check.md
Normal file
19
docs/v2/api/idata/with-feature-check.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# withFeatureCheck():IData`
|
||||
|
||||
This method allows us to examine other features beyond `navigator.userAgent`. Currently this further improve the detection of the following:
|
||||
|
||||
- browser :
|
||||
- Brave (check for `navigator.isBrave`)
|
||||
- device :
|
||||
- iPad (check for `navigator.standalone` & `navigator.maxTouchPoints`)
|
||||
|
||||
## Code Example
|
||||
|
||||
```js
|
||||
// suppose this code runs on iPad
|
||||
const withoutFeatureCheck = UAParser();
|
||||
const withFeatureCheck = UAParser().withFeatureCheck();
|
||||
|
||||
console.log(withoutFeatureCheck.device); // { vendor : "Apple", model : "Macintosh", type : undefined }
|
||||
console.log(withFeatureCheck.device); // { vendor : "Apple", model : "iPad", type : "tablet" }
|
||||
```
|
||||
Reference in New Issue
Block a user