This JavaScript code defines and initializes a loader for **Yandex...

August 23, 2025 at 10:47 AM

var YaGamesLoader; ( () => { "use strict"; var e = { r: e => { "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e, "__esModule", { value: !0 }) } }; ( () => { if (void 0 !== e) { var r = e.u , o = e.e , t = {} , a = {}; e.u = function(e) { return r(e) + (t.hasOwnProperty(e) ? "?" + t[e] : "") } , e.e = function(t) { return o(t).catch((function(o) { var n = a.hasOwnProperty(t) ? a[t] : 3; if (n < 1) { var s = r(t); throw o.message = "Loading chunk " + t + " failed after 3 retries.\n(" + s + ")", o.request = s, o } return new Promise((function(r) { setTimeout((function() { a[t] = n - 1, r(e.e(t)) } ), 0) } )) } )) } } } )(); var r = {}; e.r(r); var o, t = Object.defineProperty, a = Object.getOwnPropertySymbols, n = Object.prototype.hasOwnProperty, s = Object.prototype.propertyIsEnumerable, i = (e, r, o) => r in e ? t(e, r, { enumerable: !0, configurable: !0, writable: !0, value: o }) : e[r] = o, d = (e, r) => { for (var o in r || (r = {})) n.call(r, o) && i(e, o, r[o]); if (a) for (var o of a(r)) s.call(r, o) && i(e, o, r[o]); return e } ; if (window === window.top || window.sdkLoaderWasInited) { const e = window.sdkLoaderWasInited ? "The SDK Loader was loaded more than once" : "SDK initialization outside of frame"; console.warn(e) } else { let e, r, t, a; window.sdkLoaderWasInited = !0; let n = !1 , s = !1 , i = !1; window.isSdkLoader = !0, window.YaGames = { init: o => { var d; return s ? null == (d = window.YaGames) ? void 0 : d.init(o) : i ? Promise.reject(v) : a ? (console.warn("YaGames was initialized"), a) : (a = new Promise(( (a, s) => { e = a, r = s, t = o, n = !0 } )), a) } }; const c = { type: "unknown", isMobile: () => !1, isTablet: () => !1, isDesktop: () => !1, isTV: () => !1 } , l = (e, r={}) => { var o, t; window.parent.postMessage({ source: "YandexGamesSDK", messageId: `${Date.now()}-${Math.random()}`, type: "error", data: { block: "ErrorCounter/common: sdk-loader", error: { message: `[SDK LOADER]: ${e}` }, additional: d(d(d({}, r), (null == (o = r.error) ? void 0 : o.message) ? { originalMessage: null == (t = r.error) ? void 0 : t.message } : {}), r.source ? { originalSource: r.source } : {}), level: "error", source: "sdk-loader", type: "error" } }, "*") } ; Object.defineProperty(window.YaGames, "deviceInfo", { get: () => (l("accessing deviceInfo in YaGames"), c) }); const u = e => { const r = document.querySelectorAll(`script[src="${e}"]`); for (let e of Array.from(r)) e.remove() } , m = /^(https|yandexgames):\/\/[^\/][^.][\w.\/\-]+\/sdk\/v2(\?|$)/ , w = /(^|yandex\.net)\/sdk\/(v\d+|_)\/v2(\.[0-9a-f]*)?\.js/ , f = "url sdk not valid" , v = "load sdk file error" , p = 3 , g = (o, a) => { const d = document.createElement("script"); d.src = o, d.onload = () => { var o; s = !0, n && (null == (o = window.YaGames) || o.init(t).then((r => { e(r) } )).catch((e => { l("Error while init sdk", { error: e }), r(e) } ))) } , d.onerror = (e, t, s, d, c) => { u(o), a > 0 ? g(o, --a) : (i = !0, l(v, { event: e, error: c, source: t }), n && r(v)) } , (document.head || document.documentElement).append(d) } , h = (e, o="") => { if (e && (m.test(e) || w.test(e))) { const r = new URL(e,location.origin); o && r.searchParams.set("dvh", o), g(r.toString(), p) } else i = !0, n && r(f), l(f) } , y = new URLSearchParams(location.search).get("sdk"); y ? h(y) : (o = function*() { return new Promise(( (e, r) => { const o = `${Date.now()}-${Math.random()}` , t = setTimeout(( () => { r(new Error("Get external iframe timeout")) } ), 500); window.addEventListener("message", (function r(a) { const {data: n} = function(e) { try { return { data: JSON.parse(e), error: null } } catch (e) { return { data: null, error: e } } }(a.data); n && n.messageId === o && (window.removeEventListener("message", r), clearTimeout(t), e(n.payload)) } )), window.parent.postMessage(JSON.stringify({ source: "YandexGamesSDK", actionName: "GET_IFRAME_ORIGIN_SRC", channel: "EARLY_SDK_EVENT", messageId: o }), "*") } )) } , new Promise(( (e, r) => { var t = e => { try { n(o.next(e)) } catch (e) { r(e) } } , a = e => { try { n(o.throw(e)) } catch (e) { r(e) } } , n = r => r.done ? e(r.value) : Promise.resolve(r.value).then(t, a); n((o = o.apply(void 0, null)).next()) } ))).then((e => { const r = new URL(e) , o = r.searchParams , t = o.get("sdk") , a = o.get("dvh") || ""; if (t) h(t, a); else { const e = new URLSearchParams(r.search).get("sdk"); h(e) } } )).catch((e => { l("Error while get iframe src", { error: e }), console.error("SDK initialization failed", e) } )) } YaGamesLoader = r } )(); //# sourceMappingURL=https://s3.mdst.yandex.net/games/source-maps/_/sdk/sdk-loader/sdk.js.map

This JavaScript code defines and initializes a loader for Yandex Games SDK (YaGamesLoader). The function of this loader is to facilitate initializing the Yandex Games SDK within an environment, typically an iframe or a game hosted on the Yandex Games platform. Here's a summary of what the code does:

1. Module System Simulation

  • The first part of the code defines a module-like environment using the object e, which maintains compatibility with module exports (__esModule and Symbol.toStringTag) to ensure interoperability with modular JavaScript.

2. Retry Logic for Loading Chunks

  • The code customizes e.u and e.e to modify how JavaScript chunks are loaded dynamically. If a failure occurs while loading a chunk, it retries the request up to 3 times. If it fails after all retries, it logs an error.

3. SDK Initialization Check

  • The code checks whether the SDK loader is already initialized (sdkLoaderWasInited) or if it is running in the correct environment (e.g., not in the parent window but within an iframe). If it detects improper usage or reinitialization, it logs warnings to the console.

4. Defines the YaGames Object

  • A YaGames object is added to the window object to handle SDK initialization and provide access to relevant features, such as:
    • YaGames.init: A method to initialize the SDK asynchronously with optional configuration (o).
    • YaGames.deviceInfo: Provides an object representing the type of device the SDK is running on (e.g., mobile, desktop) and logs info/warnings when accessed.

5. Script Loader with Validation

  • The function h performs validation of SDK JavaScript file URLs to ensure they are appropriate (specific to Yandex domains).
  • If the URL is valid, it dynamically loads the corresponding SDK script using the document.createElement("script") method.
  • Error handling is included to retry the script load (g), and if all retries fail, the error is logged, and initialization promises are rejected.

6. Handling Communication Between Iframe and Parent Window

  • Communication between the iframe (where this code runs) and the parent window is managed using postMessage.
  • The code sends messages to the parent to request the SDK file’s source or report errors (e.g., invalid SDK URLs, loading errors).
  • It sets up message event listeners to process responses and initialize the SDK after receiving the required information.

7. Error Reporting

  • Errors encountered during the SDK loading process are logged and reported using a structured message format (postMessage). This might include:
    • Invalid URLs.
    • SDK loading failures.
    • Errors during initialization.

Purpose

The primary goal of this code is to initialize the Yandex Games SDK, dynamically loading it if needed, while ensuring the loading process is robust (with retries) and executed within the proper iframe environment. It provides functionalities for application developers to integrate their games with Yandex's platform securely and transparently.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node