@enact/webos
Version:
webOS support library
189 lines (186 loc) • 7.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
exports.detect = detect;
exports.platform = exports.parseUserAgent = void 0;
var _deprecate = _interopRequireDefault(require("@enact/core/internal/deprecate"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /**
* 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
}];
var platforms = [
// LG webOS before adapting Chrome
{
regex: /Web0S;.*Safari\/537.41/,
version: 1
},
// using WebKit 537.41 for WebAppManager, using Chrome 26 for the browser app
{
regex: /Web0S;.*Safari\/538.2/,
version: 2
}].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('SmartWatch') > -1) {
platformInfo.watch = true;
} else if (userAgent.indexOf('SmartTV') > -1) {
platformInfo.tv = true;
} else if (userAgent.indexOf('Large Screen') > -1) {
(0, _deprecate["default"])({
name: 'Detecting webOS TV by "Large Screen" from the user agent string',
until: '5.0.0'
});
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 (legacyInfo.platformVersionMajor && legacyInfo.platformVersionMinor) {
var major = parseInt(legacyInfo.platformVersionMajor);
var minor = parseInt(legacyInfo.platformVersionMinor);
if (major < 3 || major === 3 && minor <= 0) {
platformInfo.legacy = true;
} else {
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} [watch] `true` for LG webOS SmartWatch. Deprecated: will be removed in 5.0.0.
* @property {Boolean} [open] `true` for Open webOS
* @property {Boolean} [legacy] `true` for legacy webOS (Palm and HP hardware). Deprecated: will be removed in 5.0.0.
* @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', 'watch', 'open', 'legacy', 'unknown', 'version', 'chrome'].forEach(function (name) {
Object.defineProperty(platform, name, {
enumerable: true,
get: function get() {
if (name === 'watch' || name === 'legacy') {
(0, _deprecate["default"])({
name: name,
until: '5.0.0'
});
}
var p = detect();
return p[name];
}
});
});
var _default = exports["default"] = platform;