subscription.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. (function () {
  2. // Vue app for Subscription page
  3. const el = document.getElementById('subscription-data');
  4. if (!el) return;
  5. const textarea = document.getElementById('subscription-links');
  6. const rawLinks = (textarea?.value || '').split('\n').filter(Boolean);
  7. const data = {
  8. sId: el.getAttribute('data-sid') || '',
  9. subUrl: el.getAttribute('data-sub-url') || '',
  10. subJsonUrl: el.getAttribute('data-subjson-url') || '',
  11. subClashUrl: el.getAttribute('data-subclash-url') || '',
  12. download: el.getAttribute('data-download') || '',
  13. upload: el.getAttribute('data-upload') || '',
  14. used: el.getAttribute('data-used') || '',
  15. total: el.getAttribute('data-total') || '',
  16. remained: el.getAttribute('data-remained') || '',
  17. expireMs: (parseInt(el.getAttribute('data-expire') || '0', 10) || 0) * 1000,
  18. lastOnlineMs: (parseInt(el.getAttribute('data-lastonline') || '0', 10) || 0),
  19. downloadByte: parseInt(el.getAttribute('data-downloadbyte') || '0', 10) || 0,
  20. uploadByte: parseInt(el.getAttribute('data-uploadbyte') || '0', 10) || 0,
  21. totalByte: parseInt(el.getAttribute('data-totalbyte') || '0', 10) || 0,
  22. datepicker: el.getAttribute('data-datepicker') || 'gregorian',
  23. };
  24. // Normalize lastOnline to milliseconds if it looks like seconds
  25. if (data.lastOnlineMs && data.lastOnlineMs < 10_000_000_000) {
  26. data.lastOnlineMs *= 1000;
  27. }
  28. function renderLink(item) {
  29. return (
  30. Vue.h('a-list-item', {}, [
  31. Vue.h('a-space', { props: { size: 'small' } }, [
  32. Vue.h('a-button', { props: { size: 'small' }, on: { click: () => copy(item) } }, [Vue.h('a-icon', { props: { type: 'copy' } })]),
  33. Vue.h('span', { class: 'break-all' }, item)
  34. ])
  35. ])
  36. );
  37. }
  38. function copy(text) {
  39. ClipboardManager.copyText(text).then(ok => {
  40. const messageType = ok ? 'success' : 'error';
  41. Vue.prototype.$message[messageType](ok ? 'Copied' : 'Copy failed');
  42. });
  43. }
  44. function open(url) {
  45. window.location.href = url;
  46. }
  47. function drawQR(value) {
  48. try {
  49. new QRious({ element: document.getElementById('qrcode'), value, size: 220 });
  50. } catch (e) {
  51. console.warn(e);
  52. }
  53. }
  54. // Try to extract a human label (email/ps) from different link types
  55. function linkName(link, idx) {
  56. try {
  57. if (link.startsWith('vmess://')) {
  58. const json = JSON.parse(atob(link.replace('vmess://', '')));
  59. if (json.ps) return json.ps;
  60. if (json.add && json.id) return json.add; // fallback host
  61. } else if (link.startsWith('vless://') || link.startsWith('trojan://')) {
  62. const hashIdx = link.indexOf('#');
  63. if (hashIdx !== -1) return decodeURIComponent(link.substring(hashIdx + 1));
  64. const qIdx = link.indexOf('?');
  65. if (qIdx !== -1) {
  66. const qs = new URL('http://x/?' + link.substring(qIdx + 1, hashIdx !== -1 ? hashIdx : undefined)).searchParams;
  67. if (qs.get('remark')) return qs.get('remark');
  68. if (qs.get('email')) return qs.get('email');
  69. }
  70. const at = link.indexOf('@');
  71. const protSep = link.indexOf('://');
  72. if (at !== -1 && protSep !== -1) return link.substring(protSep + 3, at);
  73. } else if (link.startsWith('ss://')) {
  74. const hashIdx = link.indexOf('#');
  75. if (hashIdx !== -1) return decodeURIComponent(link.substring(hashIdx + 1));
  76. }
  77. } catch (e) { /* ignore and fallback */ }
  78. return 'Link ' + (idx + 1);
  79. }
  80. const app = new Vue({
  81. delimiters: ['[[', ']]'],
  82. el: '#app',
  83. data: {
  84. themeSwitcher,
  85. app: data,
  86. links: rawLinks,
  87. lang: '',
  88. viewportWidth: (typeof window !== 'undefined' ? window.innerWidth : 1024),
  89. },
  90. async mounted() {
  91. this.lang = LanguageManager.getLanguage();
  92. const tpl = document.getElementById('subscription-data');
  93. const sj = tpl ? tpl.getAttribute('data-subjson-url') : '';
  94. const sc = tpl ? tpl.getAttribute('data-subclash-url') : '';
  95. if (sj) this.app.subJsonUrl = sj;
  96. if (sc) this.app.subClashUrl = sc;
  97. drawQR(this.app.subUrl);
  98. try {
  99. const elJson = document.getElementById('qrcode-subjson');
  100. if (elJson && this.app.subJsonUrl) {
  101. new QRious({ element: elJson, value: this.app.subJsonUrl, size: 220 });
  102. }
  103. const elClash = document.getElementById('qrcode-subclash');
  104. if (elClash && this.app.subClashUrl) {
  105. new QRious({ element: elClash, value: this.app.subClashUrl, size: 220 });
  106. }
  107. } catch (e) { /* ignore */ }
  108. this._onResize = () => { this.viewportWidth = window.innerWidth; };
  109. window.addEventListener('resize', this._onResize);
  110. },
  111. beforeDestroy() {
  112. if (this._onResize) window.removeEventListener('resize', this._onResize);
  113. },
  114. computed: {
  115. isMobile() {
  116. return this.viewportWidth < 576;
  117. },
  118. isUnlimited() {
  119. return !this.app.totalByte;
  120. },
  121. isActive() {
  122. const now = Date.now();
  123. const expiryOk = !this.app.expireMs || this.app.expireMs >= now;
  124. const trafficOk = !this.app.totalByte || (this.app.uploadByte + this.app.downloadByte) <= this.app.totalByte;
  125. return expiryOk && trafficOk;
  126. },
  127. shadowrocketUrl() {
  128. const rawUrl = this.app.subUrl + '?flag=shadowrocket';
  129. const base64Url = btoa(rawUrl);
  130. const remark = encodeURIComponent(this.app.sId || 'Subscription');
  131. return `shadowrocket://add/sub/${base64Url}?remark=${remark}`;
  132. },
  133. v2boxUrl() {
  134. return `v2box://install-sub?url=${encodeURIComponent(this.app.subUrl)}&name=${encodeURIComponent(this.app.sId)}`;
  135. },
  136. streisandUrl() {
  137. return `streisand://import/${encodeURIComponent(this.app.subUrl)}`;
  138. },
  139. v2raytunUrl() {
  140. return this.app.subUrl;
  141. },
  142. npvtunUrl() {
  143. return this.app.subUrl;
  144. },
  145. happUrl() {
  146. return `happ://add/${this.app.subUrl}`;
  147. }
  148. },
  149. methods: {
  150. renderLink,
  151. copy,
  152. open,
  153. linkName,
  154. i18nLabel(key) {
  155. return '{{ i18n "' + key + '" }}';
  156. },
  157. },
  158. });
  159. })();