@seznam/seznam.cz-browser-game-module-api
Version:
Abstraction API for integrating video game modules with the Seznam.cz web browser's internal APIs.
211 lines • 8.69 kB
JavaScript
const sbrowserAndroidApiBindings = (typeof sbrowser_games === 'object' && sbrowser_games ? sbrowser_games : {});
const sbrowserIOSApiBindings = (() => {
if (typeof webkit !== 'object' || !webkit || !('messageHandlers' in webkit)) {
return {};
}
const webkitWithMessageHandlers = webkit;
if (typeof webkitWithMessageHandlers.messageHandlers !== 'object' || !webkitWithMessageHandlers.messageHandlers) {
return {};
}
return webkitWithMessageHandlers.messageHandlers;
})();
export function isTablet() {
if (typeof navigator === 'object' && navigator && typeof navigator.userAgent === 'string') {
if (/\(iPad;/.test(navigator.userAgent)) {
return true;
}
}
if (typeof sbrowserAndroidApiBindings.isTablet === 'function') {
try {
return !!sbrowserAndroidApiBindings.isTablet();
}
catch (sbrowserApiError) {
console.error('SBrowser API.isTablet: failed to execute the isTabled method', sbrowserApiError);
return false;
}
}
console.warn('SBrowser API.isTablet: no supported SBrowser native API is available, defaulting to false');
return false;
}
export function terminateApp() {
return (callNativeVoidReturningMethod('terminateApp') ||
callNativeVoidReturningMethod('terminate', [], 'terminate'));
}
export function gamesPlay(gameId) {
callNativeVoidReturningMethod('gamesPlay', [gameId]);
}
export function gamesExit() {
return callNativeVoidReturningMethod('gamesExit');
}
export function submitUsageStatistics(gameId, gamesPlayed, gamesWon) {
return callNativeVoidReturningMethod('submitUsageStatistics', [gameId, gamesPlayed, gamesWon], JSON.stringify({
game: gameId,
'games-played': gamesPlayed,
'games-won': gamesWon,
}));
}
export function openLoginForm() {
return callNativeVoidReturningMethod('openLoginForm');
}
export function isSignedIn() {
const iosIsSignedIn = (sbrowserIOSApiBindings.isSignedIn &&
typeof sbrowserIOSApiBindings.isSignedIn.postMessage === 'function' ?
sbrowserIOSApiBindings.isSignedIn
:
null);
return new Promise((resolve, reject) => {
if (typeof sbrowserAndroidApiBindings.isSignedIn === 'function') {
try {
const result = sbrowserAndroidApiBindings.isSignedIn();
resolve(result);
return;
}
catch (sbrowserApiError) {
console.error('SBrowser API.isSignedIn: Failed to execute the isSignedIn method', sbrowserApiError);
reject(sbrowserApiError);
return;
}
}
else if (iosIsSignedIn && typeof iosIsSignedIn.postMessage === 'function') {
try {
window.sbrowser_games = window.sbrowser_games || {};
window.sbrowser_games.isSignedIn = (isUserSignedIn) => {
resolve(!!isUserSignedIn);
if (window.sbrowser_games && typeof window.sbrowser_games.isSignedIn === 'function') {
delete window.sbrowser_games.isSignedIn;
}
};
iosIsSignedIn.postMessage('');
return;
}
catch (sbrowserApiError) {
console.error('SBrowser API.isSignedIn: Failed to execute the isSignedIn method', sbrowserApiError);
reject(sbrowserApiError);
return;
}
}
else {
reject(new Error('No supported native SBrowser API is available'));
}
});
}
export function getStorage() {
const methods = ['get', 'set', 'delete'];
if (methods.some(methodName => typeof sbrowserAndroidApiBindings[`storage_${methodName}`] !== 'function') &&
methods.some(methodName => !sbrowserIOSApiBindings[`storage_${methodName}`] ||
typeof sbrowserIOSApiBindings[`storage_${methodName}`].postMessage !== 'function')) {
return null;
}
return {
get(key) {
return executeOperation('storage_get', key);
},
set(key, value) {
return executeOperation('storage_set', key, value);
},
delete(key) {
return executeOperation('storage_delete', key);
},
};
function executeOperation(methodName, key, value = undefined) {
return new Promise((resolve, reject) => {
const { callbackID } = createCallback(resolve, reject);
const serializedValue = value === undefined ? value : JSON.stringify(value);
const androidArgs = value === undefined ? [key] : [key, serializedValue];
const iOSArg = JSON.stringify(value === undefined ? { key, callbackID } : { key, value: serializedValue, callbackID });
const result = callNativeMethod(methodName, androidArgs, iOSArg);
if (result.usedImplementation === 'Android') {
delete window.sbrowser_games[callbackID];
if (result.thrownError) {
reject(result.thrownError);
}
else {
const { returnedValue } = result;
resolve(typeof returnedValue === 'string' ? JSON.parse(returnedValue) : null);
}
}
});
}
function createCallback(resolve, reject) {
const callbackID = createGlobalCallback((error, value) => {
if (error) {
reject(error);
}
else if (value === null || value === undefined) {
resolve(null);
}
else {
resolve(JSON.parse(value));
}
});
const callback = window.sbrowser_games[callbackID];
return { callbackID, callback };
}
function createGlobalCallback(callback) {
window.sbrowser_games = window.sbrowser_games || {};
const callbackIdPrefix = `${Date.now()}` + Math.floor(Math.random() * 8192);
let idCounter = 0;
while (`callback_${callbackIdPrefix}_${idCounter}` in window.sbrowser_games) {
idCounter++;
}
const callbackId = `callback_${callbackIdPrefix}_${idCounter}`;
window.sbrowser_games[callbackId] = (error, value) => {
delete window.sbrowser_games[callbackId];
callback(error, value);
};
return callbackId;
}
}
function callNativeVoidReturningMethod(methodName, androidArguments = [], iosArgument = '') {
const callResult = callNativeMethod(methodName, androidArguments, iosArgument);
return callResult.usedImplementation !== null && callResult.thrownError === null;
}
function callNativeMethod(methodName, androidArguments = [], iosArgument = '') {
if (typeof sbrowserAndroidApiBindings[methodName] === 'function') {
try {
const returnedValue = sbrowserAndroidApiBindings[methodName](...androidArguments);
return {
usedImplementation: 'Android',
returnedValue,
thrownError: null,
};
}
catch (sbrowserApiError) {
console.error(`SBrowser API.${methodName}: Failed to execute the ${methodName} method`, sbrowserApiError);
return {
usedImplementation: 'Android',
returnedValue: undefined,
thrownError: sbrowserApiError instanceof Error ? sbrowserApiError : new Error(`${sbrowserApiError}`),
};
}
}
const iosMethodBinding = (sbrowserIOSApiBindings[methodName] &&
typeof sbrowserIOSApiBindings[methodName].postMessage === 'function' ?
sbrowserIOSApiBindings[methodName]
:
null);
if (iosMethodBinding && typeof iosMethodBinding.postMessage === 'function') {
try {
const returnedValue = iosMethodBinding.postMessage(iosArgument);
return {
usedImplementation: 'iOS',
returnedValue,
thrownError: null,
};
}
catch (sbrowserApiError) {
console.error(`SBrowser API.${methodName}: Failed to execute the ${methodName} method`, sbrowserApiError);
return {
usedImplementation: 'iOS',
returnedValue: undefined,
thrownError: sbrowserApiError instanceof Error ? sbrowserApiError : new Error(`${sbrowserApiError}`),
};
}
}
return {
usedImplementation: null,
returnedValue: undefined,
thrownError: null,
};
}
//# sourceMappingURL=index.js.map