Skip to content

Commit a1e1ec1

Browse files
authored
Update kapa_widget.js
1 parent 7f36e5c commit a1e1ec1

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

static/js/kapa_widget.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
function insertKapaWidget() {
22
// Check if user agent is iOS 16.4 or lower
33
function isOldiOS() {
4-
const ua = navigator.userAgent;
4+
const ua = navigator.userAgent;
55

6-
// Check if it's an iOS device
7-
const isIOS = /iPad|iPhone/.test(ua);
6+
// Check if it's an iOS device
7+
const isIOS = /iPad|iPhone/.test(ua);
88

9-
if (isIOS) {
10-
// Extract iOS version if it exists
11-
const iosVersionMatch = ua.match(/OS (\d+)_(\d+)/);
12-
if (iosVersionMatch) {
13-
const majorVersion = parseInt(iosVersionMatch[1], 10);
14-
const minorVersion = parseInt(iosVersionMatch[2], 10);
9+
if (isIOS) {
10+
// Regex to capture major version and optionally minor version.
11+
// Examples: "OS 16_4" (major: 16, minor: 4), "OS 16" (major: 16, minor: defaults to 0)
12+
const iosVersionMatch = ua.match(/OS (\d+)(?:_(\d+))?/);
1513

16-
// Return true if iOS version is 16.4 or lower
17-
return majorVersion < 16 || (majorVersion === 16 && minorVersion <= 4);
14+
if (iosVersionMatch) {
15+
const majorVersion = parseInt(iosVersionMatch[1], 10);
16+
let minorVersion = 0; // Default minor version to 0
17+
18+
// If minor version (group 2) was captured in the regex
19+
if (iosVersionMatch[2]) {
20+
minorVersion = parseInt(iosVersionMatch[2], 10);
21+
// Fallback if parsing somehow still results in NaN (though \d+ should prevent this for a captured group)
22+
if (isNaN(minorVersion)) {
23+
minorVersion = 0;
24+
}
1825
}
19-
}
2026

21-
return false;
27+
// Return true if iOS version is 16.4 or lower
28+
// (major < 16) OR (major == 16 AND minor <= 4)
29+
return majorVersion < 16 || (majorVersion === 16 && minorVersion <= 4);
30+
} else {
31+
// It's an iOS device, but we couldn't parse the OS version string using the regex.
32+
// To be safe and prevent loading the widget on a potentially old/unsupported iOS version,
33+
// assume it IS an "old iOS" in this scenario.
34+
console.warn('Kapa widget: iOS device detected, but OS version parsing failed. Assuming old iOS to prevent loading.');
35+
return true; // Treat as "old" if parsing fails for an iOS device
36+
}
2237
}
2338

39+
// Not an iOS device, or parsing failed and we're not treating it as old by default (if the above 'else' was different)
40+
return false;
41+
}
42+
2443
// Only insert script if not running on older iOS as Kapa does not support it (using a look behind regex)
2544
if (!isOldiOS()) {
2645
const script = document.createElement('script');

0 commit comments

Comments
 (0)