@enact/webos
Version:
webOS support library
167 lines (163 loc) • 5.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
exports.detect = detect;
exports.platform = exports.parseUserAgent = void 0;
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* Utilities for webOS platform detection.
*
* @module webos/platform
* @exports detect
* @exports platform
*/
var webOSVersion = [
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.2.1 Chrome/38.0.2125.122 Safari/537.36 WebAppManager
{
chrome: 38,
version: 3
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.34 Safari/537.36 WebAppManager
{
chrome: 53,
version: 4
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36 WebAppManager
{
chrome: 68,
version: 5
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 WebAppManager
{
chrome: 79,
version: 6
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 WebAppManager
{
chrome: 87,
version: 22
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.128 Safari/537.36 WebAppManager
{
chrome: 94,
version: 23
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.211 Safari/537.36 WebAppManager
{
chrome: 108,
version: 24
},
// Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.270 Safari/537.36 WebAppManager
{
chrome: 120,
version: 25
}];
var platforms = [].concat(_toConsumableArray(webOSVersion.map(function (_ref) {
var chrome = _ref.chrome,
version = _ref.version;
return {
regex: new RegExp("Web0S;.*Chrome/".concat(chrome)),
version: version,
chrome: chrome
};
})), [{
regex: /Web0S;.*Chrome\/(\d+)/
},
// Fallback
{
regex: /Web0S;/
}]);
var parseUserAgent = exports.parseUserAgent = function parseUserAgent(userAgent) {
// build out our cached platform determination for future usage
var platformInfo = {
webos: false
};
if (userAgent.indexOf('SmartTV') > -1) {
platformInfo.tv = true;
} else {
var _window$webOSSystem, _webOSSystem$stageRea;
var webOSSystem = (_window$webOSSystem = window.webOSSystem) !== null && _window$webOSSystem !== void 0 ? _window$webOSSystem : window.PalmSystem;
try {
var legacyInfo = JSON.parse(webOSSystem.deviceInfo || '{}');
if (typeof legacyInfo.platformVersionMajor !== 'undefined' && typeof legacyInfo.platformVersionMinor !== 'undefined') {
platformInfo.open = true;
} else {
platformInfo.unknown = true;
}
} catch (e) {
platformInfo.open = true;
}
webOSSystem === null || webOSSystem === void 0 || (_webOSSystem$stageRea = webOSSystem.stageReady) === null || _webOSSystem$stageRea === void 0 || _webOSSystem$stageRea.call(webOSSystem);
}
for (var index = 0, p, match; p = platforms[index]; index++) {
match = p.regex.exec(userAgent);
if (match) {
platformInfo.webos = true;
if (p.version) {
platformInfo.version = p.version;
}
if (p.chrome) {
platformInfo.chrome = p.chrome;
} else if (match[1]) {
// if a chrome version is detected
platformInfo.chrome = Number(match[1]);
}
break;
}
}
return platformInfo;
};
var _platform = null;
/**
* Returns the {@link webos/platform.platform} object.
*
* @function
* @returns {Object} The {@link webos/platform.platform} object
* @memberof webos/platform
* @public
*/
function detect() {
if (_platform) {
// if we've already determined the platform, we'll use that determination
return _platform;
} else if (typeof window === 'undefined' || !window.webOSSystem && !window.PalmSystem) {
// if window isn't available (in prerendering or snapshot runs), bail out early
return {
unknown: true
};
}
_platform = parseUserAgent(window.navigator.userAgent || '');
return _platform;
}
/**
* Provides identification of webOS variants.
*
* @readonly
* @type {Object}
* @property {Boolean} webos `true` for webOS
* @property {Boolean} [tv] `true` for LG webOS SmartTV
* @property {Boolean} [open] `true` for Open webOS
* @property {Boolean} [unknown] `true` for any unknown system
* @property {Number} [version] The version of the platform if detected
* @property {Number} [chrome] The version of Chrome if detected
* @memberof webos/platform
* @public
*/
var platform = exports.platform = {};
['tv', 'open', 'unknown', 'version', 'chrome'].forEach(function (name) {
Object.defineProperty(platform, name, {
enumerable: true,
get: function get() {
var p = detect();
return p[name];
}
});
});
var _default = exports["default"] = platform;