index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. },
  31. methods: {
  32. convert: async function () {
  33. this.$nextTick(async () => {
  34. this.urlResult = null;
  35. try {
  36. if (this.selectedType === 'uniEncode') {
  37. this.resultContent = EncodeUtils.uniEncode(this.sourceContent);
  38. } else if (this.selectedType === 'uniDecode') {
  39. this.resultContent = EncodeUtils.uniDecode(this.sourceContent.replace(/\\U/g, '\\u'));
  40. } else if (this.selectedType === 'utf8Encode') {
  41. this.resultContent = encodeURIComponent(this.sourceContent);
  42. } else if (this.selectedType === 'utf8Decode') {
  43. this.resultContent = decodeURIComponent(this.sourceContent);
  44. } else if (this.selectedType === 'utf16Encode') {
  45. this.resultContent = EncodeUtils.utf8to16(encodeURIComponent(this.sourceContent));
  46. } else if (this.selectedType === 'utf16Decode') {
  47. this.resultContent = decodeURIComponent(EncodeUtils.utf16to8(this.sourceContent));
  48. } else if (this.selectedType === 'base64Encode') {
  49. this.resultContent = EncodeUtils.base64Encode(EncodeUtils.utf8Encode(this.sourceContent));
  50. } else if (this.selectedType === 'base64Decode') {
  51. this.resultContent = EncodeUtils.utf8Decode(EncodeUtils.base64Decode(this.sourceContent));
  52. } else if (this.selectedType === 'md5Encode') {
  53. this.resultContent = EncodeUtils.md5(this.sourceContent);
  54. } else if (this.selectedType === 'hexEncode') {
  55. this.resultContent = EncodeUtils.hexEncode(this.sourceContent);
  56. } else if (this.selectedType === 'hexDecode') {
  57. this.resultContent = EncodeUtils.hexDecode(this.sourceContent);
  58. } else if (this.selectedType === 'html2js') {
  59. this.resultContent = EncodeUtils.html2js(this.sourceContent);
  60. } else if (this.selectedType === 'sha1Encode') {
  61. this.resultContent = EncodeUtils.sha1Encode(this.sourceContent);
  62. } else if (this.selectedType === 'htmlEntityEncode') {
  63. this.resultContent = he.encode(this.sourceContent, {
  64. 'useNamedReferences': true,
  65. 'allowUnsafeSymbols': true
  66. });
  67. } else if (this.selectedType === 'htmlEntityFullEncode') {
  68. this.resultContent = he.encode(this.sourceContent, {
  69. 'encodeEverything': true,
  70. 'useNamedReferences': true,
  71. 'allowUnsafeSymbols': true
  72. });
  73. } else if (this.selectedType === 'htmlEntityDecode') {
  74. this.resultContent = he.decode(this.sourceContent, {
  75. 'isAttributeValue': false
  76. });
  77. } else if (this.selectedType === 'urlParamsDecode') {
  78. let res = EncodeUtils.urlParamsDecode(this.sourceContent);
  79. if (res.error) {
  80. this.resultContent = res.error;
  81. } else {
  82. this.urlResult = res;
  83. }
  84. } else if(this.selectedType === 'jwtDecode') {
  85. let {header,payload,sign} = EncodeUtils.jwtDecode(this.sourceContent);
  86. this.resultContent = `Header: ${header}\n\nPayload: ${payload}\n\nSign: ${sign}`;
  87. } else if(this.selectedType === 'cookieDecode') {
  88. let ckJson = EncodeUtils.formatCookieStringToJson(this.sourceContent);
  89. this.resultContent = JSON.stringify(ckJson,null,4);
  90. } else if (this.selectedType === 'gzipEncode') {
  91. // gzip压缩
  92. if (!this.sourceContent.trim()) {
  93. this.resultContent = '请输入需要压缩的文本内容';
  94. return;
  95. }
  96. this.resultContent = '正在压缩...';
  97. this.resultContent = await EncodeUtils.gzipEncode(this.sourceContent);
  98. } else if (this.selectedType === 'gzipDecode') {
  99. // gzip解压缩
  100. if (!this.sourceContent.trim()) {
  101. this.resultContent = '请输入需要解压缩的Base64编码数据';
  102. return;
  103. }
  104. this.resultContent = '正在解压缩...';
  105. this.resultContent = await EncodeUtils.gzipDecode(this.sourceContent);
  106. }
  107. } catch (error) {
  108. this.resultContent = '操作失败: ' + error.message;
  109. }
  110. this.$forceUpdate();
  111. });
  112. },
  113. clear: function () {
  114. this.sourceContent = '';
  115. this.resultContent = '';
  116. },
  117. getResult: function () {
  118. this.$refs.rstCode.select();
  119. },
  120. openOptionsPage: function(event) {
  121. event.preventDefault();
  122. event.stopPropagation();
  123. chrome.runtime.openOptionsPage();
  124. },
  125. openDonateModal: function(event ){
  126. event.preventDefault();
  127. event.stopPropagation();
  128. chrome.runtime.sendMessage({
  129. type: 'fh-dynamic-any-thing',
  130. thing: 'open-donate-modal',
  131. params: { toolName: 'en-decode' }
  132. });
  133. }
  134. }
  135. });