subscription.js 5.5 KB

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