UNPKG

@benshi.ai/js-sdk

Version:

Benshi SDK

234 lines 10.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = exports.isDevEnvironment = exports.listenPageScrolled = exports.listenPageChanged = exports.listenApplicationVisibilityChange = exports.listenApplicationClose = exports.listenLocationChangedEvent = exports.getNetworkInformation = exports.getOperatingSystem = exports.getDeviceId = void 0; var os_1 = require("os"); var uuid_1 = require("uuid"); var is_in_browser_1 = __importDefault(require("is-in-browser")); var VISIBILITY_HIDDEN = 'hidden'; var VISIBILITY_VISIBLE = 'visible'; var visibilityInitialized = false; function getDeviceId() { if (!is_in_browser_1.default) { var interfaces_1 = (0, os_1.networkInterfaces)(); var macs = Object.keys(interfaces_1) .map(function (key) { return interfaces_1[key] .filter(function (info) { return !info.internal; }) .map(function (info) { return info.mac; }); }) .flat(); if (macs.length !== 0) { return macs[0].split(':').join(''); } else { return ''; } } var deviceIdStorageKey = 'deviceIdStorageKey'; var deviceId; if (localStorage.getItem(deviceIdStorageKey) === null) { deviceId = (0, uuid_1.v4)(); localStorage.setItem(deviceIdStorageKey, deviceId); } else { deviceId = localStorage.getItem(deviceIdStorageKey); } return deviceId; } exports.getDeviceId = getDeviceId; function getOperatingSystem() { var _a; if (!is_in_browser_1.default) { return "".concat((0, os_1.platform)(), "-").concat((0, os_1.release)()); } // Current web specification defines `window.navigator.userAgentData.platform // as the official way to retrieve the operating system, but it is not // widely adopted by browsers. To avoid that problem this function // also takes into account the deprecated mechanism `window.navigator.platform` if (!(window === null || window === void 0 ? void 0 : window.navigator)) { return ''; } var platform = (_a = window.navigator["userAgentData"]) === null || _a === void 0 ? void 0 : _a.platform; var platformLegacy = window.navigator.platform; return platform || platformLegacy; } exports.getOperatingSystem = getOperatingSystem; function getNetworkInformation() { if (!is_in_browser_1.default) { return { downlink: 0, uplink: 0 }; } // in case the speed is not available, we have agreed to send 0 // otherwise, return the network speed in kbps var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection; if (!connection) { return { downlink: 0, uplink: 0 }; } return { downlink: parseInt(((connection.downlink || 0) * 1000).toString()), uplink: 0 }; } exports.getNetworkInformation = getNetworkInformation; function listenLocationChangedEvent(clb) { var originalPushState = window.history.pushState; var originalReplaceState = window.history.replaceState; window.history.pushState = function (data, title, url) { clb("".concat(window.location.origin, "/").concat(url)); return originalPushState.call(window.history, data, title, url); }; window.history.replaceState = function (data, title, url) { var currentPath = url || ''; // first time is undefined clb("".concat(window.location.origin, "/").concat(currentPath)); return originalReplaceState.call(window.history, data, title, url); }; window.addEventListener('popstate', function (evt) { clb(evt.target.location.href); }); } exports.listenLocationChangedEvent = listenLocationChangedEvent; function listenApplicationClose(clb) { window.addEventListener('unload', function () { clb(); }); } exports.listenApplicationClose = listenApplicationClose; function listenApplicationVisibilityChange(clb_visible, clb_hide) { if (document.visibilityState === VISIBILITY_VISIBLE) { visibilityInitialized = true; } document.addEventListener('visibilitychange', function (e) { console.warn('[event] [visibilitychange] ', document.visibilityState); if (document.visibilityState === VISIBILITY_VISIBLE) { visibilityInitialized = true; clb_visible(); } else if (document.visibilityState === VISIBILITY_HIDDEN) { if (!visibilityInitialized) { return; } clb_hide(); } }); } exports.listenApplicationVisibilityChange = listenApplicationVisibilityChange; function listenPageChanged(clb) { listenLocationChangedEvent(function (newUrl) { // the reason of this delay is that the new title // is not available yet when the location-changed // event is fired setTimeout(function () { clb(newUrl, document.title); }, 10); }); } exports.listenPageChanged = listenPageChanged; function listenPageScrolled(threshold, clb) { // const currentScrollTop = (): number => (window.pageYOffset || document.documentElement.scrollTop) - (document.documentElement.clientTop || 0) var getScrollPercent = function () { var SCROLL_TOP = 'scrollTop'; var SCROLL_HEIGHT = 'scrollHeight'; var currentScrollTop = document.documentElement[SCROLL_TOP] || document.body[SCROLL_TOP]; var totalScrollHeight = document.documentElement[SCROLL_HEIGHT] || document.body[SCROLL_HEIGHT]; var visibleHeight = document.documentElement.clientHeight; return currentScrollTop / (totalScrollHeight - visibleHeight) * 100; }; window.addEventListener('scroll', function (e) { var scroll = getScrollPercent(); if (scroll < threshold) { return; } clb(scroll); }); } exports.listenPageScrolled = listenPageScrolled; // legacy function. Leave it here for reusing it when needed function getContext() { return __awaiter(this, void 0, void 0, function () { var context, battery; return __generator(this, function (_a) { switch (_a.label) { case 0: context = {}; if (!!!navigator) return [3 /*break*/, 2]; return [4 /*yield*/, navigator.getBattery()]; case 1: battery = (_a.sent()) || null; context.browser = { user_agent: window.navigator.userAgent, languages: __spreadArray([], window.navigator.languages, true), online: navigator.onLine, battery: battery ? { charging: battery.charging, chargingTime: battery.chargingTime, dischargingTime: battery.dischargingTime, level: battery.level } : null }; _a.label = 2; case 2: return [2 /*return*/, context]; } }); }); } function isDevEnvironment() { return document.location.search.includes('bsenv=dev'); } exports.isDevEnvironment = isDevEnvironment; function init() { window.addEventListener('beforeunload', function () { }); } exports.init = init; //# sourceMappingURL=BsSystem.js.map