|
1 | 1 | function insertKapaWidget() { |
2 | 2 | // Check if user agent is iOS 16.4 or lower |
3 | 3 | function isOldiOS() { |
4 | | - const ua = navigator.userAgent; |
| 4 | + const ua = navigator.userAgent; |
5 | 5 |
|
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); |
8 | 8 |
|
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+))?/); |
15 | 13 |
|
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 | + } |
18 | 25 | } |
19 | | - } |
20 | 26 |
|
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 | + } |
22 | 37 | } |
23 | 38 |
|
| 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 | + |
24 | 43 | // Only insert script if not running on older iOS as Kapa does not support it (using a look behind regex) |
25 | 44 | if (!isOldiOS()) { |
26 | 45 | const script = document.createElement('script'); |
|
0 commit comments