@inappstory/game-center-api 1.3.7 → 1.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/logger.html CHANGED
@@ -24,6 +24,56 @@
24
24
  // canTryReload - for Script error. and unexpected EOL
25
25
 
26
26
  if (window.gameLoadFailed == null) {
27
+ var isAndroid = Boolean(window.Android);
28
+ var isIos = Boolean(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.gameLoaded);
29
+ var isWeb = !isAndroid && !isIos;
30
+ var sourceWindow, sourceWindowOrigin;
31
+
32
+ function initWebSource(event) {
33
+ // call only for initGame msg from sdk
34
+ // prevent override webSource.sourceWindowOrigin from game itself msg (mobile safari issue)
35
+ sourceWindow = event.source;
36
+ sourceWindowOrigin = event.origin;
37
+
38
+ // enable broadcast for corner case (VK WKWebView)
39
+ if (event.origin === "null" || event.origin == null || !Boolean(event.origin)) {
40
+ sourceWindowOrigin = "*";
41
+ }
42
+
43
+ // save to gameLoadingInfo
44
+ if (window.gameLoadingInfo == null) {
45
+ window.gameLoadingInfo = {
46
+ sourceWindow: sourceWindow,
47
+ sourceWindowOrigin: sourceWindowOrigin,
48
+ };
49
+ }
50
+ }
51
+
52
+ if (isWeb) {
53
+ var messageListener = function (event) {
54
+ var data = event.data;
55
+
56
+ if (Array.isArray(data)) {
57
+ switch (data[0]) {
58
+ case "initGame":
59
+ initWebSource(event);
60
+ window.removeEventListener("message", messageListener);
61
+ break;
62
+ case "cb":
63
+ if (data[1] && data[1].cb != null) {
64
+ if (data[1].cb === "initGame") {
65
+ initWebSource(event);
66
+ window.removeEventListener("message", messageListener);
67
+ }
68
+ }
69
+ break;
70
+ }
71
+ }
72
+ };
73
+
74
+ window.addEventListener("message", messageListener, false);
75
+ }
76
+
27
77
  window.gameLoadFailed = function (reason, canTryReload) {
28
78
  if (window.Android) {
29
79
  if (window.Android.gameLoadFailed) {
@@ -31,20 +81,47 @@
31
81
  }
32
82
  } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.gameLoaded) {
33
83
  if (window.webkit.messageHandlers.gameLoadFailed) {
34
- window.webkit.messageHandlers.gameLoadFailed.postMessage(JSON.stringify({ reason: reason, canTryReload: canTryReload }));
84
+ window.webkit.messageHandlers.gameLoadFailed.postMessage(
85
+ JSON.stringify({
86
+ reason: reason,
87
+ canTryReload: canTryReload,
88
+ })
89
+ );
90
+ }
91
+ } else if (isWeb) {
92
+ if (sourceWindow != null && sourceWindowOrigin != null) {
93
+ sourceWindow.postMessage(["gameLoadFailed", reason, canTryReload], sourceWindowOrigin);
35
94
  }
36
95
  }
37
96
  };
38
97
  }
39
98
 
40
99
  setTimeout(function () {
41
- if (!window.gameLoadingInfo.loadStarted) {
100
+ let loadStarted = false;
101
+ let error = "";
102
+ if (window.gameLoadingInfo != null) {
103
+ if (window.gameLoadingInfo.loadStarted != null) {
104
+ loadStarted = window.gameLoadingInfo.loadStarted;
105
+ }
106
+ if (window.gameLoadingInfo.error != null) {
107
+ error = window.gameLoadingInfo.error;
108
+ }
109
+ }
110
+ if (!loadStarted) {
42
111
  // call only on serious js failure (when loadStarted false - parse error, Syntax error, EOL error)
43
- window.gameLoadFailed(window.gameLoadingInfo.error, false);
112
+ if (window.gameLoadFailed != null) {
113
+ window.gameLoadFailed(error, false);
114
+ }
44
115
  }
45
116
  }, 30000);
46
117
 
47
118
  window.onerror = function (msg, url, lineNo, columnNo, error) {
119
+ // console.log({msg, url, lineNo, columnNo, error});
120
+
121
+ if (window.gameLoadingInfo == null) {
122
+ window.gameLoadingInfo = {};
123
+ }
124
+
48
125
  var payload = {
49
126
  type: "onerror event",
50
127
  message: msg,
@@ -58,8 +135,7 @@
58
135
  payload.errorMessage = error.message;
59
136
  payload.errorStack = error.stack;
60
137
  }
61
- // if (msg === "Script error.") {
62
- if (window.gameLoadingInfo) {
138
+ if (window.gameLoadingInfo != null) {
63
139
  payload.loadingState = window.gameLoadingInfo.state;
64
140
  payload.loadingDescription = window.gameLoadingInfo.description;
65
141
  }
@@ -70,13 +146,21 @@
70
146
  payload.androidExists = window.Android != null;
71
147
  payload.androidGameLoadedExists = window.Android != null && window.Android.gameLoaded != null;
72
148
 
73
- // }
74
- window._sendErrorLog(payload);
75
-
76
149
  window.gameLoadingInfo.error = msg;
150
+
77
151
  if (msg === "Script error.") {
78
- window.gameLoadFailed(msg, true);
152
+ // skip sendErrorLog for Script error. (this is error from other scripts, not from oyr game.
153
+ // When a syntax(?) error occurs in a script, loaded from a different origin,
154
+ // the details of the syntax error are not reported to prevent leaking information (see bug 363897).
155
+ // Instead, the error reported is simply "Script error." This behavior can be overriden in some browsers
156
+ // using the crossorigin attribute on and having the server send the appropriate CORS HTTP response headers.
157
+ // A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable
158
+ // in the browser console and not accessible via JavaScript.
159
+ // https://ravikiranj.net/posts/2014/code/how-fix-cryptic-script-error-javascript/
160
+ // https://stackoverflow.com/questions/45844565/script-error-errors-in-window-onerror-in-safari-only
161
+ return false;
79
162
  }
163
+ window._sendErrorLog(payload);
80
164
 
81
165
  return false;
82
166
  };
@@ -13,6 +13,8 @@ declare global {
13
13
  _log: (text: string) => void;
14
14
  _sendErrorLog: (payload: Record<string, any>) => void;
15
15
  gameLoadingInfo: {
16
+ sourceWindow?: Window | null;
17
+ sourceWindowOrigin?: string | null;
16
18
  loadStarted: boolean;
17
19
  loaded: boolean;
18
20
  state: string;
@@ -7,13 +7,19 @@ const env_1 = require("../env");
7
7
  const Source_1 = require("./web/Source");
8
8
  const createNonce_1 = require("../createNonce");
9
9
  const semver = require("semver");
10
- window.gameLoadingInfo = {
10
+ const _gameLoadingInfo = {
11
11
  loadStarted: false,
12
12
  loaded: false,
13
13
  state: "before gameReader API creation",
14
14
  description: "",
15
15
  error: "",
16
16
  };
17
+ if (window.gameLoadingInfo != null) {
18
+ window.gameLoadingInfo = { ...window.gameLoadingInfo, ..._gameLoadingInfo };
19
+ }
20
+ else {
21
+ window.gameLoadingInfo = _gameLoadingInfo;
22
+ }
17
23
  const gameReader = (function () {
18
24
  const self = (window.gameReader || {});
19
25
  self._e = self._e || [];
@@ -224,17 +230,29 @@ const gameLoadedSdkCallbackInternal = (config) => {
224
230
  console.error(e);
225
231
  }
226
232
  };
227
- window.gameLoadingInfo = {
233
+ const _gameLoadingInfoCreated = {
228
234
  loadStarted: false,
229
235
  loaded: false,
230
236
  state: "gameReader API created",
231
237
  description: "",
232
238
  error: "",
233
239
  };
240
+ if (window.gameLoadingInfo != null) {
241
+ window.gameLoadingInfo = { ...window.gameLoadingInfo, ..._gameLoadingInfoCreated };
242
+ }
243
+ else {
244
+ window.gameLoadingInfo = _gameLoadingInfoCreated;
245
+ }
234
246
  const gameLoadFailedSdkCallback = (reason, canTryReload) => {
235
- if (!window.gameLoadingInfo.loaded) {
236
- window.gameLoadingInfo.state = "before call gameLoadFailedSdkCallback";
237
- window.gameLoadingInfo.description = reason;
247
+ let loaded = false;
248
+ if (window.gameLoadingInfo != null && window.gameLoadingInfo.loaded != null) {
249
+ loaded = window.gameLoadingInfo.loaded;
250
+ }
251
+ if (!loaded) {
252
+ if (window.gameLoadingInfo != null) {
253
+ window.gameLoadingInfo.state = "before call gameLoadFailedSdkCallback";
254
+ window.gameLoadingInfo.description = reason;
255
+ }
238
256
  if (env_1.isAndroid) {
239
257
  if (window.Android.gameLoadFailed) {
240
258
  window.Android.gameLoadFailed(reason, canTryReload);
@@ -246,7 +264,7 @@ const gameLoadFailedSdkCallback = (reason, canTryReload) => {
246
264
  }
247
265
  }
248
266
  else if (env_1.isWeb) {
249
- if (Source_1.webSource.sourceWindow && Source_1.webSource.sourceWindowOrigin) {
267
+ if (Source_1.webSource.sourceWindow != null && Source_1.webSource.sourceWindowOrigin != null) {
250
268
  Source_1.webSource.sourceWindow.postMessage(["gameLoadFailed", reason, canTryReload], Source_1.webSource.sourceWindowOrigin);
251
269
  }
252
270
  }
@@ -329,3 +347,6 @@ exports.gameOnForeground = new Promise((resolve, reject) => {
329
347
  gameOnForegroundResolve = resolve;
330
348
  gameOnForegroundReject = reject;
331
349
  });
350
+ exports.gameOnForeground.catch(e => {
351
+ console.log("on gameOnForeground reject", e);
352
+ });
@@ -4,12 +4,18 @@ exports.webSource = void 0;
4
4
  class Source {
5
5
  constructor() { }
6
6
  get sourceWindowOrigin() {
7
+ if (window.gameLoadingInfo != null && window.gameLoadingInfo.sourceWindowOrigin != null) {
8
+ return window.gameLoadingInfo.sourceWindowOrigin;
9
+ }
7
10
  return this._sourceWindowOrigin;
8
11
  }
9
12
  set sourceWindowOrigin(value) {
10
13
  this._sourceWindowOrigin = value;
11
14
  }
12
15
  get sourceWindow() {
16
+ if (window.gameLoadingInfo != null && window.gameLoadingInfo.sourceWindow != null) {
17
+ return window.gameLoadingInfo.sourceWindow;
18
+ }
13
19
  return this._sourceWindow;
14
20
  }
15
21
  set sourceWindow(value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inappstory/game-center-api",
3
- "version": "1.3.7",
3
+ "version": "1.3.9",
4
4
  "description": "",
5
5
  "dependencies": {
6
6
  "semver": "^7.3.8",