Skip to content

Commit

Permalink
feat: add device detection for meta in app browser (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravishekhar authored Aug 1, 2024
1 parent a802a67 commit d9c3900
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ export function isFacebookWebView(ua?: string = getUserAgent()): boolean {
return /FBAN/.test(ua) || /FBAV/.test(ua);
}

export function isInstagramWebView(ua?: string = getUserAgent()): boolean {
return /Instagram/.test(ua);
}

export function isMetaWebView(ua?: string = getUserAgent()): boolean {
return isFacebookWebView(ua) || isInstagramWebView(ua);
}

export function isMetaInAppBrowser(ua?: string = getUserAgent()): boolean {
return /IABMV\/1/.test(ua);
}

export function isFirefox(ua?: string = getUserAgent()): boolean {
return /Firefox/i.test(ua);
}
Expand Down
50 changes: 50 additions & 0 deletions test/tests/device/isInstagramWebView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* @flow */

import { isInstagramWebView } from "../../../src/device";

describe("isInstagramWebView", () => {
const IOS_INSTAGRAM_USER_AGENT =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21F90 Instagram 339.0.3.12.91 (iPhone15,3; iOS 17_5_1; en_US; en; scale=3.00; 1290x2796; 619461904; IABMV/1)";

const ANDROID_INSTAGRAM_USER_AGENT =
"Mozilla/5.0 (Linux; Android 9; moto e(6) plus Build/PTAS29.401-58-8; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/112.0.5615.101 Mobile Safari/537.36 Instagram 278.0.0.21.117 Android (28/9; 280dpi; 720x1418; motorola; moto e(6) plus; pokerp; mt6762; en_GB; 464315243)";

beforeEach(() => {
window.navigator = {};
});
it("should return true for iOS Instagram Webview", () => {
window.navigator.userAgent = IOS_INSTAGRAM_USER_AGENT;
const bool = isInstagramWebView();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true for Android Instagram Webview", () => {
window.navigator.userAgent = ANDROID_INSTAGRAM_USER_AGENT;
const bool = isInstagramWebView();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});

it("should return true for iOS Instagram Webview when User Agent is passed as param", () => {
const bool = isInstagramWebView(IOS_INSTAGRAM_USER_AGENT);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true for Android Instagram Webview when User Agent is passed as param", () => {
const bool = isInstagramWebView(ANDROID_INSTAGRAM_USER_AGENT);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});

it("should return false when userAgent does NOT contain Instagram", () => {
window.navigator.userAgent = "potato";
const bool = isInstagramWebView();
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});
});
86 changes: 86 additions & 0 deletions test/tests/device/isMetaInAppBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* @flow */

import { isMetaInAppBrowser } from "../../../src/device";

describe("isMetaInAppBrowser", () => {
const INSTAGRAM_USER_AGENT_IN_APP_BROWSER =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21F90 Instagram 339.0.3.12.91 (iPhone15,3; iOS 17_5_1; en_US; en; scale=3.00; 1290x2796; 619461904; IABMV/1)";

const INSTAGRAM_USER_AGENT_WEBVIEW =
"Mozilla/5.0 (Linux; Android 9; moto e(6) plus Build/PTAS29.401-58-8; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/112.0.5615.101 Mobile Safari/537.36 Instagram 278.0.0.21.117 Android (28/9; 280dpi; 720x1418; motorola; moto e(6) plus; pokerp; mt6762; en_GB; 464315243)";

const FACEBOOK_USER_AGENT_WEBVIEW =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21F90 [FBAN/FBIOS;FBAV/474.0.0.28.107;FBBV/625583284;FBDV/iPhone11,8;FBMD/iPhone;FBSN/iOS;FBSV/17.5.1;FBSS/2;FBID/phone;FBLC/es_LA;FBOP/5;FBRV/627786156]";

const FACEBOOK_USER_AGENT_IN_APP_BROWSER =
"Mozilla/5.0 (Linux; Android 14; Pixel 6 Build/AP2A.240705.004; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/127.0.6533.61 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/474.0.0.52.74;IABMV/1;]";
beforeEach(() => {
window.navigator = {};
});
// Tests using Instagram UA
it("should return true for Instagram In App Browser", () => {
window.navigator.userAgent = INSTAGRAM_USER_AGENT_IN_APP_BROWSER;
const bool = isMetaInAppBrowser();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return false for Instagram Webview", () => {
window.navigator.userAgent = INSTAGRAM_USER_AGENT_WEBVIEW;
const bool = isMetaInAppBrowser();
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});

it("should return true for Instagram In App Browser when UA is passed as param", () => {
const bool = isMetaInAppBrowser(INSTAGRAM_USER_AGENT_IN_APP_BROWSER);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return false for Instagram Webview when UA is passed as param", () => {
const bool = isMetaInAppBrowser(INSTAGRAM_USER_AGENT_WEBVIEW);
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});

// Tests using FB user agents

it("should return true for Facebook In App Browser", () => {
window.navigator.userAgent = FACEBOOK_USER_AGENT_IN_APP_BROWSER;
const bool = isMetaInAppBrowser();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return false for Facebook Webview", () => {
window.navigator.userAgent = FACEBOOK_USER_AGENT_WEBVIEW;
const bool = isMetaInAppBrowser();
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});

it("should return true for Facebook In App Browser when UA is passed as param", () => {
const bool = isMetaInAppBrowser(FACEBOOK_USER_AGENT_IN_APP_BROWSER);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return false for Facebook Webview when UA is passed as param", () => {
const bool = isMetaInAppBrowser(FACEBOOK_USER_AGENT_WEBVIEW);
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});

it("should return false when userAgent does NOT contain IABMV/1", () => {
window.navigator.userAgent = "potato";
const bool = isMetaInAppBrowser();
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});
});
86 changes: 86 additions & 0 deletions test/tests/device/isMetaWebView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* @flow */

import { isMetaWebView } from "../../../src/device";

describe("isMetaWebView", () => {
const IOS_INSTAGRAM_USER_AGENT =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21F90 Instagram 339.0.3.12.91 (iPhone15,3; iOS 17_5_1; en_US; en; scale=3.00; 1290x2796; 619461904; IABMV/1)";

const ANDROID_INSTAGRAM_USER_AGENT =
"Mozilla/5.0 (Linux; Android 9; moto e(6) plus Build/PTAS29.401-58-8; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/112.0.5615.101 Mobile Safari/537.36 Instagram 278.0.0.21.117 Android (28/9; 280dpi; 720x1418; motorola; moto e(6) plus; pokerp; mt6762; en_GB; 464315243)";

const IOS_FACEBOOK_USERAGENT =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21F90 [FBAN/FBIOS;FBAV/474.0.0.28.107;FBBV/625583284;FBDV/iPhone11,8;FBMD/iPhone;FBSN/iOS;FBSV/17.5.1;FBSS/2;FBID/phone;FBLC/es_LA;FBOP/5;FBRV/627786156]";

const ANDROID_FACEBOOK_USERAGENT =
"Mozilla/5.0 (Linux; Android 14; Pixel 6 Build/AP2A.240705.004; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/127.0.6533.61 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/474.0.0.52.74;IABMV/1;]";
beforeEach(() => {
window.navigator = {};
});
// Tests using Instagram UA
it("should return true for iOS Instagram Webview", () => {
window.navigator.userAgent = IOS_INSTAGRAM_USER_AGENT;
const bool = isMetaWebView();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true for Android Instagram Webview", () => {
window.navigator.userAgent = ANDROID_INSTAGRAM_USER_AGENT;
const bool = isMetaWebView();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});

it("should return true for iOS Instagram Webview when User Agent is passed as param", () => {
const bool = isMetaWebView(IOS_INSTAGRAM_USER_AGENT);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true for Android Instagram Webview when User Agent is passed as param", () => {
const bool = isMetaWebView(ANDROID_INSTAGRAM_USER_AGENT);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});

// Tests using FB user agents

it("should return true for iOS Facebook Webview", () => {
window.navigator.userAgent = IOS_FACEBOOK_USERAGENT;
const bool = isMetaWebView();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true for Android Facebook Webview", () => {
window.navigator.userAgent = ANDROID_FACEBOOK_USERAGENT;
const bool = isMetaWebView();
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});

it("should return true for iOS Facebook Webview when User Agent is passed as param", () => {
const bool = isMetaWebView(IOS_FACEBOOK_USERAGENT);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true for Android Facebook Webview when User Agent is passed as param", () => {
const bool = isMetaWebView(ANDROID_FACEBOOK_USERAGENT);
if (!bool) {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});

it("should return false when userAgent does NOT contain Instagram", () => {
window.navigator.userAgent = "potato";
const bool = isMetaWebView();
if (bool) {
throw new Error(`Expected false, got ${JSON.stringify(bool)}`);
}
});
});

0 comments on commit d9c3900

Please sign in to comment.