-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathGame.js
48 lines (44 loc) · 1.51 KB
/
Game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2018-present, Evgeny Nadymov
*
* This source code is licensed under the GPL v.3.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
export function openGameInBrowser(url, message) {
let sharedPreferences = {};
try {
sharedPreferences = JSON.parse(localStorage.getItem('botshare')) || { };
} catch (e) { }
const existing = sharedPreferences['' + message.id];
let hash = existing ? existing : ''
let addHash = 'tgShareScoreUrl=' + encodeURIComponent('tg://share_game_score?hash=');
if (!existing) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const array = new Uint8Array(1);
for (let i = 0; i < 20; i++) {
window.crypto.getRandomValues(array)
hash += chars[array[0] % chars.length];
}
sharedPreferences['' + message.id] = hash;
localStorage.setItem('botshare', JSON.stringify(sharedPreferences));
}
addHash += hash;
const index = url.indexOf('#');
if (index < 0) {
url += '#' + addHash;
} else {
const curHash = url.substring(0, index + 1);
if (curHash.indexOf('=') >= 0 || curHash.indexOf('?') >= 0) {
url += '&' + addHash;
} else {
if (curHash.length > 0) {
url += '?' + addHash;
} else {
url += addHash;
}
}
}
const newWindow = window.open();
newWindow.opener = null;
newWindow.location = url;
}