index.js 7.9 KB

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