1
0

index.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. if (window.location.protocol === "https:") {
  113. return window.crypto.randomUUID();
  114. } else {
  115. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
  116. .replace(/[xy]/g, function (c) {
  117. const randomValues = new Uint8Array(1);
  118. window.crypto.getRandomValues(randomValues);
  119. let randomValue = randomValues[0] % 16;
  120. let calculatedValue = (c === 'x') ? randomValue : (randomValue & 0x3 | 0x8);
  121. return calculatedValue.toString(16);
  122. });
  123. }
  124. }
  125. static randomShadowsocksPassword(method = SSMethods.BLAKE3_AES_256_GCM) {
  126. let length = 32;
  127. if ([SSMethods.BLAKE3_AES_128_GCM].includes(method)) {
  128. length = 16;
  129. }
  130. const array = new Uint8Array(length);
  131. window.crypto.getRandomValues(array);
  132. return Base64.alternativeEncode(String.fromCharCode(...array));
  133. }
  134. static randomBase32String(length = 16) {
  135. const array = new Uint8Array(length);
  136. window.crypto.getRandomValues(array);
  137. const base32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  138. let result = '';
  139. let bits = 0;
  140. let buffer = 0;
  141. for (let i = 0; i < array.length; i++) {
  142. buffer = (buffer << 8) | array[i];
  143. bits += 8;
  144. while (bits >= 5) {
  145. bits -= 5;
  146. result += base32Chars[(buffer >>> bits) & 0x1F];
  147. }
  148. }
  149. if (bits > 0) {
  150. result += base32Chars[(buffer << (5 - bits)) & 0x1F];
  151. }
  152. return result;
  153. }
  154. }
  155. class ObjectUtil {
  156. static getPropIgnoreCase(obj, prop) {
  157. for (const name in obj) {
  158. if (!obj.hasOwnProperty(name)) {
  159. continue;
  160. }
  161. if (name.toLowerCase() === prop.toLowerCase()) {
  162. return obj[name];
  163. }
  164. }
  165. return undefined;
  166. }
  167. static deepSearch(obj, key) {
  168. if (obj instanceof Array) {
  169. for (let i = 0; i < obj.length; ++i) {
  170. if (this.deepSearch(obj[i], key)) {
  171. return true;
  172. }
  173. }
  174. } else if (obj instanceof Object) {
  175. for (let name in obj) {
  176. if (!obj.hasOwnProperty(name)) {
  177. continue;
  178. }
  179. if (this.deepSearch(obj[name], key)) {
  180. return true;
  181. }
  182. }
  183. } else {
  184. return this.isEmpty(obj) ? false : obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  185. }
  186. return false;
  187. }
  188. static isEmpty(obj) {
  189. return obj === null || obj === undefined || obj === '';
  190. }
  191. static isArrEmpty(arr) {
  192. return !this.isEmpty(arr) && arr.length === 0;
  193. }
  194. static copyArr(dest, src) {
  195. dest.splice(0);
  196. for (const item of src) {
  197. dest.push(item);
  198. }
  199. }
  200. static clone(obj) {
  201. let newObj;
  202. if (obj instanceof Array) {
  203. newObj = [];
  204. this.copyArr(newObj, obj);
  205. } else if (obj instanceof Object) {
  206. newObj = {};
  207. for (const key of Object.keys(obj)) {
  208. newObj[key] = obj[key];
  209. }
  210. } else {
  211. newObj = obj;
  212. }
  213. return newObj;
  214. }
  215. static deepClone(obj) {
  216. let newObj;
  217. if (obj instanceof Array) {
  218. newObj = [];
  219. for (const item of obj) {
  220. newObj.push(this.deepClone(item));
  221. }
  222. } else if (obj instanceof Object) {
  223. newObj = {};
  224. for (const key of Object.keys(obj)) {
  225. newObj[key] = this.deepClone(obj[key]);
  226. }
  227. } else {
  228. newObj = obj;
  229. }
  230. return newObj;
  231. }
  232. static cloneProps(dest, src, ...ignoreProps) {
  233. if (dest == null || src == null) {
  234. return;
  235. }
  236. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  237. for (const key of Object.keys(src)) {
  238. if (!src.hasOwnProperty(key)) {
  239. continue;
  240. } else if (!dest.hasOwnProperty(key)) {
  241. continue;
  242. } else if (src[key] === undefined) {
  243. continue;
  244. }
  245. if (ignoreEmpty) {
  246. dest[key] = src[key];
  247. } else {
  248. let ignore = false;
  249. for (let i = 0; i < ignoreProps.length; ++i) {
  250. if (key === ignoreProps[i]) {
  251. ignore = true;
  252. break;
  253. }
  254. }
  255. if (!ignore) {
  256. dest[key] = src[key];
  257. }
  258. }
  259. }
  260. }
  261. static delProps(obj, ...props) {
  262. for (const prop of props) {
  263. if (prop in obj) {
  264. delete obj[prop];
  265. }
  266. }
  267. }
  268. static execute(func, ...args) {
  269. if (!this.isEmpty(func) && typeof func === 'function') {
  270. func(...args);
  271. }
  272. }
  273. static orDefault(obj, defaultValue) {
  274. if (obj == null) {
  275. return defaultValue;
  276. }
  277. return obj;
  278. }
  279. static equals(a, b) {
  280. for (const key in a) {
  281. if (!a.hasOwnProperty(key)) {
  282. continue;
  283. }
  284. if (!b.hasOwnProperty(key)) {
  285. return false;
  286. } else if (a[key] !== b[key]) {
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. }
  293. class Wireguard {
  294. static gf(init) {
  295. var r = new Float64Array(16);
  296. if (init) {
  297. for (var i = 0; i < init.length; ++i)
  298. r[i] = init[i];
  299. }
  300. return r;
  301. }
  302. static pack(o, n) {
  303. var b, m = this.gf(), t = this.gf();
  304. for (var i = 0; i < 16; ++i)
  305. t[i] = n[i];
  306. this.carry(t);
  307. this.carry(t);
  308. this.carry(t);
  309. for (var j = 0; j < 2; ++j) {
  310. m[0] = t[0] - 0xffed;
  311. for (var i = 1; i < 15; ++i) {
  312. m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
  313. m[i - 1] &= 0xffff;
  314. }
  315. m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
  316. b = (m[15] >> 16) & 1;
  317. m[14] &= 0xffff;
  318. this.cswap(t, m, 1 - b);
  319. }
  320. for (var i = 0; i < 16; ++i) {
  321. o[2 * i] = t[i] & 0xff;
  322. o[2 * i + 1] = t[i] >> 8;
  323. }
  324. }
  325. static carry(o) {
  326. var c;
  327. for (var i = 0; i < 16; ++i) {
  328. o[(i + 1) % 16] += (i < 15 ? 1 : 38) * Math.floor(o[i] / 65536);
  329. o[i] &= 0xffff;
  330. }
  331. }
  332. static cswap(p, q, b) {
  333. var t, c = ~(b - 1);
  334. for (var i = 0; i < 16; ++i) {
  335. t = c & (p[i] ^ q[i]);
  336. p[i] ^= t;
  337. q[i] ^= t;
  338. }
  339. }
  340. static add(o, a, b) {
  341. for (var i = 0; i < 16; ++i)
  342. o[i] = (a[i] + b[i]) | 0;
  343. }
  344. static subtract(o, a, b) {
  345. for (var i = 0; i < 16; ++i)
  346. o[i] = (a[i] - b[i]) | 0;
  347. }
  348. static multmod(o, a, b) {
  349. var t = new Float64Array(31);
  350. for (var i = 0; i < 16; ++i) {
  351. for (var j = 0; j < 16; ++j)
  352. t[i + j] += a[i] * b[j];
  353. }
  354. for (var i = 0; i < 15; ++i)
  355. t[i] += 38 * t[i + 16];
  356. for (var i = 0; i < 16; ++i)
  357. o[i] = t[i];
  358. this.carry(o);
  359. this.carry(o);
  360. }
  361. static invert(o, i) {
  362. var c = this.gf();
  363. for (var a = 0; a < 16; ++a)
  364. c[a] = i[a];
  365. for (var a = 253; a >= 0; --a) {
  366. this.multmod(c, c, c);
  367. if (a !== 2 && a !== 4)
  368. this.multmod(c, c, i);
  369. }
  370. for (var a = 0; a < 16; ++a)
  371. o[a] = c[a];
  372. }
  373. static clamp(z) {
  374. z[31] = (z[31] & 127) | 64;
  375. z[0] &= 248;
  376. }
  377. static generatePublicKey(privateKey) {
  378. var r, z = new Uint8Array(32);
  379. var a = this.gf([1]),
  380. b = this.gf([9]),
  381. c = this.gf(),
  382. d = this.gf([1]),
  383. e = this.gf(),
  384. f = this.gf(),
  385. _121665 = this.gf([0xdb41, 1]),
  386. _9 = this.gf([9]);
  387. for (var i = 0; i < 32; ++i)
  388. z[i] = privateKey[i];
  389. this.clamp(z);
  390. for (var i = 254; i >= 0; --i) {
  391. r = (z[i >>> 3] >>> (i & 7)) & 1;
  392. this.cswap(a, b, r);
  393. this.cswap(c, d, r);
  394. this.add(e, a, c);
  395. this.subtract(a, a, c);
  396. this.add(c, b, d);
  397. this.subtract(b, b, d);
  398. this.multmod(d, e, e);
  399. this.multmod(f, a, a);
  400. this.multmod(a, c, a);
  401. this.multmod(c, b, e);
  402. this.add(e, a, c);
  403. this.subtract(a, a, c);
  404. this.multmod(b, a, a);
  405. this.subtract(c, d, f);
  406. this.multmod(a, c, _121665);
  407. this.add(a, a, d);
  408. this.multmod(c, c, a);
  409. this.multmod(a, d, f);
  410. this.multmod(d, b, _9);
  411. this.multmod(b, e, e);
  412. this.cswap(a, b, r);
  413. this.cswap(c, d, r);
  414. }
  415. this.invert(c, c);
  416. this.multmod(a, a, c);
  417. this.pack(z, a);
  418. return z;
  419. }
  420. static generatePresharedKey() {
  421. var privateKey = new Uint8Array(32);
  422. window.crypto.getRandomValues(privateKey);
  423. return privateKey;
  424. }
  425. static generatePrivateKey() {
  426. var privateKey = this.generatePresharedKey();
  427. this.clamp(privateKey);
  428. return privateKey;
  429. }
  430. static encodeBase64(dest, src) {
  431. 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]);
  432. for (var i = 0; i < 4; ++i)
  433. dest[i] = input[i] + 65 +
  434. (((25 - input[i]) >> 8) & 6) -
  435. (((51 - input[i]) >> 8) & 75) -
  436. (((61 - input[i]) >> 8) & 15) +
  437. (((62 - input[i]) >> 8) & 3);
  438. }
  439. static keyToBase64(key) {
  440. var i, base64 = new Uint8Array(44);
  441. for (i = 0; i < 32 / 3; ++i)
  442. this.encodeBase64(base64.subarray(i * 4), key.subarray(i * 3));
  443. this.encodeBase64(base64.subarray(i * 4), Uint8Array.from([key[i * 3 + 0], key[i * 3 + 1], 0]));
  444. base64[43] = 61;
  445. return String.fromCharCode.apply(null, base64);
  446. }
  447. static keyFromBase64(encoded) {
  448. const binaryStr = atob(encoded);
  449. const bytes = new Uint8Array(binaryStr.length);
  450. for (let i = 0; i < binaryStr.length; i++) {
  451. bytes[i] = binaryStr.charCodeAt(i);
  452. }
  453. return bytes;
  454. }
  455. static generateKeypair(secretKey = '') {
  456. var privateKey = secretKey.length > 0 ? this.keyFromBase64(secretKey) : this.generatePrivateKey();
  457. var publicKey = this.generatePublicKey(privateKey);
  458. return {
  459. publicKey: this.keyToBase64(publicKey),
  460. privateKey: secretKey.length > 0 ? secretKey : this.keyToBase64(privateKey)
  461. };
  462. }
  463. }
  464. class ClipboardManager {
  465. static copyText(content = "") {
  466. // !! here old way of copying is used because not everyone can afford https connection
  467. return new Promise((resolve) => {
  468. try {
  469. const textarea = window.document.createElement('textarea');
  470. textarea.style.fontSize = '12pt';
  471. textarea.style.border = '0';
  472. textarea.style.padding = '0';
  473. textarea.style.margin = '0';
  474. textarea.style.position = 'absolute';
  475. textarea.style.left = '-9999px';
  476. textarea.style.top = `${window.pageYOffset || document.documentElement.scrollTop}px`;
  477. textarea.setAttribute('readonly', '');
  478. textarea.value = content;
  479. window.document.body.appendChild(textarea);
  480. textarea.select();
  481. window.document.execCommand("copy");
  482. window.document.body.removeChild(textarea);
  483. resolve(true)
  484. } catch {
  485. resolve(false)
  486. }
  487. })
  488. }
  489. }
  490. class Base64 {
  491. static encode(content = "", safe = false) {
  492. if (safe) {
  493. return Base64.encode(content)
  494. .replace(/\+/g, '-')
  495. .replace(/=/g, '')
  496. .replace(/\//g, '_')
  497. }
  498. return window.btoa(
  499. String.fromCharCode(...new TextEncoder().encode(content))
  500. )
  501. }
  502. static alternativeEncode(content) {
  503. return window.btoa(
  504. content
  505. )
  506. }
  507. static decode(content = "") {
  508. return new TextDecoder()
  509. .decode(
  510. Uint8Array.from(window.atob(content), c => c.charCodeAt(0))
  511. )
  512. }
  513. }
  514. class SizeFormatter {
  515. static ONE_KB = 1024;
  516. static ONE_MB = this.ONE_KB * 1024;
  517. static ONE_GB = this.ONE_MB * 1024;
  518. static ONE_TB = this.ONE_GB * 1024;
  519. static ONE_PB = this.ONE_TB * 1024;
  520. static sizeFormat(size) {
  521. if (size <= 0) return "0 B";
  522. if (size < this.ONE_KB) return size.toFixed(0) + " B";
  523. if (size < this.ONE_MB) return (size / this.ONE_KB).toFixed(2) + " KB";
  524. if (size < this.ONE_GB) return (size / this.ONE_MB).toFixed(2) + " MB";
  525. if (size < this.ONE_TB) return (size / this.ONE_GB).toFixed(2) + " GB";
  526. if (size < this.ONE_PB) return (size / this.ONE_TB).toFixed(2) + " TB";
  527. return (size / this.ONE_PB).toFixed(2) + " PB";
  528. }
  529. }
  530. class CPUFormatter {
  531. static cpuSpeedFormat(speed) {
  532. return speed > 1000 ? (speed / 1000).toFixed(2) + " GHz" : speed.toFixed(2) + " MHz";
  533. }
  534. static cpuCoreFormat(cores) {
  535. return cores === 1 ? "1 Core" : cores + " Cores";
  536. }
  537. }
  538. class TimeFormatter {
  539. static formatSecond(second) {
  540. if (second < 60) return second.toFixed(0) + 's';
  541. if (second < 3600) return (second / 60).toFixed(0) + 'm';
  542. if (second < 3600 * 24) return (second / 3600).toFixed(0) + 'h';
  543. let day = Math.floor(second / 3600 / 24);
  544. let remain = ((second / 3600) - (day * 24)).toFixed(0);
  545. return day + 'd' + (remain > 0 ? ' ' + remain + 'h' : '');
  546. }
  547. }
  548. class NumberFormatter {
  549. static addZero(num) {
  550. return num < 10 ? "0" + num : num;
  551. }
  552. static toFixed(num, n) {
  553. n = Math.pow(10, n);
  554. return Math.floor(num * n) / n;
  555. }
  556. }
  557. class Utils {
  558. static debounce(fn, delay) {
  559. let timeoutID = null;
  560. return function () {
  561. clearTimeout(timeoutID);
  562. let args = arguments;
  563. let that = this;
  564. timeoutID = setTimeout(() => fn.apply(that, args), delay);
  565. };
  566. }
  567. }
  568. class CookieManager {
  569. static getCookie(cname) {
  570. let name = cname + '=';
  571. let ca = document.cookie.split(';');
  572. for (let c of ca) {
  573. c = c.trim();
  574. if (c.indexOf(name) === 0) {
  575. return decodeURIComponent(c.substring(name.length, c.length));
  576. }
  577. }
  578. return '';
  579. }
  580. static setCookie(cname, cvalue, exdays) {
  581. const d = new Date();
  582. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  583. let expires = 'expires=' + d.toUTCString();
  584. document.cookie = cname + '=' + encodeURIComponent(cvalue) + ';' + expires + ';path=/';
  585. }
  586. }
  587. class ColorUtils {
  588. static usageColor(data, threshold, total) {
  589. switch (true) {
  590. case data === null: return "purple";
  591. case total < 0: return "green";
  592. case total == 0: return "purple";
  593. case data < total - threshold: return "green";
  594. case data < total: return "orange";
  595. default: return "red";
  596. }
  597. }
  598. static clientUsageColor(clientStats, trafficDiff) {
  599. switch (true) {
  600. case !clientStats || clientStats.total == 0: return "#7a316f";
  601. case clientStats.up + clientStats.down < clientStats.total - trafficDiff: return "#008771";
  602. case clientStats.up + clientStats.down < clientStats.total: return "#f37b24";
  603. default: return "#cf3c3c";
  604. }
  605. }
  606. static userExpiryColor(threshold, client, isDark = false) {
  607. if (!client.enable) return isDark ? '#2c3950' : '#bcbcbc';
  608. let now = new Date().getTime(), expiry = client.expiryTime;
  609. switch (true) {
  610. case expiry === null: return "#7a316f";
  611. case expiry < 0: return "#008771";
  612. case expiry == 0: return "#7a316f";
  613. case now < expiry - threshold: return "#008771";
  614. case now < expiry: return "#f37b24";
  615. default: return "#cf3c3c";
  616. }
  617. }
  618. }
  619. class ArrayUtils {
  620. static doAllItemsExist(array1, array2) {
  621. return array1.every(item => array2.includes(item));
  622. }
  623. }
  624. class URLBuilder {
  625. static buildURL({ host, port, isTLS, base, path }) {
  626. if (!host || host.length === 0) host = window.location.hostname;
  627. if (!port || port.length === 0) port = window.location.port;
  628. if (isTLS === undefined) isTLS = window.location.protocol === "https:";
  629. const protocol = isTLS ? "https:" : "http:";
  630. port = String(port);
  631. if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
  632. port = "";
  633. } else {
  634. port = `:${port}`;
  635. }
  636. return `${protocol}//${host}${port}${base}${path}`;
  637. }
  638. }
  639. class LanguageManager {
  640. static supportedLanguages = [
  641. {
  642. name: "العربية",
  643. value: "ar-EG",
  644. icon: "🇪🇬",
  645. },
  646. {
  647. name: "English",
  648. value: "en-US",
  649. icon: "🇺🇸",
  650. },
  651. {
  652. name: "فارسی",
  653. value: "fa-IR",
  654. icon: "🇮🇷",
  655. },
  656. {
  657. name: "简体中文",
  658. value: "zh-CN",
  659. icon: "🇨🇳",
  660. },
  661. {
  662. name: "繁體中文",
  663. value: "zh-TW",
  664. icon: "🇹🇼",
  665. },
  666. {
  667. name: "日本語",
  668. value: "ja-JP",
  669. icon: "🇯🇵",
  670. },
  671. {
  672. name: "Русский",
  673. value: "ru-RU",
  674. icon: "🇷🇺",
  675. },
  676. {
  677. name: "Tiếng Việt",
  678. value: "vi-VN",
  679. icon: "🇻🇳",
  680. },
  681. {
  682. name: "Español",
  683. value: "es-ES",
  684. icon: "🇪🇸",
  685. },
  686. {
  687. name: "Indonesian",
  688. value: "id-ID",
  689. icon: "🇮🇩",
  690. },
  691. {
  692. name: "Український",
  693. value: "uk-UA",
  694. icon: "🇺🇦",
  695. },
  696. {
  697. name: "Türkçe",
  698. value: "tr-TR",
  699. icon: "🇹🇷",
  700. },
  701. {
  702. name: "Português",
  703. value: "pt-BR",
  704. icon: "🇧🇷",
  705. }
  706. ]
  707. static getLanguage() {
  708. let lang = CookieManager.getCookie("lang");
  709. if (!lang) {
  710. if (window.navigator) {
  711. lang = window.navigator.language || window.navigator.userLanguage;
  712. if (LanguageManager.isSupportLanguage(lang)) {
  713. CookieManager.setCookie("lang", lang, 150);
  714. } else {
  715. CookieManager.setCookie("lang", "en-US", 150);
  716. window.location.reload();
  717. }
  718. } else {
  719. CookieManager.setCookie("lang", "en-US", 150);
  720. window.location.reload();
  721. }
  722. }
  723. return lang;
  724. }
  725. static setLanguage(language) {
  726. if (!LanguageManager.isSupportLanguage(language)) {
  727. language = "en-US";
  728. }
  729. CookieManager.setCookie("lang", language, 150);
  730. window.location.reload();
  731. }
  732. static isSupportLanguage(language) {
  733. const languageFilter = LanguageManager.supportedLanguages.filter((lang) => {
  734. return lang.value === language
  735. })
  736. return languageFilter.length > 0;
  737. }
  738. }
  739. const MediaQueryMixin = {
  740. data() {
  741. return {
  742. isMobile: window.innerWidth <= 768,
  743. };
  744. },
  745. methods: {
  746. updateDeviceType() {
  747. this.isMobile = window.innerWidth <= 768;
  748. },
  749. },
  750. mounted() {
  751. window.addEventListener('resize', this.updateDeviceType);
  752. },
  753. beforeDestroy() {
  754. window.removeEventListener('resize', this.updateDeviceType);
  755. },
  756. }
  757. class FileManager {
  758. static downloadTextFile(content, filename = 'file.txt', options = { type: "text/plain" }) {
  759. let link = window.document.createElement('a');
  760. link.download = filename;
  761. link.style.border = '0';
  762. link.style.padding = '0';
  763. link.style.margin = '0';
  764. link.style.position = 'absolute';
  765. link.style.left = '-9999px';
  766. link.style.top = `${window.pageYOffset || window.document.documentElement.scrollTop}px`;
  767. link.href = URL.createObjectURL(new Blob([content], options));
  768. link.click();
  769. URL.revokeObjectURL(link.href);
  770. link.remove();
  771. }
  772. }