Skip to content

Commit fe1e99e

Browse files
authored
fix: improve browser version detection (#738)
* fix: improve browser version detection * chore: finalize variables
1 parent e861032 commit fe1e99e

File tree

1 file changed

+77
-7
lines changed

1 file changed

+77
-7
lines changed

lib/src/support/platform/web.dart

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,82 @@ BrowserType lkBrowserImplementation() {
6767
}
6868

6969
BrowserVersion lkBrowserVersionImplementation() {
70-
var appVersion = web.window.navigator.appVersion;
71-
Match match =
72-
RegExp(r'Version/(\d+)(\.(\d+))?(\.(\d+))?').firstMatch(appVersion)!;
73-
var major = int.parse(match.group(1)!);
74-
var minor = int.parse(match.group(3) ?? '0');
75-
var patch = int.parse(match.group(5) ?? '0');
70+
final ua = web.window.navigator.userAgent;
71+
final appVersion = web.window.navigator.appVersion;
72+
73+
BrowserVersion chromeBased(String prefix) {
74+
Match? match =
75+
RegExp(prefix + r'/(\d+)\.(\d+)\.(\d+)\.(\d+)').firstMatch(appVersion);
76+
if (match == null) {
77+
return BrowserVersion(0, 0, 0);
78+
}
79+
80+
final major = int.parse(match.group(1)!);
81+
final minor = int.parse(match.group(2)!);
82+
final patch = int.parse(match.group(3)!);
83+
return BrowserVersion(major, minor, patch);
84+
}
85+
86+
switch (lkBrowserImplementation()) {
87+
case BrowserType.chrome:
88+
return chromeBased('Chrome');
89+
case BrowserType.firefox:
90+
Match? match = RegExp(r'rv:(\d+)\.(\d+)\)').firstMatch(ua);
91+
if (match == null) {
92+
return BrowserVersion(0, 0, 0);
93+
}
94+
95+
final major = int.parse(match.group(1)!);
96+
final minor = int.parse(match.group(2)!);
97+
return BrowserVersion(major, minor, 0);
98+
case BrowserType.safari:
99+
Match? match =
100+
RegExp(r'Version/(\d+)(\.(\d+))?(\.(\d+))?').firstMatch(appVersion);
101+
if (match == null) {
102+
return BrowserVersion(0, 0, 0);
103+
}
76104

77-
return BrowserVersion(major, minor, patch);
105+
final major = int.parse(match.group(1)!);
106+
final minor = int.parse(match.group(3) ?? '0');
107+
final patch = int.parse(match.group(5) ?? '0');
108+
return BrowserVersion(major, minor, patch);
109+
case BrowserType.internetExplorer:
110+
Match? match = RegExp(r'MSIE (\d+)\.(\d+);').firstMatch(appVersion);
111+
if (match != null) {
112+
final major = int.parse(match.group(1)!);
113+
final minor = int.parse(match.group(2)!);
114+
return BrowserVersion(major, minor, 0);
115+
}
116+
117+
match = RegExp(r'rv[: ](\d+)\.(\d+)').firstMatch(appVersion);
118+
if (match != null) {
119+
final major = int.parse(match.group(1)!);
120+
final minor = int.parse(match.group(2)!);
121+
return BrowserVersion(major, minor, 0);
122+
}
123+
124+
match = RegExp(r'Edge/(\d+)\.(\d+)$').firstMatch(appVersion);
125+
if (match != null) {
126+
final major = int.parse(match.group(1)!);
127+
final minor = int.parse(match.group(2)!);
128+
return BrowserVersion(major, minor, 0);
129+
}
130+
131+
return BrowserVersion(0, 0, 0);
132+
case BrowserType.wkWebView:
133+
Match? match =
134+
RegExp(r'AppleWebKit/(\d+)\.(\d+)\.(\d+)').firstMatch(appVersion);
135+
if (match == null) {
136+
return BrowserVersion(0, 0, 0);
137+
}
138+
139+
final major = int.parse(match.group(1)!);
140+
final minor = int.parse(match.group(2)!);
141+
final patch = int.parse(match.group(3)!);
142+
return BrowserVersion(major, minor, patch);
143+
case BrowserType.edge:
144+
return chromeBased('Edg');
145+
default:
146+
return BrowserVersion(0, 0, 0);
147+
}
78148
}

0 commit comments

Comments
 (0)