@swrve/smarttv-sdk
Version:
Swrve marketing engagement platform SDK for SmartTV OTT devices
287 lines (286 loc) • 9.94 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculatePPI = exports.detectScreenDiagonal = exports.clearLocalStorage = void 0;
const IPlatform_1 = require("./IPlatform");
const uuid_1 = require("../uuid");
const SwrveLogger_1 = __importDefault(require("../SwrveLogger"));
const protectedKeyPrefix = /^swrve\./;
function clearLocalStorage(force = false) {
const keys = [];
for (let i = 0; i < localStorage.length; i += 1) {
const key = localStorage.key(i);
if (key && force) {
keys.push(key);
}
else if (key && !protectedKeyPrefix.test(key)) {
keys.push(key);
}
}
keys.forEach(key => localStorage.removeItem(key));
}
exports.clearLocalStorage = clearLocalStorage;
if (typeof window !== "undefined" && window.localStorage && window.localStorage.clear !== clearLocalStorage) {
window.localStorage.clear = clearLocalStorage;
}
/**
* Base Platform (also the browser platform)
*/
class BasePlatform {
constructor(config) {
this.defaultMapping = {
36: "Return",
38: "Up",
40: "Down",
37: "Left",
39: "Right",
13: "Enter",
65: "A",
66: "B",
67: "C",
68: "D",
8: "Back",
179: "Play",
227: "FastForward",
228: "Rewind",
112: "F1",
};
/** True if the platform needs a proxy. */
this.needsProxy = true;
/** True if this platform supports the magic wand. */
this.supportsMagicWandNatively = false;
/** Number of history entries on start. */
this.startHistoryLength = 0;
this.networkListeners = [];
if (config) {
this.config = config;
if (config.customKeyMappingBase) {
this.defaultMapping = config.customKeyMappingBase;
}
}
}
name() {
var _a, _b;
if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.customDeviceName) {
return (_b = this.config) === null || _b === void 0 ? void 0 : _b.customDeviceName;
}
else {
return {
name: "Browser",
variation: "Base",
};
}
}
init(deviceProperties) {
if (typeof window !== "undefined") {
this.startHistoryLength = window.history.length;
}
const cache = document.createElement("div");
cache.id = "PALImageCache";
cache.style.overflow = "hidden";
cache.style.position = "absolute";
cache.style.left = "-10000px";
cache.style.width = "1px";
cache.style.height = "1px";
if (document.getElementById("PALImageCache") === null) {
document.body.appendChild(cache);
}
SwrveLogger_1.default.info("PAL init");
return Promise.resolve();
}
monitorNetwork(networkListener) {
if (this.networkMonitorHandle === undefined) {
this.networkMonitorHandle = this.initNetworkListener();
}
this.networkListeners.push(networkListener);
return networkListener;
}
stopMonitoringNetwork(networkListener) {
this.networkListeners = this.networkListeners.filter(listener => listener !== networkListener);
if (this.networkListeners.length === 0 && this.networkMonitorHandle !== undefined) {
this.removeNetworkListener(this.networkMonitorHandle);
delete this.networkMonitorHandle;
}
}
getNeedsProxy() {
return this.needsProxy;
}
getSupportsMagicWandNatively() {
return this.supportsMagicWandNatively;
}
disableScreenSaver() {
SwrveLogger_1.default.error("platform does not know how to disable screensaver");
}
openLink(link) {
if (typeof window !== "undefined") {
window.open(link);
}
}
enableScreenSaver() {
SwrveLogger_1.default.error("platform does not know how to enable screensaver");
}
get synchronousStorage() {
if (typeof window !== "undefined") {
return window.localStorage;
}
else {
return null;
}
}
downloadAssets(assets) {
const downloading = assets.map((asset) => {
const img = document.createElement("img");
img.src = asset.path;
SwrveLogger_1.default.info("PAL download " + asset.path);
const imageCache = document.getElementById("PALImageCache");
if (imageCache) {
imageCache.appendChild(img);
}
else {
SwrveLogger_1.default.info(" PAL: Image cache does not exist");
}
return new Promise((resolve, reject) => {
img.addEventListener("load", () => {
resolve();
});
img.addEventListener("error", () => {
reject();
});
});
});
return Promise.all(downloading)
.then(() => void 0);
}
exit() {
const backlength = window.history.length - this.startHistoryLength - 1;
window.history.go(-backlength);
}
get deviceID() {
const localStorage = this.synchronousStorage;
if (localStorage) {
let saved = localStorage.getItem("swrve.deviceid");
if (!saved) {
saved = uuid_1.generateUuid().toString();
localStorage.setItem("swrve.deviceid", saved);
}
return saved;
}
else {
return "";
}
}
get firmware() {
return 'Not Supported';
}
get timezone() {
if (this._timezone === undefined) {
this._timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
}
return this._timezone || "";
}
get region() {
if (this._region === undefined) {
this._region = this.timezone.indexOf('/') !== -1 ? this.timezone.split('/')[0] : "";
}
return this._region || "";
}
getDeviceBrowserVersion() {
const match = navigator == null || navigator.appVersion == null
? null
: navigator.appVersion.match(/^[^\s]*/);
return match == null
? null
: match[0];
}
get model() {
return "";
}
get os() {
var _a, _b;
return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.customOS) !== null && _b !== void 0 ? _b : "web";
}
get osVersion() {
var _a, _b;
if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.customOSVersion) {
return (_b = this.config) === null || _b === void 0 ? void 0 : _b.customOSVersion;
}
else {
const match = navigator.userAgent.match(/[^\s]+$/);
return match ? match[0] : "Unknown version";
}
}
get appStore() {
var _a, _b;
return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.customAppStore) !== null && _b !== void 0 ? _b : "google";
}
get language() {
if (this._language === undefined && typeof navigator !== "undefined") {
this._language = navigator.language.split('-')[0];
}
return this._language || "";
}
get countryCode() {
if (this._countryCode === undefined && typeof navigator !== "undefined") {
this._countryCode = navigator.language.split('-')[1];
}
return this._countryCode || "";
}
get screenDPI() {
if (this._screenDPI === undefined) {
this._screenDPI = calculatePPI(this.screenWidth, this.screenHeight, detectScreenDiagonal(this.model));
}
return this._screenDPI;
}
get screenHeight() {
if (this._screenHeight === undefined) {
this._screenHeight = typeof navigator !== "undefined" ? window.innerHeight : 0;
}
return this._screenHeight || 0;
}
get screenWidth() {
if (this._screenWidth === undefined) {
this._screenWidth = typeof navigator !== "undefined" ? window.innerWidth : 0;
}
return this._screenWidth || 0;
}
supportsHDR() {
return false;
}
getKeymapping() {
return this.defaultMapping;
}
triggerNetworkChange(status) {
this.networkListeners.forEach(listener => listener(status));
}
initNetworkListener() {
const onOnline = () => this.triggerNetworkChange(IPlatform_1.NETWORK_CONNECTED);
const onOffline = () => this.triggerNetworkChange(IPlatform_1.NETWORK_DISCONNECTED);
window.addEventListener('offline', onOffline);
window.addEventListener('online', onOnline);
return [onOnline, onOffline];
}
removeNetworkListener(handle) {
const [onOnline, onOffline] = handle;
window.removeEventListener('offline', onOffline);
window.removeEventListener('online', onOnline);
}
}
exports.default = BasePlatform;
// First numeric part of Samsung and LG model names indicates screen size in inches
function detectScreenDiagonal(modelName) {
let size;
const match = modelName.match(/\d+/);
if (match) {
size = parseInt(match[0], 10);
}
// fallback to median screen size on the market
return size || 50;
}
exports.detectScreenDiagonal = detectScreenDiagonal;
function calculatePPI(pixelWidth, pixelHeight, inchDiagonal) {
const pixelDiagonal = Math.sqrt((pixelWidth * pixelWidth) + (pixelHeight * pixelHeight));
return Math.round(pixelDiagonal / inchDiagonal);
}
exports.calculatePPI = calculatePPI;