Skip to content

Commit

Permalink
feat: add short circuit to allow popups in supported browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
sioked committed Apr 19, 2024
1 parent 449034e commit b242fb6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,26 @@ export function isMacOsCna(): boolean {
return /Macintosh.*AppleWebKit(?!.*Safari)/i.test(userAgent);
}

export function isPopupSupportedWebview(ua?: string = getUserAgent()): boolean {
return /FB\/MW/.test(ua);
}

export function supportsPopups(ua?: string = getUserAgent()): boolean {
return !(
isWebView(ua) ||
isIosWebview(ua) ||
isAndroidWebview(ua) ||
isOperaMini(ua) ||
isFirefoxIOS(ua) ||
isEdgeIOS(ua) ||
isFacebookWebView(ua) ||
isQQBrowser(ua) ||
isElectron() ||
isMacOsCna() ||
isStandAlone()
return (
isPopupSupportedWebview(ua) ||
!(
isWebView(ua) ||
isIosWebview(ua) ||
isAndroidWebview(ua) ||
isOperaMini(ua) ||
isFirefoxIOS(ua) ||
isEdgeIOS(ua) ||
isFacebookWebView(ua) ||
isQQBrowser(ua) ||
isElectron() ||
isMacOsCna() ||
isStandAlone()
)
);
}

Expand Down
22 changes: 22 additions & 0 deletions src/device.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* @flow */
import { describe, test, expect } from "vitest";

import { supportsPopups, isPopupSupportedWebview } from "./device";

describe("supportsPopups", () => {
test("returns true when short circuited", () => {
const ua = `Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv)
AppleWebKit/537.36 (KHTML, like Gecko)
Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/44.25.0.1;FB/MW]`;
expect(supportsPopups(ua)).toBe(true);
});
});

describe("isPopupSupportedWebview", () => {
test("returns true when fb webview supported user agent", () => {
const ua = `Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv)
AppleWebKit/537.36 (KHTML, like Gecko)
Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/44.25.0.1;FB/MW]`;
expect(isPopupSupportedWebview(ua)).toBe(true);
});
});
11 changes: 11 additions & 0 deletions test/tests/device/supportsPopups.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ describe("supportsPopups", () => {
throw new Error(`Expected true, got ${JSON.stringify(bool)}`);
}
});
it("should return true when in facebook multiwindow experiment", () => {
window.navigator.userAgent = `Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv)
AppleWebKit/537.36 (KHTML, like Gecko)
Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/44.25.0.1;FB/MW]`;
const result = supportsPopups();
if (!result) {
throw new Error(
`Expected to support popups, got ${JSON.stringify(result)}`
);
}
});
});
describe("WebView on Android", () => {
it("should return false when it is a valid WebView UA in KitKat to Lollipop", () => {
Expand Down

0 comments on commit b242fb6

Please sign in to comment.