178f3cfe2e129dcfa730b23740069d68aa602017
[vchess.git] / client / src / utils / misc.js
1 export const util =
2 {
3 // Source: https://www.quirksmode.org/js/cookies.html
4 setCookie: function(name, value)
5 {
6 var date = new Date();
7 date.setTime(date.getTime()+(183*24*60*60*1000)); //6 months
8 var expires = "; expires="+date.toGMTString();
9 document.cookie = name+"="+value+expires+"; path=/";
10 },
11
12 getCookie: function(name, defaut) {
13 var nameEQ = name + "=";
14 var ca = document.cookie.split(';');
15 for (var i=0;i < ca.length;i++)
16 {
17 var c = ca[i];
18 while (c.charAt(0)==' ')
19 c = c.substring(1,c.length);
20 if (c.indexOf(nameEQ) == 0)
21 return c.substring(nameEQ.length,c.length);
22 }
23 return defaut; //cookie not found
24 },
25
26 // Random (enough) string for socket and game IDs
27 getRandString: function()
28 {
29 return (Date.now().toString(36) + Math.random().toString(36).substr(2, 7))
30 .toUpperCase();
31 },
32
33 // Shortcut for an often used click (on a modal)
34 doClick: function(elemId)
35 {
36 document.getElementById(elemId).click(); //or ".checked = true"
37 },
38
39 translate: function(msg)
40 {
41 return translations[msg];
42 },
43 };