[PM-8582] Move Safari browser check to libs/platform (#11007)

This commit is contained in:
Victoria League 2024-09-16 15:36:05 -04:00 committed by GitHub
parent 26f3dcfc66
commit 112bad03b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import { Observable } from "rxjs"; import { Observable } from "rxjs";
import { DeviceType } from "@bitwarden/common/enums"; import { DeviceType } from "@bitwarden/common/enums";
import { isBrowserSafariApi } from "@bitwarden/platform";
import { TabMessage } from "../../types/tab-messages"; import { TabMessage } from "../../types/tab-messages";
import { BrowserPlatformUtilsService } from "../services/platform-utils/browser-platform-utils.service"; import { BrowserPlatformUtilsService } from "../services/platform-utils/browser-platform-utils.service";
@ -9,10 +10,7 @@ import { registerContentScriptsPolyfill } from "./browser-api.register-content-s
export class BrowserApi { export class BrowserApi {
static isWebExtensionsApi: boolean = typeof browser !== "undefined"; static isWebExtensionsApi: boolean = typeof browser !== "undefined";
static isSafariApi: boolean = static isSafariApi: boolean = isBrowserSafariApi();
navigator.userAgent.indexOf(" Safari/") !== -1 &&
navigator.userAgent.indexOf(" Chrome/") === -1 &&
navigator.userAgent.indexOf(" Chromium/") === -1;
static isChromeApi: boolean = !BrowserApi.isSafariApi && typeof chrome !== "undefined"; static isChromeApi: boolean = !BrowserApi.isSafariApi && typeof chrome !== "undefined";
static isFirefoxOnAndroid: boolean = static isFirefoxOnAndroid: boolean =
navigator.userAgent.indexOf("Firefox/") !== -1 && navigator.userAgent.indexOf("Android") !== -1; navigator.userAgent.indexOf("Firefox/") !== -1 && navigator.userAgent.indexOf("Android") !== -1;

View File

@ -0,0 +1 @@
export * from "./services/browser-service";

View File

@ -0,0 +1,41 @@
import { isBrowserSafariApi } from "./browser-service";
describe("browser-service", () => {
describe("isBrowserSafariApi", () => {
it("returns true if browser is safari", () => {
jest
.spyOn(navigator, "userAgent", "get")
.mockReturnValue(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15",
);
const result = isBrowserSafariApi();
expect(result).toBe(true);
});
it("returns false if browser is chrome", () => {
jest
.spyOn(navigator, "userAgent", "get")
.mockReturnValue(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
);
const result = isBrowserSafariApi();
expect(result).toBe(false);
});
it("returns false if browser is firefox", () => {
jest
.spyOn(navigator, "userAgent", "get")
.mockReturnValue(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:130.0) Gecko/20100101 Firefox/130.0",
);
const result = isBrowserSafariApi();
expect(result).toBe(false);
});
});
});

View File

@ -0,0 +1,7 @@
export function isBrowserSafariApi(): boolean {
return (
navigator.userAgent.indexOf(" Safari/") !== -1 &&
navigator.userAgent.indexOf(" Chrome/") === -1 &&
navigator.userAgent.indexOf(" Chromium/") === -1
);
}