initial commit

This commit is contained in:
2026-06-25 21:30:32 +00:00
commit 328faf6251
220 changed files with 162103 additions and 0 deletions
+340
View File
@@ -0,0 +1,340 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Closure user agent detection (Browser).
* @see <a href="http://www.useragentstring.com/">User agent strings</a>
* For more information on rendering engine, platform, or device see the other
* sub-namespaces in goog.labs.userAgent, goog.labs.userAgent.platform,
* goog.labs.userAgent.device respectively.)
*/
goog.module('goog.labs.userAgent.browser');
goog.module.declareLegacyNamespace();
const googArray = goog.require('goog.array');
const googObject = goog.require('goog.object');
const util = goog.require('goog.labs.userAgent.util');
const {compareVersions} = goog.require('goog.string.internal');
// TODO(nnaze): Refactor to remove excessive exclusion logic in matching
// functions.
/**
* @return {boolean} Whether to use navigator.userAgentData to determine
* browser's brand.
*/
function useUserAgentBrand() {
const userAgentData = util.getUserAgentData();
return !!userAgentData && userAgentData.brands.length > 0;
}
/**
* @return {boolean} Whether the user's browser is Opera. Note: Chromium based
* Opera (Opera 15+) is detected as Chrome to avoid unnecessary special
* casing.
*/
function matchOpera() {
if (util.ASSUME_CLIENT_HINTS_SUPPORT || util.getUserAgentData()) {
// This will remain false for non Chromium based Opera.
return false;
}
return util.matchUserAgent('Opera');
}
/** @return {boolean} Whether the user's browser is IE. */
function matchIE() {
if (util.ASSUME_CLIENT_HINTS_SUPPORT || util.getUserAgentData()) {
// This will remain false for IE.
return false;
}
return util.matchUserAgent('Trident') || util.matchUserAgent('MSIE');
}
/**
* @return {boolean} Whether the user's browser is Edge. This refers to
* EdgeHTML based Edge.
*/
function matchEdgeHtml() {
if (util.ASSUME_CLIENT_HINTS_SUPPORT || util.getUserAgentData()) {
// This will remain false for non chromium based Edge.
return false;
}
return util.matchUserAgent('Edge');
}
/** @return {boolean} Whether the user's browser is Chromium based Edge. */
function matchEdgeChromium() {
if (useUserAgentBrand()) {
return util.matchUserAgentDataBrand('Edge');
}
return util.matchUserAgent('Edg/');
}
/** @return {boolean} Whether the user's browser is Chromium based Opera. */
function matchOperaChromium() {
if (useUserAgentBrand()) {
return util.matchUserAgentDataBrand('Opera');
}
return util.matchUserAgent('OPR');
}
/** @return {boolean} Whether the user's browser is Firefox. */
function matchFirefox() {
if (useUserAgentBrand()) {
return util.matchUserAgentDataBrand('Firefox');
}
return util.matchUserAgent('Firefox') || util.matchUserAgent('FxiOS');
}
/** @return {boolean} Whether the user's browser is Safari. */
function matchSafari() {
if (useUserAgentBrand()) {
// This will always be false before Safari adopt the Client Hint support.
return util.matchUserAgentDataBrand('Safari');
}
return util.matchUserAgent('Safari') &&
!(matchChrome() || matchCoast() || matchOpera() || matchEdgeHtml() ||
matchEdgeChromium() || matchOperaChromium() || matchFirefox() ||
isSilk() || util.matchUserAgent('Android'));
}
/**
* @return {boolean} Whether the user's browser is Coast (Opera's Webkit-based
* iOS browser).
*/
function matchCoast() {
if (util.ASSUME_CLIENT_HINTS_SUPPORT || util.getUserAgentData()) {
// This will remain false for Coast.
return false;
}
return util.matchUserAgent('Coast');
}
/** @return {boolean} Whether the user's browser is iOS Webview. */
function matchIosWebview() {
// iOS Webview does not show up as Chrome or Safari. Also check for Opera's
// WebKit-based iOS browser, Coast.
return (util.matchUserAgent('iPad') || util.matchUserAgent('iPhone')) &&
!matchSafari() && !matchChrome() && !matchCoast() && !matchFirefox() &&
util.matchUserAgent('AppleWebKit');
}
/**
* @return {boolean} Whether the user's browser is any Chromium browser. This
* returns true for Chrome, Opera 15+, and Edge Chromium.
*/
function matchChrome() {
if (useUserAgentBrand()) {
return util.matchUserAgentDataBrand('Chromium');
}
return (util.matchUserAgent('Chrome') || util.matchUserAgent('CriOS')) &&
!matchEdgeHtml();
}
/** @return {boolean} Whether the user's browser is the Android browser. */
function matchAndroidBrowser() {
// Android can appear in the user agent string for Chrome on Android.
// This is not the Android standalone browser if it does.
return util.matchUserAgent('Android') &&
!(isChrome() || isFirefox() || isOpera() || isSilk());
}
/** @return {boolean} Whether the user's browser is Opera. */
const isOpera = matchOpera;
/** @return {boolean} Whether the user's browser is IE. */
const isIE = matchIE;
/** @return {boolean} Whether the user's browser is EdgeHTML based Edge. */
const isEdge = matchEdgeHtml;
/** @return {boolean} Whether the user's browser is Chromium based Edge. */
const isEdgeChromium = matchEdgeChromium;
/** @return {boolean} Whether the user's browser is Chromium based Opera. */
const isOperaChromium = matchOperaChromium;
/** @return {boolean} Whether the user's browser is Firefox. */
const isFirefox = matchFirefox;
/** @return {boolean} Whether the user's browser is Safari. */
const isSafari = matchSafari;
/**
* @return {boolean} Whether the user's browser is Coast (Opera's Webkit-based
* iOS browser).
*/
const isCoast = matchCoast;
/** @return {boolean} Whether the user's browser is iOS Webview. */
const isIosWebview = matchIosWebview;
/**
* @return {boolean} Whether the user's browser is any Chromium based browser (
* Chrome, Blink-based Opera (15+) and Edge Chromium).
*/
const isChrome = matchChrome;
/** @return {boolean} Whether the user's browser is the Android browser. */
const isAndroidBrowser = matchAndroidBrowser;
/**
* For more information, see:
* http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html
* @return {boolean} Whether the user's browser is Silk.
*/
function isSilk() {
if (useUserAgentBrand()) {
return util.matchUserAgentDataBrand('Silk');
}
return util.matchUserAgent('Silk');
}
/**
* @return {string} The browser version or empty string if version cannot be
* determined. Note that for Internet Explorer, this returns the version of
* the browser, not the version of the rendering engine. (IE 8 in
* compatibility mode will return 8.0 rather than 7.0. To determine the
* rendering engine version, look at document.documentMode instead. See
* http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more
* details.)
*/
function getVersion() {
const userAgentString = util.getUserAgent();
// Special case IE since IE's version is inside the parenthesis and
// without the '/'.
if (isIE()) {
return getIEVersion(userAgentString);
}
const versionTuples = util.extractVersionTuples(userAgentString);
// Construct a map for easy lookup.
const versionMap = {};
versionTuples.forEach((tuple) => {
// Note that the tuple is of length three, but we only care about the
// first two.
const key = tuple[0];
const value = tuple[1];
versionMap[key] = value;
});
const versionMapHasKey = goog.partial(googObject.containsKey, versionMap);
// Gives the value with the first key it finds, otherwise empty string.
function lookUpValueWithKeys(keys) {
const key = googArray.find(keys, versionMapHasKey);
return versionMap[key] || '';
}
// Check Opera before Chrome since Opera 15+ has "Chrome" in the string.
// See
// http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond
if (isOpera()) {
// Opera 10 has Version/10.0 but Opera/9.8, so look for "Version" first.
// Opera uses 'OPR' for more recent UAs.
return lookUpValueWithKeys(['Version', 'Opera']);
}
// Check Edge before Chrome since it has Chrome in the string.
if (isEdge()) {
return lookUpValueWithKeys(['Edge']);
}
// Check Chromium Edge before Chrome since it has Chrome in the string.
if (isEdgeChromium()) {
return lookUpValueWithKeys(['Edg']);
}
if (isChrome()) {
return lookUpValueWithKeys(['Chrome', 'CriOS', 'HeadlessChrome']);
}
// Usually products browser versions are in the third tuple after "Mozilla"
// and the engine.
const tuple = versionTuples[2];
return tuple && tuple[1] || '';
}
/**
* @param {string|number} version The version to check.
* @return {boolean} Whether the browser version is higher or the same as the
* given version.
*/
function isVersionOrHigher(version) {
return compareVersions(getVersion(), version) >= 0;
}
/**
* Determines IE version. More information:
* http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString
* http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
* http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx
* http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx
* @param {string} userAgent the User-Agent.
* @return {string}
*/
function getIEVersion(userAgent) {
// IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade
// bug. Example UA:
// Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
// like Gecko.
// See http://www.whatismybrowser.com/developers/unknown-user-agent-fragments.
const rv = /rv: *([\d\.]*)/.exec(userAgent);
if (rv && rv[1]) {
return rv[1];
}
let version = '';
const msie = /MSIE +([\d\.]+)/.exec(userAgent);
if (msie && msie[1]) {
// IE in compatibility mode usually identifies itself as MSIE 7.0; in this
// case, use the Trident version to determine the version of IE. For more
// details, see the links above.
const tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);
if (msie[1] == '7.0') {
if (tridentVersion && tridentVersion[1]) {
switch (tridentVersion[1]) {
case '4.0':
version = '8.0';
break;
case '5.0':
version = '9.0';
break;
case '6.0':
version = '10.0';
break;
case '7.0':
version = '11.0';
break;
}
} else {
version = '7.0';
}
} else {
version = msie[1];
}
}
return version;
}
exports = {
getVersion,
isAndroidBrowser,
isChrome,
isCoast,
isEdge,
isEdgeChromium,
isFirefox,
isIE,
isIosWebview,
isOpera,
isOperaChromium,
isSafari,
isSilk,
isVersionOrHigher,
};
+69
View File
@@ -0,0 +1,69 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Closure user device detection (based on user agent).
* @see http://en.wikipedia.org/wiki/User_agent
* For more information on browser brand, platform, or engine see the other
* sub-namespaces in goog.labs.userAgent (browser, platform, and engine).
*/
goog.provide('goog.labs.userAgent.device');
goog.require('goog.labs.userAgent.util');
/**
* Currently we detect the iPhone, iPod and Android mobiles (devices that have
* both Android and Mobile in the user agent string).
*
* @return {boolean} Whether the user is using a mobile device.
*/
goog.labs.userAgent.device.isMobile = function() {
'use strict';
if (goog.labs.userAgent.util.ASSUME_CLIENT_HINTS_SUPPORT ||
goog.labs.userAgent.util.getUserAgentData()) {
return goog.labs.userAgent.util.getUserAgentData().mobile;
}
return !goog.labs.userAgent.device.isTablet() &&
(goog.labs.userAgent.util.matchUserAgent('iPod') ||
goog.labs.userAgent.util.matchUserAgent('iPhone') ||
goog.labs.userAgent.util.matchUserAgent('Android') ||
goog.labs.userAgent.util.matchUserAgent('IEMobile'));
};
/**
* Currently we detect Kindle Fire, iPad, and Android tablets (devices that have
* Android but not Mobile in the user agent string).
*
* @return {boolean} Whether the user is using a tablet.
*/
goog.labs.userAgent.device.isTablet = function() {
'use strict';
if (goog.labs.userAgent.util.ASSUME_CLIENT_HINTS_SUPPORT ||
goog.labs.userAgent.util.getUserAgentData()) {
return !goog.labs.userAgent.util.getUserAgentData().mobile &&
(goog.labs.userAgent.util.matchUserAgent('iPad') ||
goog.labs.userAgent.util.matchUserAgent('Android') ||
goog.labs.userAgent.util.matchUserAgent('Silk'));
}
return goog.labs.userAgent.util.matchUserAgent('iPad') ||
(goog.labs.userAgent.util.matchUserAgent('Android') &&
!goog.labs.userAgent.util.matchUserAgent('Mobile')) ||
goog.labs.userAgent.util.matchUserAgent('Silk');
};
/**
* @return {boolean} Whether the user is using a desktop computer (which we
* assume to be the case if they are not using either a mobile or tablet
* device).
*/
goog.labs.userAgent.device.isDesktop = function() {
'use strict';
return !goog.labs.userAgent.device.isMobile() &&
!goog.labs.userAgent.device.isTablet();
};
+145
View File
@@ -0,0 +1,145 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Closure user agent detection.
* @see http://en.wikipedia.org/wiki/User_agent
* For more information on browser brand, platform, or device see the other
* sub-namespaces in goog.labs.userAgent (browser, platform, and device).
*/
goog.module('goog.labs.userAgent.engine');
goog.module.declareLegacyNamespace();
const googArray = goog.require('goog.array');
const googString = goog.require('goog.string.internal');
const util = goog.require('goog.labs.userAgent.util');
/**
* @return {boolean} Whether the rendering engine is Presto.
*/
function isPresto() {
return util.matchUserAgent('Presto');
}
/**
* @return {boolean} Whether the rendering engine is Trident.
*/
function isTrident() {
// IE only started including the Trident token in IE8.
return util.matchUserAgent('Trident') || util.matchUserAgent('MSIE');
}
/**
* @return {boolean} Whether the rendering engine is EdgeHTML.
*/
function isEdge() {
return util.matchUserAgent('Edge');
}
/**
* @return {boolean} Whether the rendering engine is WebKit. This will return
* true for Chrome, Blink-based Opera (15+), Edge Chromium and Safari.
*/
function isWebKit() {
return util.matchUserAgentIgnoreCase('WebKit') && !isEdge();
}
/**
* @return {boolean} Whether the rendering engine is Gecko.
*/
function isGecko() {
return util.matchUserAgent('Gecko') && !isWebKit() && !isTrident() &&
!isEdge();
}
/**
* @return {string} The rendering engine's version or empty string if version
* can't be determined.
*/
function getVersion() {
const userAgentString = util.getUserAgent();
if (userAgentString) {
const tuples = util.extractVersionTuples(userAgentString);
const engineTuple = getEngineTuple(tuples);
if (engineTuple) {
// In Gecko, the version string is either in the browser info or the
// Firefox version. See Gecko user agent string reference:
// http://goo.gl/mULqa
if (engineTuple[0] == 'Gecko') {
return getVersionForKey(tuples, 'Firefox');
}
return engineTuple[1];
}
// MSIE has only one version identifier, and the Trident version is
// specified in the parenthetical. IE Edge is covered in the engine tuple
// detection.
const browserTuple = tuples[0];
let info;
if (browserTuple && (info = browserTuple[2])) {
const match = /Trident\/([^\s;]+)/.exec(info);
if (match) {
return match[1];
}
}
}
return '';
}
/**
* @param {!Array<!Array<string>>} tuples Extracted version tuples.
* @return {!Array<string>|undefined} The engine tuple or undefined if not
* found.
*/
function getEngineTuple(tuples) {
if (!isEdge()) {
return tuples[1];
}
for (let i = 0; i < tuples.length; i++) {
const tuple = tuples[i];
if (tuple[0] == 'Edge') {
return tuple;
}
}
}
/**
* @param {string|number} version The version to check.
* @return {boolean} Whether the rendering engine version is higher or the same
* as the given version.
*/
function isVersionOrHigher(version) {
return googString.compareVersions(getVersion(), version) >= 0;
}
/**
* @param {!Array<!Array<string>>} tuples Version tuples.
* @param {string} key The key to look for.
* @return {string} The version string of the given key, if present.
* Otherwise, the empty string.
*/
function getVersionForKey(tuples, key) {
// TODO(nnaze): Move to util if useful elsewhere.
const pair = googArray.find(tuples, function(pair) {
return key == pair[0];
});
return pair && pair[1] || '';
}
exports = {
getVersion,
isEdge,
isGecko,
isPresto,
isTrident,
isVersionOrHigher,
isWebKit,
};
+181
View File
@@ -0,0 +1,181 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Closure user agent platform detection.
* @see <a href="http://www.useragentstring.com/">User agent strings</a>
* For more information on browser brand, rendering engine, or device see the
* other sub-namespaces in goog.labs.userAgent (browser, engine, and device
* respectively).
*/
goog.module('goog.labs.userAgent.platform');
goog.module.declareLegacyNamespace();
const googString = goog.require('goog.string.internal');
const util = goog.require('goog.labs.userAgent.util');
/**
* @return {boolean} Whether the platform is Android.
*/
function isAndroid() {
return util.matchUserAgent('Android');
}
/**
* @return {boolean} Whether the platform is iPod.
*/
function isIpod() {
return util.matchUserAgent('iPod');
}
/**
* @return {boolean} Whether the platform is iPhone.
*/
function isIphone() {
return util.matchUserAgent('iPhone') && !util.matchUserAgent('iPod') &&
!util.matchUserAgent('iPad');
}
/**
* Returns whether the platform is iPad.
* Note that iPadOS 13+ spoofs macOS Safari by default in its user agent, and in
* this scenario the platform will not be recognized as iPad. If you must have
* iPad-specific behavior, use
* {@link goog.labs.userAgent.extra.isSafariDesktopOnMobile}.
* @return {boolean} Whether the platform is iPad.
*/
function isIpad() {
return util.matchUserAgent('iPad');
}
/**
* Returns whether the platform is iOS.
* Note that iPadOS 13+ spoofs macOS Safari by default in its user agent, and in
* this scenario the platform will not be recognized as iOS. If you must have
* iPad-specific behavior, use
* {@link goog.labs.userAgent.extra.isSafariDesktopOnMobile}.
* @return {boolean} Whether the platform is iOS.
*/
function isIos() {
return isIphone() || isIpad() || isIpod();
}
/**
* @return {boolean} Whether the platform is Mac.
*/
function isMacintosh() {
return util.matchUserAgent('Macintosh');
}
/**
* Note: ChromeOS is not considered to be Linux as it does not report itself
* as Linux in the user agent string.
* @return {boolean} Whether the platform is Linux.
*/
function isLinux() {
return util.matchUserAgent('Linux');
}
/**
* @return {boolean} Whether the platform is Windows.
*/
function isWindows() {
return util.matchUserAgent('Windows');
}
/**
* @return {boolean} Whether the platform is ChromeOS.
*/
function isChromeOS() {
return util.matchUserAgent('CrOS');
}
/**
* @return {boolean} Whether the platform is Chromecast.
*/
function isChromecast() {
return util.matchUserAgent('CrKey');
}
/**
* @return {boolean} Whether the platform is KaiOS.
*/
function isKaiOS() {
return util.matchUserAgentIgnoreCase('KaiOS');
}
/**
* The version of the platform. We only determine the version for Windows,
* Mac, and Chrome OS. It doesn't make much sense on Linux. For Windows, we only
* look at the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given
* version 0.0.
*
* @return {string} The platform version or empty string if version cannot be
* determined.
*/
function getVersion() {
const userAgentString = util.getUserAgent();
let version = '', re;
if (isWindows()) {
re = /Windows (?:NT|Phone) ([0-9.]+)/;
const match = re.exec(userAgentString);
if (match) {
version = match[1];
} else {
version = '0.0';
}
} else if (isIos()) {
re = /(?:iPhone|iPod|iPad|CPU)\s+OS\s+(\S+)/;
const match = re.exec(userAgentString);
// Report the version as x.y.z and not x_y_z
version = match && match[1].replace(/_/g, '.');
} else if (isMacintosh()) {
re = /Mac OS X ([0-9_.]+)/;
const match = re.exec(userAgentString);
// Note: some old versions of Camino do not report an OSX version.
// Default to 10.
version = match ? match[1].replace(/_/g, '.') : '10';
} else if (isKaiOS()) {
re = /(?:KaiOS)\/(\S+)/i;
const match = re.exec(userAgentString);
version = match && match[1];
} else if (isAndroid()) {
re = /Android\s+([^\);]+)(\)|;)/;
const match = re.exec(userAgentString);
version = match && match[1];
} else if (isChromeOS()) {
re = /(?:CrOS\s+(?:i686|x86_64)\s+([0-9.]+))/;
const match = re.exec(userAgentString);
version = match && match[1];
}
return version || '';
}
/**
* @param {string|number} version The version to check.
* @return {boolean} Whether the browser version is higher or the same as the
* given version.
*/
function isVersionOrHigher(version) {
return googString.compareVersions(getVersion(), version) >= 0;
}
exports = {
getVersion,
isAndroid,
isChromeOS,
isChromecast,
isIos,
isIpad,
isIphone,
isIpod,
isKaiOS,
isLinux,
isMacintosh,
isVersionOrHigher,
isWindows,
};
+39
View File
@@ -0,0 +1,39 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Defines for goog.labs.userAgent.
*/
goog.module('goog.labs.userAgent');
/**
* @define {string} Optional runtime override for the USE_CLIENT_HINTS flag.
* If this is set (for example, to 'foo.bar') then any value of USE_CLIENT_HINTS
* will be overridden by `globalThis.foo.bar` if it is non-null.
* This flag will be removed in December 2021.
*/
const USE_CLIENT_HINTS_OVERRIDE =
goog.define('goog.labs.userAgent.USE_CLIENT_HINTS_OVERRIDE', '');
/**
* @define {boolean} If true, use navigator.userAgentData
* TODO(user) Flip flag in 2021/12.
*/
const USE_CLIENT_HINTS =
goog.define('goog.labs.userAgent.USE_CLIENT_HINTS', false);
// TODO(user): Replace the IIFE with a simple null-coalescing operator.
// NOTE: This can't be done with a helper function, or else we risk an inlining
// back-off causing a huge code size regression if a non-inlined helper function
// prevents the optimizer from detecting the (possibly large) dead code paths.
/** @const {boolean} */
exports.USE_CLIENT_HINTS = (() => {
const override = USE_CLIENT_HINTS_OVERRIDE ?
goog.getObjectByName(USE_CLIENT_HINTS_OVERRIDE) :
null;
return override != null ? override : USE_CLIENT_HINTS;
})();
+211
View File
@@ -0,0 +1,211 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Utilities used by goog.labs.userAgent tools. These functions
* should not be used outside of goog.labs.userAgent.*.
*
*/
goog.module('goog.labs.userAgent.util');
goog.module.declareLegacyNamespace();
const {USE_CLIENT_HINTS} = goog.require('goog.labs.userAgent');
const {caseInsensitiveContains, contains} = goog.require('goog.string.internal');
/**
* @const {boolean} If true, use navigator.userAgentData without check.
* TODO(user) FEATURESET_YEAR >= 2022 if it supports mobile and all the
* brands we need.
*/
const ASSUME_CLIENT_HINTS_SUPPORT = false;
/**
* Gets the native userAgent string from navigator if it exists.
* If navigator or navigator.userAgent string is missing, returns an empty
* string.
* @return {string}
*/
function getNativeUserAgentString() {
const navigator = getNavigator();
if (navigator) {
const userAgent = navigator.userAgent;
if (userAgent) {
return userAgent;
}
}
return '';
}
/**
* Gets the native userAgentData object from navigator if it exists.
* If navigator.userAgentData object is missing or USE_CLIENT_HINTS is set to
* false, returns null.
* @return {?NavigatorUAData}
*/
function getNativeUserAgentData() {
if (!USE_CLIENT_HINTS) {
return null;
}
const navigator = getNavigator();
// TODO(user): Use navigator?.userAgent ?? null once it's supported.
if (navigator) {
return navigator.userAgentData || null;
}
return null;
}
/**
* Getter for the native navigator.
* @return {!Navigator}
*/
function getNavigator() {
return goog.global.navigator;
}
/**
* A possible override for applications which wish to not check
* navigator.userAgent but use a specified value for detection instead.
* @type {string}
*/
let userAgentInternal = getNativeUserAgentString();
/**
* A possible override for applications which wish to not check
* navigator.userAgentData but use a specified value for detection instead.
* @type {?NavigatorUAData}
*/
let userAgentDataInternal = getNativeUserAgentData();
/**
* Override the user agent string with the given value.
* This should only be used for testing within the goog.labs.userAgent
* namespace.
* Pass `null` to use the native browser object instead.
* @param {?string=} userAgent The userAgent override.
* @return {void}
*/
function setUserAgent(userAgent = undefined) {
userAgentInternal =
typeof userAgent === 'string' ? userAgent : getNativeUserAgentString();
}
/** @return {string} The user agent string. */
function getUserAgent() {
return userAgentInternal;
}
/**
* Override the user agent data object with the given value.
* This should only be used for testing within the goog.labs.userAgent
* namespace.
* Pass `null` to specify the absence of userAgentData. Note that this behavior
* is different from setUserAgent.
* @param {?NavigatorUAData} userAgentData The userAgentData override.
*/
function setUserAgentData(userAgentData) {
userAgentDataInternal = userAgentData;
}
/**
* If the user agent data object was overridden using setUserAgentData,
* reset it so that it uses the native browser object instead, if it exists.
*/
function resetUserAgentData() {
userAgentDataInternal = getNativeUserAgentData();
}
/** @return {?NavigatorUAData} Navigator.userAgentData if exist */
function getUserAgentData() {
return userAgentDataInternal;
}
/**
* Checks if any string in userAgentData.brands matches str.
* Returns false if userAgentData is not supported.
* @param {string} str
* @return {boolean} Whether any brand string from userAgentData contains the
* given string.
*/
function matchUserAgentDataBrand(str) {
const data = getUserAgentData();
if (!data) return false;
return data.brands.some(({brand}) => brand && contains(brand, str));
}
/**
* @param {string} str
* @return {boolean} Whether the user agent contains the given string.
*/
function matchUserAgent(str) {
const userAgent = getUserAgent();
return contains(userAgent, str);
}
/**
* @param {string} str
* @return {boolean} Whether the user agent contains the given string, ignoring
* case.
*/
function matchUserAgentIgnoreCase(str) {
const userAgent = getUserAgent();
return caseInsensitiveContains(userAgent, str);
}
/**
* Parses the user agent into tuples for each section.
* @param {string} userAgent
* @return {!Array<!Array<string>>} Tuples of key, version, and the contents of
* the parenthetical.
*/
function extractVersionTuples(userAgent) {
// Matches each section of a user agent string.
// Example UA:
// Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)
// AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405
// This has three version tuples: Mozilla, AppleWebKit, and Mobile.
const versionRegExp = new RegExp(
// Key. Note that a key may have a space.
// (i.e. 'Mobile Safari' in 'Mobile Safari/5.0')
'(\\w[\\w ]+)' +
'/' + // slash
'([^\\s]+)' + // version (i.e. '5.0b')
'\\s*' + // whitespace
'(?:\\((.*?)\\))?', // parenthetical info. parentheses not matched.
'g');
const data = [];
let match;
// Iterate and collect the version tuples. Each iteration will be the
// next regex match.
while (match = versionRegExp.exec(userAgent)) {
data.push([
match[1], // key
match[2], // value
// || undefined as this is not undefined in IE7 and IE8
match[3] || undefined // info
]);
}
return data;
}
exports = {
ASSUME_CLIENT_HINTS_SUPPORT,
extractVersionTuples,
getNativeUserAgentString,
getUserAgent,
getUserAgentData,
matchUserAgent,
matchUserAgentDataBrand,
matchUserAgentIgnoreCase,
resetUserAgentData,
setUserAgent,
setUserAgentData,
};