|
16 | 16 |
|
17 | 17 | var t = d.getElementsByTagName("script")[0]; |
18 | 18 | var s = d.createElement("script"); |
19 | | - s.src = "https://yandex.ru/games/sdk/v2"; |
| 19 | + var url; |
| 20 | + // {{#yagames.sdk_url}} |
| 21 | + url = "{{{yagames.sdk_url}}}"; |
| 22 | + // {{/yagames.sdk_url}} |
| 23 | + s.src = url || "/sdk.js"; |
20 | 24 | s.async = true; |
21 | 25 | t.parentNode.insertBefore(s, t); |
22 | 26 | s.onload = function () { |
|
25 | 29 | options = {{{yagames.sdk_init_options}}}; |
26 | 30 | // {{/yagames.sdk_init_options}} |
27 | 31 |
|
28 | | - YaGames.init(options) |
29 | | - .then(function (ysdk) { |
30 | | - // {{#yagames.service_worker_url}} |
31 | | - var isNativeCache = false; |
32 | | - // {{#yagames.manifest_url}} |
33 | | - isNativeCache = ysdk.yandexApp && ysdk.yandexApp.enabled; |
34 | | - // {{/yagames.manifest_url}} |
35 | | - if ("serviceWorker" in navigator) { |
36 | | - if (isNativeCache) { |
37 | | - // Force unregister all Service Workers |
38 | | - navigator.serviceWorker.getRegistrations().then(function (registrations) { |
39 | | - registrations.forEach(function (registration) { |
40 | | - registration.unregister().then(function (isUnregistered) { |
41 | | - console.info("YaGames: Service Worker unregistration result", isUnregistered); |
| 32 | + try { |
| 33 | + YaGames.init(options) |
| 34 | + .then(function (ysdk) { |
| 35 | + // {{#yagames.service_worker_url}} |
| 36 | + var isNativeCache = false; |
| 37 | + // {{#yagames.manifest_url}} |
| 38 | + isNativeCache = ysdk.yandexApp && ysdk.yandexApp.enabled; |
| 39 | + // {{/yagames.manifest_url}} |
| 40 | + if ("serviceWorker" in navigator) { |
| 41 | + if (isNativeCache) { |
| 42 | + // Force unregister all Service Workers |
| 43 | + navigator.serviceWorker.getRegistrations().then(function (registrations) { |
| 44 | + registrations.forEach(function (registration) { |
| 45 | + registration.unregister().then(function (isUnregistered) { |
| 46 | + console.info("YaGames: Service Worker unregistration result", isUnregistered); |
| 47 | + }); |
42 | 48 | }); |
43 | 49 | }); |
44 | | - }); |
45 | | - } else { |
46 | | - // Register Service Worker |
47 | | - navigator.serviceWorker.register("{{yagames.service_worker_url}}").then( |
48 | | - function (registration) { |
49 | | - console.info("YaGames: Service Worker registration successful with scope", registration.scope); |
50 | | - }, |
51 | | - function (err) { |
52 | | - console.info("YaGames: Service Worker registration failed", err); |
53 | | - } |
54 | | - ); |
| 50 | + } else { |
| 51 | + // Register Service Worker |
| 52 | + navigator.serviceWorker.register("{{yagames.service_worker_url}}").then( |
| 53 | + function (registration) { |
| 54 | + console.info("YaGames: Service Worker registration successful with scope", registration.scope); |
| 55 | + }, |
| 56 | + function (err) { |
| 57 | + console.info("YaGames: Service Worker registration failed", err); |
| 58 | + } |
| 59 | + ); |
| 60 | + } |
55 | 61 | } |
56 | | - } |
57 | | - // {{/yagames.service_worker_url}} |
| 62 | + // {{/yagames.service_worker_url}} |
58 | 63 |
|
59 | | - // {{#yagames.sdk_init_snippet}} |
60 | | - {{{yagames.sdk_init_snippet}}} |
61 | | - // {{/yagames.sdk_init_snippet}} |
| 64 | + // {{#yagames.sdk_init_snippet}} |
| 65 | + {{{yagames.sdk_init_snippet}}} |
| 66 | + // {{/yagames.sdk_init_snippet}} |
62 | 67 |
|
63 | | - send(0, "init", ysdk); |
64 | | - }) |
65 | | - .catch(function (err) { |
66 | | - send(0, "error", err + ""); |
67 | | - }); |
| 68 | + send(0, "init", ysdk); |
| 69 | + }) |
| 70 | + .catch(function (err) { |
| 71 | + send(0, "error", err + ""); |
| 72 | + }); |
| 73 | + } catch (err) { |
| 74 | + // In the case that the user specified an incorrect URL and YaGames is undefined. |
| 75 | + send(0, "error", "Incorrect SDK. " + err); |
| 76 | + } |
68 | 77 | }; |
69 | 78 | s.onerror = function () { |
70 | 79 | send(0, "error", "Error loading SDK. Reload the page."); |
|
0 commit comments