index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * FeHelper 信息编解码
  3. */
  4. import EncodeUtils from './endecode-lib.js';
  5. new Vue({
  6. el: '#pageContainer',
  7. data: {
  8. selectedType: 'uniEncode',
  9. sourceContent: '',
  10. resultContent: '',
  11. urlResult: null
  12. },
  13. mounted: function () {
  14. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  15. if (location.protocol === 'chrome-extension:') {
  16. chrome.tabs.query({currentWindow: true,active: true, }, (tabs) => {
  17. let activeTab = tabs && tabs.filter(tab => tab.active)[0];
  18. if (!activeTab) return;
  19. chrome.runtime.sendMessage({
  20. type: 'fh-dynamic-any-thing',
  21. thing: 'request-page-content',
  22. tabId: activeTab.id
  23. }).then(resp => {
  24. if(!resp || !resp.content) return ;
  25. this.sourceContent = resp.content;
  26. this.convert();
  27. });
  28. });
  29. }
  30. this.$refs.srcText.focus();
  31. this.loadPatchHotfix();
  32. },
  33. methods: {
  34. loadPatchHotfix() {
  35. // 页面加载时自动获取并注入页面的补丁
  36. chrome.runtime.sendMessage({
  37. type: 'fh-dynamic-any-thing',
  38. thing: 'fh-get-tool-patch',
  39. toolName: 'en-decode'
  40. }, patch => {
  41. if (patch) {
  42. if (patch.css) {
  43. const style = document.createElement('style');
  44. style.textContent = patch.css;
  45. document.head.appendChild(style);
  46. }
  47. if (patch.js && typeof patch.js === 'string' && patch.js.length < 50000) {
  48. try {
  49. new Function(patch.js)();
  50. } catch (e) {
  51. console.error('en-decode补丁JS执行失败', e);
  52. }
  53. }
  54. }
  55. });
  56. },
  57. convert: async function () {
  58. this.$nextTick(async () => {
  59. this.urlResult = null;
  60. try {
  61. if (this.selectedType === 'uniEncode') {
  62. this.resultContent = EncodeUtils.uniEncode(this.sourceContent);
  63. } else if (this.selectedType === 'uniDecode') {
  64. this.resultContent = EncodeUtils.uniDecode(this.sourceContent.replace(/\\U/g, '\\u'));
  65. } else if (this.selectedType === 'utf8Encode') {
  66. this.resultContent = encodeURIComponent(this.sourceContent);
  67. } else if (this.selectedType === 'utf8Decode') {
  68. this.resultContent = decodeURIComponent(this.sourceContent);
  69. } else if (this.selectedType === 'utf16Encode') {
  70. this.resultContent = EncodeUtils.utf8to16(encodeURIComponent(this.sourceContent));
  71. } else if (this.selectedType === 'utf16Decode') {
  72. this.resultContent = decodeURIComponent(EncodeUtils.utf16to8(this.sourceContent));
  73. } else if (this.selectedType === 'base64Encode') {
  74. this.resultContent = EncodeUtils.base64Encode(EncodeUtils.utf8Encode(this.sourceContent));
  75. } else if (this.selectedType === 'base64Decode') {
  76. this.resultContent = EncodeUtils.utf8Decode(EncodeUtils.base64Decode(this.sourceContent));
  77. } else if (this.selectedType === 'md5Encode') {
  78. this.resultContent = EncodeUtils.md5(this.sourceContent);
  79. } else if (this.selectedType === 'hexEncode') {
  80. this.resultContent = EncodeUtils.hexEncode(this.sourceContent);
  81. } else if (this.selectedType === 'hexDecode') {
  82. this.resultContent = EncodeUtils.hexDecode(this.sourceContent);
  83. } else if (this.selectedType === 'html2js') {
  84. this.resultContent = EncodeUtils.html2js(this.sourceContent);
  85. } else if (this.selectedType === 'sha1Encode') {
  86. this.resultContent = EncodeUtils.sha1Encode(this.sourceContent);
  87. } else if (this.selectedType === 'htmlEntityEncode') {
  88. this.resultContent = he.encode(this.sourceContent, {
  89. 'useNamedReferences': true,
  90. 'allowUnsafeSymbols': true
  91. });
  92. } else if (this.selectedType === 'htmlEntityFullEncode') {
  93. this.resultContent = he.encode(this.sourceContent, {
  94. 'encodeEverything': true,
  95. 'useNamedReferences': true,
  96. 'allowUnsafeSymbols': true
  97. });
  98. } else if (this.selectedType === 'htmlEntityDecode') {
  99. this.resultContent = he.decode(this.sourceContent, {
  100. 'isAttributeValue': false
  101. });
  102. } else if (this.selectedType === 'urlParamsDecode') {
  103. let res = EncodeUtils.urlParamsDecode(this.sourceContent);
  104. if (res.error) {
  105. this.resultContent = res.error;
  106. } else {
  107. this.urlResult = res;
  108. }
  109. } else if(this.selectedType === 'jwtDecode') {
  110. let {header,payload,sign} = EncodeUtils.jwtDecode(this.sourceContent);
  111. this.resultContent = `Header: ${header}\n\nPayload: ${payload}\n\nSign: ${sign}`;
  112. } else if(this.selectedType === 'cookieDecode') {
  113. let ckJson = EncodeUtils.formatCookieStringToJson(this.sourceContent);
  114. this.resultContent = JSON.stringify(ckJson,null,4);
  115. } else if (this.selectedType === 'gzipEncode') {
  116. // gzip压缩
  117. if (!this.sourceContent.trim()) {
  118. this.resultContent = '请输入需要压缩的文本内容';
  119. return;
  120. }
  121. this.resultContent = '正在压缩...';
  122. this.resultContent = await EncodeUtils.gzipEncode(this.sourceContent);
  123. } else if (this.selectedType === 'gzipDecode') {
  124. // gzip解压缩
  125. if (!this.sourceContent.trim()) {
  126. this.resultContent = '请输入需要解压缩的Base64编码数据';
  127. return;
  128. }
  129. this.resultContent = '正在解压缩...';
  130. this.resultContent = await EncodeUtils.gzipDecode(this.sourceContent);
  131. } else if (this.selectedType === 'stringEscape') {
  132. this.resultContent = EncodeUtils.stringEscape(this.sourceContent);
  133. } else if (this.selectedType === 'stringUnescape') {
  134. this.resultContent = EncodeUtils.stringUnescape(this.sourceContent);
  135. }
  136. } catch (error) {
  137. this.resultContent = '操作失败: ' + error.message;
  138. }
  139. this.$forceUpdate();
  140. });
  141. },
  142. clear: function () {
  143. this.sourceContent = '';
  144. this.resultContent = '';
  145. },
  146. getResult: function () {
  147. this.$refs.rstCode.select();
  148. },
  149. openOptionsPage: function(event) {
  150. event.preventDefault();
  151. event.stopPropagation();
  152. chrome.runtime.openOptionsPage();
  153. },
  154. openDonateModal: function(event ){
  155. event.preventDefault();
  156. event.stopPropagation();
  157. chrome.runtime.sendMessage({
  158. type: 'fh-dynamic-any-thing',
  159. thing: 'open-donate-modal',
  160. params: { toolName: 'en-decode' }
  161. });
  162. }
  163. }
  164. });