index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. class Msg {
  2. constructor(success = false, msg = "", obj = null) {
  3. this.success = success;
  4. this.msg = msg;
  5. this.obj = obj;
  6. }
  7. }
  8. class HttpUtil {
  9. static _handleMsg(msg) {
  10. if (!(msg instanceof Msg) || msg.msg === "") {
  11. return;
  12. }
  13. const messageType = msg.success ? 'success' : 'error';
  14. Vue.prototype.$message[messageType](msg.msg);
  15. }
  16. static _respToMsg(resp) {
  17. if (!resp || !resp.data) {
  18. return new Msg(false, 'No response data');
  19. }
  20. const { data } = resp;
  21. if (data == null) {
  22. return new Msg(true);
  23. }
  24. if (typeof data === 'object' && 'success' in data) {
  25. return new Msg(data.success, data.msg, data.obj);
  26. }
  27. return typeof data === 'object' ? data : new Msg(false, 'unknown data:', data);
  28. }
  29. static async get(url, params, options = {}) {
  30. try {
  31. const resp = await axios.get(url, { params, ...options });
  32. const msg = this._respToMsg(resp);
  33. this._handleMsg(msg);
  34. return msg;
  35. } catch (error) {
  36. console.error('GET request failed:', error);
  37. const errorMsg = new Msg(false, error.response?.data?.message || error.message || 'Request failed');
  38. this._handleMsg(errorMsg);
  39. return errorMsg;
  40. }
  41. }
  42. static async post(url, data, options = {}) {
  43. try {
  44. const resp = await axios.post(url, data, options);
  45. const msg = this._respToMsg(resp);
  46. this._handleMsg(msg);
  47. return msg;
  48. } catch (error) {
  49. console.error('POST request failed:', error);
  50. const errorMsg = new Msg(false, error.response?.data?.message || error.message || 'Request failed');
  51. this._handleMsg(errorMsg);
  52. return errorMsg;
  53. }
  54. }
  55. static async postWithModal(url, data, modal) {
  56. if (modal) {
  57. modal.loading(true);
  58. }
  59. const msg = await this.post(url, data);
  60. if (modal) {
  61. modal.loading(false);
  62. if (msg instanceof Msg && msg.success) {
  63. modal.close();
  64. }
  65. }
  66. return msg;
  67. }
  68. }
  69. class PromiseUtil {
  70. static async sleep(timeout) {
  71. await new Promise(resolve => {
  72. setTimeout(resolve, timeout)
  73. });
  74. }
  75. }
  76. class RandomUtil {
  77. static getSeq({ type = "default", hasNumbers = true, hasLowercase = true, hasUppercase = true } = {}) {
  78. let seq = '';
  79. switch (type) {
  80. case "hex":
  81. seq += "0123456789abcdef";
  82. break;
  83. default:
  84. if (hasNumbers) seq += "0123456789";
  85. if (hasLowercase) seq += "abcdefghijklmnopqrstuvwxyz";
  86. if (hasUppercase) seq += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  87. break;
  88. }
  89. return seq;
  90. }
  91. static randomInteger(min, max) {
  92. const range = max - min + 1;
  93. const randomBuffer = new Uint32Array(1);
  94. window.crypto.getRandomValues(randomBuffer);
  95. return Math.floor((randomBuffer[0] / (0xFFFFFFFF + 1)) * range) + min;
  96. }
  97. static randomSeq(count, options = {}) {
  98. const seq = this.getSeq(options);
  99. const seqLength = seq.length;
  100. const randomValues = new Uint32Array(count);
  101. window.crypto.getRandomValues(randomValues);
  102. return Array.from(randomValues, v => seq[v % seqLength]).join('');
  103. }
  104. static randomShortIds() {
  105. const lengths = [2, 4, 6, 8, 10, 12, 14, 16].sort(() => Math.random() - 0.5);
  106. return lengths.map(len => this.randomSeq(len, { type: "hex" })).join(',');
  107. }
  108. static randomLowerAndNum(len) {
  109. return this.randomSeq(len, { hasUppercase: false });
  110. }
  111. static randomUUID() {
  112. return window.crypto.randomUUID();
  113. }
  114. static randomShadowsocksPassword() {
  115. const array = new Uint8Array(32);
  116. window.crypto.getRandomValues(array);
  117. return Base64.encode(String.fromCharCode(...array));
  118. }
  119. }
  120. class ObjectUtil {
  121. static getPropIgnoreCase(obj, prop) {
  122. for (const name in obj) {
  123. if (!obj.hasOwnProperty(name)) {
  124. continue;
  125. }
  126. if (name.toLowerCase() === prop.toLowerCase()) {
  127. return obj[name];
  128. }
  129. }
  130. return undefined;
  131. }
  132. static deepSearch(obj, key) {
  133. if (obj instanceof Array) {
  134. for (let i = 0; i < obj.length; ++i) {
  135. if (this.deepSearch(obj[i], key)) {
  136. return true;
  137. }
  138. }
  139. } else if (obj instanceof Object) {
  140. for (let name in obj) {
  141. if (!obj.hasOwnProperty(name)) {
  142. continue;
  143. }
  144. if (this.deepSearch(obj[name], key)) {
  145. return true;
  146. }
  147. }
  148. } else {
  149. return this.isEmpty(obj) ? false : obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  150. }
  151. return false;
  152. }
  153. static isEmpty(obj) {
  154. return obj === null || obj === undefined || obj === '';
  155. }
  156. static isArrEmpty(arr) {
  157. return !this.isEmpty(arr) && arr.length === 0;
  158. }
  159. static copyArr(dest, src) {
  160. dest.splice(0);
  161. for (const item of src) {
  162. dest.push(item);
  163. }
  164. }
  165. static clone(obj) {
  166. let newObj;
  167. if (obj instanceof Array) {
  168. newObj = [];
  169. this.copyArr(newObj, obj);
  170. } else if (obj instanceof Object) {
  171. newObj = {};
  172. for (const key of Object.keys(obj)) {
  173. newObj[key] = obj[key];
  174. }
  175. } else {
  176. newObj = obj;
  177. }
  178. return newObj;
  179. }
  180. static deepClone(obj) {
  181. let newObj;
  182. if (obj instanceof Array) {
  183. newObj = [];
  184. for (const item of obj) {
  185. newObj.push(this.deepClone(item));
  186. }
  187. } else if (obj instanceof Object) {
  188. newObj = {};
  189. for (const key of Object.keys(obj)) {
  190. newObj[key] = this.deepClone(obj[key]);
  191. }
  192. } else {
  193. newObj = obj;
  194. }
  195. return newObj;
  196. }
  197. static cloneProps(dest, src, ...ignoreProps) {
  198. if (dest == null || src == null) {
  199. return;
  200. }
  201. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  202. for (const key of Object.keys(src)) {
  203. if (!src.hasOwnProperty(key)) {
  204. continue;
  205. } else if (!dest.hasOwnProperty(key)) {
  206. continue;
  207. } else if (src[key] === undefined) {
  208. continue;
  209. }
  210. if (ignoreEmpty) {
  211. dest[key] = src[key];
  212. } else {
  213. let ignore = false;
  214. for (let i = 0; i < ignoreProps.length; ++i) {
  215. if (key === ignoreProps[i]) {
  216. ignore = true;
  217. break;
  218. }
  219. }
  220. if (!ignore) {
  221. dest[key] = src[key];
  222. }
  223. }
  224. }
  225. }
  226. static delProps(obj, ...props) {
  227. for (const prop of props) {
  228. if (prop in obj) {
  229. delete obj[prop];
  230. }
  231. }
  232. }
  233. static execute(func, ...args) {
  234. if (!this.isEmpty(func) && typeof func === 'function') {
  235. func(...args);
  236. }
  237. }
  238. static orDefault(obj, defaultValue) {
  239. if (obj == null) {
  240. return defaultValue;
  241. }
  242. return obj;
  243. }
  244. static equals(a, b) {
  245. for (const key in a) {
  246. if (!a.hasOwnProperty(key)) {
  247. continue;
  248. }
  249. if (!b.hasOwnProperty(key)) {
  250. return false;
  251. } else if (a[key] !== b[key]) {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. }
  258. class Wireguard {
  259. static gf(init) {
  260. var r = new Float64Array(16);
  261. if (init) {
  262. for (var i = 0; i < init.length; ++i)
  263. r[i] = init[i];
  264. }
  265. return r;
  266. }
  267. static pack(o, n) {
  268. var b, m = this.gf(), t = this.gf();
  269. for (var i = 0; i < 16; ++i)
  270. t[i] = n[i];
  271. this.carry(t);
  272. this.carry(t);
  273. this.carry(t);
  274. for (var j = 0; j < 2; ++j) {
  275. m[0] = t[0] - 0xffed;
  276. for (var i = 1; i < 15; ++i) {
  277. m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
  278. m[i - 1] &= 0xffff;
  279. }
  280. m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
  281. b = (m[15] >> 16) & 1;
  282. m[14] &= 0xffff;
  283. this.cswap(t, m, 1 - b);
  284. }
  285. for (var i = 0; i < 16; ++i) {
  286. o[2 * i] = t[i] & 0xff;
  287. o[2 * i + 1] = t[i] >> 8;
  288. }
  289. }
  290. static carry(o) {
  291. var c;
  292. for (var i = 0; i < 16; ++i) {
  293. o[(i + 1) % 16] += (i < 15 ? 1 : 38) * Math.floor(o[i] / 65536);
  294. o[i] &= 0xffff;
  295. }
  296. }
  297. static cswap(p, q, b) {
  298. var t, c = ~(b - 1);
  299. for (var i = 0; i < 16; ++i) {
  300. t = c & (p[i] ^ q[i]);
  301. p[i] ^= t;
  302. q[i] ^= t;
  303. }
  304. }
  305. static add(o, a, b) {
  306. for (var i = 0; i < 16; ++i)
  307. o[i] = (a[i] + b[i]) | 0;
  308. }
  309. static subtract(o, a, b) {
  310. for (var i = 0; i < 16; ++i)
  311. o[i] = (a[i] - b[i]) | 0;
  312. }
  313. static multmod(o, a, b) {
  314. var t = new Float64Array(31);
  315. for (var i = 0; i < 16; ++i) {
  316. for (var j = 0; j < 16; ++j)
  317. t[i + j] += a[i] * b[j];
  318. }
  319. for (var i = 0; i < 15; ++i)
  320. t[i] += 38 * t[i + 16];
  321. for (var i = 0; i < 16; ++i)
  322. o[i] = t[i];
  323. this.carry(o);
  324. this.carry(o);
  325. }
  326. static invert(o, i) {
  327. var c = this.gf();
  328. for (var a = 0; a < 16; ++a)
  329. c[a] = i[a];
  330. for (var a = 253; a >= 0; --a) {
  331. this.multmod(c, c, c);
  332. if (a !== 2 && a !== 4)
  333. this.multmod(c, c, i);
  334. }
  335. for (var a = 0; a < 16; ++a)
  336. o[a] = c[a];
  337. }
  338. static clamp(z) {
  339. z[31] = (z[31] & 127) | 64;
  340. z[0] &= 248;
  341. }
  342. static generatePublicKey(privateKey) {
  343. var r, z = new Uint8Array(32);
  344. var a = this.gf([1]),
  345. b = this.gf([9]),
  346. c = this.gf(),
  347. d = this.gf([1]),
  348. e = this.gf(),
  349. f = this.gf(),
  350. _121665 = this.gf([0xdb41, 1]),
  351. _9 = this.gf([9]);
  352. for (var i = 0; i < 32; ++i)
  353. z[i] = privateKey[i];
  354. this.clamp(z);
  355. for (var i = 254; i >= 0; --i) {
  356. r = (z[i >>> 3] >>> (i & 7)) & 1;
  357. this.cswap(a, b, r);
  358. this.cswap(c, d, r);
  359. this.add(e, a, c);
  360. this.subtract(a, a, c);
  361. this.add(c, b, d);
  362. this.subtract(b, b, d);
  363. this.multmod(d, e, e);
  364. this.multmod(f, a, a);
  365. this.multmod(a, c, a);
  366. this.multmod(c, b, e);
  367. this.add(e, a, c);
  368. this.subtract(a, a, c);
  369. this.multmod(b, a, a);
  370. this.subtract(c, d, f);
  371. this.multmod(a, c, _121665);
  372. this.add(a, a, d);
  373. this.multmod(c, c, a);
  374. this.multmod(a, d, f);
  375. this.multmod(d, b, _9);
  376. this.multmod(b, e, e);
  377. this.cswap(a, b, r);
  378. this.cswap(c, d, r);
  379. }
  380. this.invert(c, c);
  381. this.multmod(a, a, c);
  382. this.pack(z, a);
  383. return z;
  384. }
  385. static generatePresharedKey() {
  386. var privateKey = new Uint8Array(32);
  387. window.crypto.getRandomValues(privateKey);
  388. return privateKey;
  389. }
  390. static generatePrivateKey() {
  391. var privateKey = this.generatePresharedKey();
  392. this.clamp(privateKey);
  393. return privateKey;
  394. }
  395. static encodeBase64(dest, src) {
  396. var input = Uint8Array.from([(src[0] >> 2) & 63, ((src[0] << 4) | (src[1] >> 4)) & 63, ((src[1] << 2) | (src[2] >> 6)) & 63, src[2] & 63]);
  397. for (var i = 0; i < 4; ++i)
  398. dest[i] = input[i] + 65 +
  399. (((25 - input[i]) >> 8) & 6) -
  400. (((51 - input[i]) >> 8) & 75) -
  401. (((61 - input[i]) >> 8) & 15) +
  402. (((62 - input[i]) >> 8) & 3);
  403. }
  404. static keyToBase64(key) {
  405. var i, base64 = new Uint8Array(44);
  406. for (i = 0; i < 32 / 3; ++i)
  407. this.encodeBase64(base64.subarray(i * 4), key.subarray(i * 3));
  408. this.encodeBase64(base64.subarray(i * 4), Uint8Array.from([key[i * 3 + 0], key[i * 3 + 1], 0]));
  409. base64[43] = 61;
  410. return String.fromCharCode.apply(null, base64);
  411. }
  412. static keyFromBase64(encoded) {
  413. const binaryStr = atob(encoded);
  414. const bytes = new Uint8Array(binaryStr.length);
  415. for (let i = 0; i < binaryStr.length; i++) {
  416. bytes[i] = binaryStr.charCodeAt(i);
  417. }
  418. return bytes;
  419. }
  420. static generateKeypair(secretKey = '') {
  421. var privateKey = secretKey.length > 0 ? this.keyFromBase64(secretKey) : this.generatePrivateKey();
  422. var publicKey = this.generatePublicKey(privateKey);
  423. return {
  424. publicKey: this.keyToBase64(publicKey),
  425. privateKey: secretKey.length > 0 ? secretKey : this.keyToBase64(privateKey)
  426. };
  427. }
  428. }
  429. class ClipboardManager {
  430. static copyText(content = "") {
  431. // !! here old way of copying is used because not everyone can afford https connection
  432. return new Promise((resolve) => {
  433. try {
  434. const textarea = window.document.createElement('textarea');
  435. textarea.style.fontSize = '12pt';
  436. textarea.style.border = '0';
  437. textarea.style.padding = '0';
  438. textarea.style.margin = '0';
  439. textarea.style.position = 'absolute';
  440. textarea.style.left = '-9999px';
  441. textarea.style.top = `${window.pageYOffset || document.documentElement.scrollTop}px`;
  442. textarea.setAttribute('readonly', '');
  443. textarea.value = content;
  444. window.document.body.appendChild(textarea);
  445. textarea.select();
  446. window.document.execCommand("copy");
  447. window.document.body.removeChild(textarea);
  448. resolve(true)
  449. } catch {
  450. resolve(false)
  451. }
  452. })
  453. }
  454. }
  455. class Base64 {
  456. static encode(content = "", safe = false) {
  457. if (safe) {
  458. return Base64.encode(content)
  459. .replace(/\+/g, '-')
  460. .replace(/=/g, '')
  461. .replace(/\//g, '_')
  462. }
  463. return window.btoa(
  464. String.fromCharCode(...new TextEncoder().encode(content))
  465. )
  466. }
  467. static decode(content = "") {
  468. return new TextDecoder()
  469. .decode(
  470. Uint8Array.from(window.atob(content), c => c.charCodeAt(0))
  471. )
  472. }
  473. }
  474. class SizeFormatter {
  475. static ONE_KB = 1024;
  476. static ONE_MB = this.ONE_KB * 1024;
  477. static ONE_GB = this.ONE_MB * 1024;
  478. static ONE_TB = this.ONE_GB * 1024;
  479. static ONE_PB = this.ONE_TB * 1024;
  480. static sizeFormat(size) {
  481. if (size <= 0) return "0 B";
  482. if (size < this.ONE_KB) return size.toFixed(0) + " B";
  483. if (size < this.ONE_MB) return (size / this.ONE_KB).toFixed(2) + " KB";
  484. if (size < this.ONE_GB) return (size / this.ONE_MB).toFixed(2) + " MB";
  485. if (size < this.ONE_TB) return (size / this.ONE_GB).toFixed(2) + " GB";
  486. if (size < this.ONE_PB) return (size / this.ONE_TB).toFixed(2) + " TB";
  487. return (size / this.ONE_PB).toFixed(2) + " PB";
  488. }
  489. }
  490. class CPUFormatter {
  491. static cpuSpeedFormat(speed) {
  492. return speed > 1000 ? (speed / 1000).toFixed(2) + " GHz" : speed.toFixed(2) + " MHz";
  493. }
  494. static cpuCoreFormat(cores) {
  495. return cores === 1 ? "1 Core" : cores + " Cores";
  496. }
  497. }
  498. class TimeFormatter {
  499. static formatSecond(second) {
  500. if (second < 60) return second.toFixed(0) + 's';
  501. if (second < 3600) return (second / 60).toFixed(0) + 'm';
  502. if (second < 3600 * 24) return (second / 3600).toFixed(0) + 'h';
  503. let day = Math.floor(second / 3600 / 24);
  504. let remain = ((second / 3600) - (day * 24)).toFixed(0);
  505. return day + 'd' + (remain > 0 ? ' ' + remain + 'h' : '');
  506. }
  507. }
  508. class NumberFormatter {
  509. static addZero(num) {
  510. return num < 10 ? "0" + num : num;
  511. }
  512. static toFixed(num, n) {
  513. n = Math.pow(10, n);
  514. return Math.floor(num * n) / n;
  515. }
  516. }
  517. class Utils {
  518. static debounce(fn, delay) {
  519. let timeoutID = null;
  520. return function () {
  521. clearTimeout(timeoutID);
  522. let args = arguments;
  523. let that = this;
  524. timeoutID = setTimeout(() => fn.apply(that, args), delay);
  525. };
  526. }
  527. }
  528. class CookieManager {
  529. static getCookie(cname) {
  530. let name = cname + '=';
  531. let ca = document.cookie.split(';');
  532. for (let c of ca) {
  533. c = c.trim();
  534. if (c.indexOf(name) === 0) {
  535. return decodeURIComponent(c.substring(name.length, c.length));
  536. }
  537. }
  538. return '';
  539. }
  540. static setCookie(cname, cvalue, exdays) {
  541. const d = new Date();
  542. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  543. let expires = 'expires=' + d.toUTCString();
  544. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  545. }
  546. }
  547. class ColorUtils {
  548. static usageColor(data, threshold, total) {
  549. switch (true) {
  550. case data === null: return "purple";
  551. case total < 0: return "green";
  552. case total == 0: return "purple";
  553. case data < total - threshold: return "green";
  554. case data < total: return "orange";
  555. default: return "red";
  556. }
  557. }
  558. static clientUsageColor(clientStats, trafficDiff) {
  559. switch (true) {
  560. case !clientStats || clientStats.total == 0: return "#7a316f";
  561. case clientStats.up + clientStats.down < clientStats.total - trafficDiff: return "#008771";
  562. case clientStats.up + clientStats.down < clientStats.total: return "#f37b24";
  563. default: return "#cf3c3c";
  564. }
  565. }
  566. static userExpiryColor(threshold, client, isDark = false) {
  567. if (!client.enable) return isDark ? '#2c3950' : '#bcbcbc';
  568. let now = new Date().getTime(), expiry = client.expiryTime;
  569. switch (true) {
  570. case expiry === null: return "#7a316f";
  571. case expiry < 0: return "#008771";
  572. case expiry == 0: return "#7a316f";
  573. case now < expiry - threshold: return "#008771";
  574. case now < expiry: return "#f37b24";
  575. default: return "#cf3c3c";
  576. }
  577. }
  578. }
  579. class ArrayUtils {
  580. static doAllItemsExist(array1, array2) {
  581. return array1.every(item => array2.includes(item));
  582. }
  583. }
  584. class URLBuilder {
  585. static buildURL({ host, port, isTLS, base, path }) {
  586. if (!host || host.length === 0) host = window.location.hostname;
  587. if (!port || port.length === 0) port = window.location.port;
  588. if (isTLS === undefined) isTLS = window.location.protocol === "https:";
  589. const protocol = isTLS ? "https:" : "http:";
  590. port = String(port);
  591. if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
  592. port = "";
  593. } else {
  594. port = `:${port}`;
  595. }
  596. return `${protocol}//${host}${port}${base}${path}`;
  597. }
  598. }
  599. class LanguageManager {
  600. static supportedLanguages = [
  601. {
  602. name: "English",
  603. value: "en-US",
  604. icon: "🇺🇸",
  605. },
  606. {
  607. name: "فارسی",
  608. value: "fa-IR",
  609. icon: "🇮🇷",
  610. },
  611. {
  612. name: "简体中文",
  613. value: "zh-CN",
  614. icon: "🇨🇳",
  615. },
  616. {
  617. name: "繁體中文",
  618. value: "zh-TW",
  619. icon: "🇹🇼",
  620. },
  621. {
  622. name: "日本語",
  623. value: "ja-JP",
  624. icon: "🇯🇵",
  625. },
  626. {
  627. name: "Русский",
  628. value: "ru-RU",
  629. icon: "🇷🇺",
  630. },
  631. {
  632. name: "Tiếng Việt",
  633. value: "vi-VN",
  634. icon: "🇻🇳",
  635. },
  636. {
  637. name: "Español",
  638. value: "es-ES",
  639. icon: "🇪🇸",
  640. },
  641. {
  642. name: "Indonesian",
  643. value: "id-ID",
  644. icon: "🇮🇩",
  645. },
  646. {
  647. name: "Український",
  648. value: "uk-UA",
  649. icon: "🇺🇦",
  650. },
  651. {
  652. name: "Türkçe",
  653. value: "tr-TR",
  654. icon: "🇹🇷",
  655. },
  656. {
  657. name: "Português",
  658. value: "pt-BR",
  659. icon: "🇧🇷",
  660. }
  661. ]
  662. static getLanguage() {
  663. let lang = CookieManager.getCookie("lang");
  664. if (!lang) {
  665. if (window.navigator) {
  666. lang = window.navigator.language || window.navigator.userLanguage;
  667. if (LanguageManager.isSupportLanguage(lang)) {
  668. CookieManager.setCookie("lang", lang, 150);
  669. } else {
  670. CookieManager.setCookie("lang", "en-US", 150);
  671. window.location.reload();
  672. }
  673. } else {
  674. CookieManager.setCookie("lang", "en-US", 150);
  675. window.location.reload();
  676. }
  677. }
  678. return lang;
  679. }
  680. static setLanguage(language) {
  681. if (!LanguageManager.isSupportLanguage(language)) {
  682. language = "en-US";
  683. }
  684. CookieManager.setCookie("lang", language, 150);
  685. window.location.reload();
  686. }
  687. static isSupportLanguage(language) {
  688. const languageFilter = LanguageManager.supportedLanguages.filter((lang) => {
  689. return lang.value === language
  690. })
  691. return languageFilter.length > 0;
  692. }
  693. }