|
| 1 | +declare global { |
| 2 | + interface Window { |
| 3 | + opera: unknown; |
| 4 | + } |
| 5 | +} |
| 6 | + |
| 7 | +export function getContext(window: Window) { |
| 8 | + let context = {}; |
| 9 | + if (window.navigator) { |
| 10 | + const userAgent = window.navigator.userAgent; |
| 11 | + context = { |
| 12 | + ...context, |
| 13 | + $os: os(window), |
| 14 | + $browser: browser(userAgent, window.navigator.vendor, !!window.opera), |
| 15 | + $referrer: window.document.referrer, |
| 16 | + $referring_domain: referringDomain(window.document.referrer), |
| 17 | + $device: device(userAgent), |
| 18 | + $current_url: window.location.href, |
| 19 | + $host: window.location.host, |
| 20 | + $pathname: window.location.pathname, |
| 21 | + $browser_version: browserVersion( |
| 22 | + userAgent, |
| 23 | + window.navigator.vendor, |
| 24 | + !!window.opera |
| 25 | + ), |
| 26 | + $screen_height: window.screen.height, |
| 27 | + $screen_width: window.screen.width, |
| 28 | + $screen_dpr: window.devicePixelRatio |
| 29 | + }; |
| 30 | + } |
| 31 | + context = { |
| 32 | + ...context, |
| 33 | + $lib: 'js', |
| 34 | + // $lib_version: version, |
| 35 | + $insert_id: |
| 36 | + Math.random().toString(36).substring(2, 10) + |
| 37 | + Math.random().toString(36).substring(2, 10), |
| 38 | + $time: new Date().getTime() / 1000 // epoch time in seconds |
| 39 | + }; |
| 40 | + return context; // TODO: strip empty props? |
| 41 | +} |
| 42 | + |
| 43 | +function includes(haystack: string, needle: string): boolean { |
| 44 | + return haystack.indexOf(needle) >= 0; |
| 45 | +} |
| 46 | + |
| 47 | +function browser(userAgent: string, vendor: string, opera: boolean): string { |
| 48 | + vendor = vendor || ''; // vendor is undefined for at least IE9 |
| 49 | + if (opera || includes(userAgent, ' OPR/')) { |
| 50 | + if (includes(userAgent, 'Mini')) { |
| 51 | + return 'Opera Mini'; |
| 52 | + } |
| 53 | + return 'Opera'; |
| 54 | + } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) { |
| 55 | + return 'BlackBerry'; |
| 56 | + } else if ( |
| 57 | + includes(userAgent, 'IEMobile') || |
| 58 | + includes(userAgent, 'WPDesktop') |
| 59 | + ) { |
| 60 | + return 'Internet Explorer Mobile'; |
| 61 | + } else if (includes(userAgent, 'SamsungBrowser/')) { |
| 62 | + // https://developer.samsung.com/internet/user-agent-string-format |
| 63 | + return 'Samsung Internet'; |
| 64 | + } else if (includes(userAgent, 'Edge') || includes(userAgent, 'Edg/')) { |
| 65 | + return 'Microsoft Edge'; |
| 66 | + } else if (includes(userAgent, 'FBIOS')) { |
| 67 | + return 'Facebook Mobile'; |
| 68 | + } else if (includes(userAgent, 'Chrome')) { |
| 69 | + return 'Chrome'; |
| 70 | + } else if (includes(userAgent, 'CriOS')) { |
| 71 | + return 'Chrome iOS'; |
| 72 | + } else if (includes(userAgent, 'UCWEB') || includes(userAgent, 'UCBrowser')) { |
| 73 | + return 'UC Browser'; |
| 74 | + } else if (includes(userAgent, 'FxiOS')) { |
| 75 | + return 'Firefox iOS'; |
| 76 | + } else if (includes(vendor, 'Apple')) { |
| 77 | + if (includes(userAgent, 'Mobile')) { |
| 78 | + return 'Mobile Safari'; |
| 79 | + } |
| 80 | + return 'Safari'; |
| 81 | + } else if (includes(userAgent, 'Android')) { |
| 82 | + return 'Android Mobile'; |
| 83 | + } else if (includes(userAgent, 'Konqueror')) { |
| 84 | + return 'Konqueror'; |
| 85 | + } else if (includes(userAgent, 'Firefox')) { |
| 86 | + return 'Firefox'; |
| 87 | + } else if (includes(userAgent, 'MSIE') || includes(userAgent, 'Trident/')) { |
| 88 | + return 'Internet Explorer'; |
| 89 | + } else if (includes(userAgent, 'Gecko')) { |
| 90 | + return 'Mozilla'; |
| 91 | + } else { |
| 92 | + return ''; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function browserVersion( |
| 97 | + userAgent: string, |
| 98 | + vendor: string, |
| 99 | + opera: boolean |
| 100 | +): number | null { |
| 101 | + const regexList = { |
| 102 | + 'Internet Explorer Mobile': /rv:(\d+(\.\d+)?)/, |
| 103 | + 'Microsoft Edge': /Edge?\/(\d+(\.\d+)?)/, |
| 104 | + Chrome: /Chrome\/(\d+(\.\d+)?)/, |
| 105 | + 'Chrome iOS': /CriOS\/(\d+(\.\d+)?)/, |
| 106 | + 'UC Browser': /(UCBrowser|UCWEB)\/(\d+(\.\d+)?)/, |
| 107 | + Safari: /Version\/(\d+(\.\d+)?)/, |
| 108 | + 'Mobile Safari': /Version\/(\d+(\.\d+)?)/, |
| 109 | + Opera: /(Opera|OPR)\/(\d+(\.\d+)?)/, |
| 110 | + Firefox: /Firefox\/(\d+(\.\d+)?)/, |
| 111 | + 'Firefox iOS': /FxiOS\/(\d+(\.\d+)?)/, |
| 112 | + Konqueror: /Konqueror:(\d+(\.\d+)?)/, |
| 113 | + BlackBerry: /BlackBerry (\d+(\.\d+)?)/, |
| 114 | + 'Android Mobile': /android\s(\d+(\.\d+)?)/, |
| 115 | + 'Samsung Internet': /SamsungBrowser\/(\d+(\.\d+)?)/, |
| 116 | + 'Internet Explorer': /(rv:|MSIE )(\d+(\.\d+)?)/, |
| 117 | + Mozilla: /rv:(\d+(\.\d+)?)/ |
| 118 | + }; |
| 119 | + |
| 120 | + const browserString = browser( |
| 121 | + userAgent, |
| 122 | + vendor, |
| 123 | + opera |
| 124 | + ) as keyof typeof regexList; |
| 125 | + const regex: RegExp = regexList[browserString] || undefined; |
| 126 | + |
| 127 | + if (regex === undefined) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + const matches = userAgent.match(regex); |
| 131 | + if (!matches) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + return parseFloat(matches[matches.length - 2] as string); |
| 135 | +} |
| 136 | + |
| 137 | +function os(window: Window): string { |
| 138 | + const a = window.navigator.userAgent; |
| 139 | + if (/Windows/i.test(a)) { |
| 140 | + if (/Phone/.test(a) || /WPDesktop/.test(a)) { |
| 141 | + return 'Windows Phone'; |
| 142 | + } |
| 143 | + return 'Windows'; |
| 144 | + } else if (/(iPhone|iPad|iPod)/.test(a)) { |
| 145 | + return 'iOS'; |
| 146 | + } else if (/Android/.test(a)) { |
| 147 | + return 'Android'; |
| 148 | + } else if (/(BlackBerry|PlayBook|BB10)/i.test(a)) { |
| 149 | + return 'BlackBerry'; |
| 150 | + } else if (/Mac/i.test(a)) { |
| 151 | + return 'Mac OS X'; |
| 152 | + } else if (/Linux/.test(a)) { |
| 153 | + return 'Linux'; |
| 154 | + } else if (/CrOS/.test(a)) { |
| 155 | + return 'Chrome OS'; |
| 156 | + } else { |
| 157 | + return ''; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +function device(userAgent: string): string { |
| 162 | + if (/Windows Phone/i.test(userAgent) || /WPDesktop/.test(userAgent)) { |
| 163 | + return 'Windows Phone'; |
| 164 | + } else if (/iPad/.test(userAgent)) { |
| 165 | + return 'iPad'; |
| 166 | + } else if (/iPod/.test(userAgent)) { |
| 167 | + return 'iPod Touch'; |
| 168 | + } else if (/iPhone/.test(userAgent)) { |
| 169 | + return 'iPhone'; |
| 170 | + } else if (/(BlackBerry|PlayBook|BB10)/i.test(userAgent)) { |
| 171 | + return 'BlackBerry'; |
| 172 | + } else if (/Android/.test(userAgent)) { |
| 173 | + return 'Android'; |
| 174 | + } else { |
| 175 | + return ''; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +function referringDomain(referrer: string): string { |
| 180 | + const split = referrer.split('/'); |
| 181 | + if (split.length >= 3) { |
| 182 | + return split[2] as string; |
| 183 | + } |
| 184 | + return ''; |
| 185 | +} |
0 commit comments