Skip to content

Commit

Permalink
Merge pull request #116 from samuli-hiltunen-somedia/master
Browse files Browse the repository at this point in the history
Added iOS Universal User Agent.
  • Loading branch information
kaimallea authored Apr 11, 2020
2 parents 5fd4896 + 17bd09d commit 14858ae
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* Bryce Neal ([https://github.com/prettymuchbryce](https://github.com/prettymuchbryce))
* Todd Smith-Salter ([https://github.com/ToddSmithSalter](https://github.com/ToddSmithSalter))
* Ma Cheng ([https://github.com/mc-zone](https://github.com/mc-zone))
* Ruslan Ismagilov ([https://github.com/isRuslan](https://github.com/isRuslan))
* Ruslan Ismagilov ([https://github.com/isRuslan](https://github.com/isRuslan))
* Samuli Hiltunen ([https://github.com/samuli-hiltunen-somedia](https://github.com/samuli-hiltunen-somedia))
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ In a browser, the following properties of the global `isMobile` object will eith
- `isMobile.apple.phone`
- `isMobile.apple.ipod`
- `isMobile.apple.tablet`
- `isMobile.apple.universal`
- `isMobile.apple.device` (any mobile Apple device)

### Android devices
Expand Down Expand Up @@ -106,7 +107,7 @@ A real-word example: I include the minified version of the script, inline, and a
<meta charset="utf-8" />
<script>
// Minified version of isMobile included in the HTML since it's small
(function () {var f=/iPhone/i,j=/iPod/i,p=/iPad/i,g=/\bAndroid(?:.+)Mobile\b/i,i=/Android/i,d=/\bAndroid(?:.+)SD4930UR\b/i,e=/\bAndroid(?:.+)(?:KF[A-Z]{2,4})\b/i,c=/Windows Phone/i,h=/\bWindows(?:.+)ARM\b/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/\b(CriOS|Chrome)(?:.+)Mobile/i,o=/Mobile(?:.+)Firefox\b/i;function b($,a){return $.test(a)}function a($){var a=($=$||("undefined"!=typeof navigator?navigator.userAgent:"")).split("[FBAN");void 0!==a[1]&&($=a[0]),void 0!==(a=$.split("Twitter"))[1]&&($=a[0]);var r={apple:{phone:b(f,$)&&!b(c,$),ipod:b(j,$),tablet:!b(f,$)&&b(p,$)&&!b(c,$),device:(b(f,$)||b(j,$)||b(p,$))&&!b(c,$)},amazon:{phone:b(d,$),tablet:!b(d,$)&&b(e,$),device:b(d,$)||b(e,$)},android:{phone:!b(c,$)&&b(d,$)||!b(c,$)&&b(g,$),tablet:!b(c,$)&&!b(d,$)&&!b(g,$)&&(b(e,$)||b(i,$)),device:!b(c,$)&&(b(d,$)||b(e,$)||b(g,$)||b(i,$))||b(/\bokhttp\b/i,$)},windows:{phone:b(c,$),tablet:b(h,$),device:b(c,$)||b(h,$)},other:{blackberry:b(k,$),blackberry10:b(l,$),opera:b(m,$),firefox:b(o,$),chrome:b(n,$),device:b(k,$)||b(l,$)||b(m,$)||b(o,$)||b(n,$)},any:!1,phone:!1,tablet:!1};return r.any=r.apple.device||r.android.device||r.windows.device||r.other.device,r.phone=r.apple.phone||r.android.phone||r.windows.phone,r.tablet=r.apple.tablet||r.android.tablet||r.windows.tablet,r}window.isMobile=a();})();
(function () {var a={};var f=/iPhone/i,h=/iPod/i,i=/iPad/i,r=/\biOS-universal(?:.+)Mac\b/i,g=/\bAndroid(?:.+)Mobile\b/i,j=/Android/i,c=/(?:SD4930UR|\bSilk(?:.+)Mobile\b)/i,d=/Silk/i,b=/Windows Phone/i,k=/\bWindows(?:.+)ARM\b/i,m=/BlackBerry/i,n=/BB10/i,o=/Opera Mini/i,p=/\b(CriOS|Chrome)(?:.+)Mobile/i,q=/Mobile(?:.+)Firefox\b/i;function s(l){return function($){return $.test(l)}}function e(l){var $=(l=l||("undefined"!=typeof navigator?navigator.userAgent:"")).split("[FBAN");void 0!==$[1]&&(l=$[0]),void 0!==($=l.split("Twitter"))[1]&&(l=$[0]);var a=s(l),e={apple:{phone:a(f)&&!a(b),ipod:a(h),tablet:!a(f)&&a(i)&&!a(b),universal:a(r),device:(a(f)||a(h)||a(i))&&!a(b)},amazon:{phone:a(c),tablet:!a(c)&&a(d),device:a(c)||a(d)},android:{phone:!a(b)&&a(c)||!a(b)&&a(g),tablet:!a(b)&&!a(c)&&!a(g)&&(a(d)||a(j)),device:!a(b)&&(a(c)||a(d)||a(g)||a(j))||a(/\bokhttp\b/i)},windows:{phone:a(b),tablet:a(k),device:a(b)||a(k)},other:{blackberry:a(m),blackberry10:a(n),opera:a(o),firefox:a(q),chrome:a(p),device:a(m)||a(n)||a(o)||a(q)||a(p)},any:!1,phone:!1,tablet:!1};return e.any=e.apple.universal||e.apple.device||e.android.device||e.windows.device||e.other.device,e.phone=e.apple.phone||e.android.phone||e.windows.phone,e.tablet=e.apple.tablet||e.android.tablet||e.windows.tablet,e}a=e();if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=a}else if(typeof define==="function"&&define.amd){define(function(){return a})}else{this["isMobile"]=a}})();
// My own arbitrary use of isMobile, as an example
(function() {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/browser.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('E2E Tests', () => {
"ipod": false,
"phone": false,
"tablet": false,
"universal": false,
},
"other": Object {
"blackberry": false,
Expand Down
4 changes: 4 additions & 0 deletions src/isMobile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const appleIphone = /iPhone/i;
const appleIpod = /iPod/i;
const appleTablet = /iPad/i;
const appleUniversal = /\biOS-universal(?:.+)Mac\b/i;
const androidPhone = /\bAndroid(?:.+)Mobile\b/i; // Match 'Android' AND 'Mobile'
const androidTablet = /Android/i;
const amazonPhone = /(?:SD4930UR|\bSilk(?:.+)Mobile\b)/i; // Match 'Silk' AND 'Mobile'
Expand All @@ -22,6 +23,7 @@ export type isMobileResult = {
phone: boolean;
ipod: boolean;
tablet: boolean;
universal: boolean;
device: boolean;
};
amazon: {
Expand Down Expand Up @@ -79,6 +81,7 @@ export default function isMobile(userAgent?: string): isMobileResult {
phone: match(appleIphone) && !match(windowsPhone),
ipod: match(appleIpod),
tablet: !match(appleIphone) && match(appleTablet) && !match(windowsPhone),
universal: match(appleUniversal),
device:
(match(appleIphone) || match(appleIpod) || match(appleTablet)) &&
!match(windowsPhone),
Expand Down Expand Up @@ -129,6 +132,7 @@ export default function isMobile(userAgent?: string): isMobileResult {
};

result.any =
result.apple.universal ||
result.apple.device ||
result.android.device ||
result.windows.device ||
Expand Down
1 change: 1 addition & 0 deletions types/isMobile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export declare type isMobileResult = {
phone: boolean;
ipod: boolean;
tablet: boolean;
universal: boolean;
device: boolean;
};
amazon: {
Expand Down

0 comments on commit 14858ae

Please sign in to comment.