Skip to content

Commit d7edfb3

Browse files
committed
Added firefox version checker
commit_hash:09b73b1011ddc5cafdda4315131de56e3c6b368c
1 parent 10a7dea commit d7edfb3

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

src/utils/browser/__tests__/browser.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
isIOS,
2222
isAndroidWebView,
2323
} from '../browser';
24-
import { isFF, isGecko } from '../firefox';
24+
import { isFF, isGecko, getFFVersion } from '../firefox';
2525

2626
describe('browser Utils', () => {
2727
const win = (obj: any = {}) => {
@@ -352,6 +352,41 @@ describe('browser Utils', () => {
352352
).to.be.true;
353353
});
354354

355+
it('isFFVersion', () => {
356+
chai.assert(
357+
getFFVersion(win('Something')) === 0,
358+
'false for something that not looks like Firefox',
359+
);
360+
chai.assert(
361+
getFFVersion(ffWin()) === 123,
362+
'true if Firefox and version >= requested one',
363+
);
364+
chai.expect(
365+
getFFVersion(ffWin()),
366+
'false if Firefox and version < requested one',
367+
).to.be.lt(200);
368+
});
369+
370+
it('returns Firefox version for Windows UA', () => {
371+
const ctx = win({
372+
navigator: {
373+
userAgent:
374+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0',
375+
},
376+
});
377+
chai.expect(getFFVersion(ctx)).to.eq(147);
378+
});
379+
380+
it('returns Firefox version for macOS UA', () => {
381+
const ctx = win({
382+
navigator: {
383+
userAgent:
384+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0',
385+
},
386+
});
387+
chai.expect(getFFVersion(ctx)).to.eq(148);
388+
});
389+
355390
it('check isIOS', () => {
356391
chai.expect(
357392
isIOS(

src/utils/browser/browser.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { indexOfWin } from 'src/utils/array/utils';
44
import { cSome } from 'src/utils/array/some';
55
import { cReduce } from 'src/utils/array/reduce';
66
import { ctxPath, getPath, isUndefined } from 'src/utils/object';
7-
import { isFF, isFFVersionRegExp } from 'src/utils/browser/firefox';
7+
import { getFFVersion } from 'src/utils/browser/firefox';
88
import { MIN_EDGE_VERSION, MIN_FIREFOX_VERSION } from 'src/utils/browser/const';
99
import {
1010
bind,
@@ -254,22 +254,11 @@ export const isEdgeMinVersion = (ctx: Window, minVersion: number) => {
254254
return false;
255255
};
256256

257-
export const isFFVersion = (ctx: Window, minVersion: number) => {
258-
if (isFF(ctx) && minVersion) {
259-
const agent = getAgent(ctx);
260-
const version = agent.match(isFFVersionRegExp);
261-
if (version && version.length) {
262-
return +version[1] >= minVersion;
263-
}
264-
}
265-
return false;
266-
};
267-
268257
// All tracking protection browsers: intellectual TP, enhanced TP, etc
269258
export const isTP = memo((ctx: Window) => {
270259
return (
271260
isITP(ctx) ||
272-
isFFVersion(ctx, MIN_FIREFOX_VERSION) ||
261+
getFFVersion(ctx) >= MIN_FIREFOX_VERSION ||
273262
isEdgeMinVersion(ctx, MIN_EDGE_VERSION)
274263
);
275264
});

src/utils/browser/firefox.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { getPath } from 'src/utils/object/path';
22
import { memo } from 'src/utils/function/memo';
33
import { bindArg } from 'src/utils/function/bind/bind';
4-
import { checkUserAgent } from './utils';
4+
import { checkUserAgent, getAgent } from './utils';
55
import { isNil } from '../object/assertions';
6+
import { parseDecimalInt } from '../number/number';
67

78
export const isGecko = memo(bindArg(/gecko/, checkUserAgent));
89
export const isFFVersionRegExp = /Firefox\/([0-9]+)/i;
@@ -18,3 +19,15 @@ export const isFF = memo((ctx: Window) => {
1819
hasFFVersion
1920
);
2021
});
22+
23+
export const getFFVersion = (ctx: Window): number => {
24+
if (isFF(ctx)) {
25+
const agent = getAgent(ctx);
26+
const version = agent.match(isFFVersionRegExp);
27+
if (version && version.length) {
28+
return parseDecimalInt(version[1]);
29+
}
30+
return 1;
31+
}
32+
return 0;
33+
};

0 commit comments

Comments
 (0)