Skip to content

Instantly share code, notes, and snippets.

@kopyl
Last active April 8, 2026 16:09
Show Gist options
  • Select an option

  • Save kopyl/ece4f77ef299bf2a4eccdf8586dde574 to your computer and use it in GitHub Desktop.

Select an option

Save kopyl/ece4f77ef299bf2a4eccdf8586dde574 to your computer and use it in GitHub Desktop.
// Source: https://stackoverflow.com/a/9851769/18251892
export function getBrowserName(): string {
const win = window as unknown as Record<string, unknown>;
const isOpera
= (!!win.opr && !!(win.opr as Record<string, unknown>).addons)
|| !!win.opera
|| navigator.userAgent.includes(' OPR/');
const isFirefox = typeof (win as Record<string, unknown>).InstallTrigger !== 'undefined';
const isSafari
= /constructor/i.test((window.HTMLElement as unknown as string) ?? '')
|| ((p: unknown) => String(p) === '[object SafariRemoteNotification]')(!win.safari
|| (typeof win.safari !== 'undefined'
&& (win.safari as Record<string, unknown>).pushNotification));
const isIE = !!(document as unknown as Record<string, unknown>).documentMode;
const isEdgeLegacy = !isIE && !!(win as Record<string, unknown>).StyleMedia;
const isChrome
= !!win.chrome
&& (!!(win.chrome as Record<string, unknown>).webstore
|| !!(win.chrome as Record<string, unknown>).runtime);
const isEdgeChromium = isChrome && navigator.userAgent.includes('Edg');
if (isEdgeChromium) return 'Edge (Chromium)';
if (isEdgeLegacy) return 'Edge (Legacy)';
if (isOpera) return 'Opera';
if (isFirefox) return 'Firefox';
if (isSafari) return 'Safari';
if (isIE) return 'Internet Explorer';
if (isChrome) return 'Chrome';
return 'Unknown';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment