index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * FeHelper 信息编解码
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. selectedType: 'uniEncode',
  8. sourceContent: '',
  9. resultContent: ''
  10. },
  11. mounted: function () {
  12. let MSG_TYPE = Tarp.require('../static/js/msg_type');
  13. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  14. chrome.runtime.onMessage.addListener((request, sender, callback) => {
  15. if (request.type === MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event === MSG_TYPE.EN_DECODE) {
  16. if (request.content) {
  17. this.sourceContent = request.content;
  18. this.convert();
  19. }
  20. }
  21. });
  22. this.$refs.srcText.focus();
  23. },
  24. methods: {
  25. convert: function () {
  26. this.$nextTick(() => {
  27. let tools = Tarp.require('./endecode-lib');
  28. if (this.selectedType === 'uniEncode') {
  29. this.resultContent = tools.uniEncode(this.sourceContent);
  30. } else if (this.selectedType === 'uniDecode') {
  31. this.resultContent = tools.uniDecode(this.sourceContent.replace(/\\U/g, '\\u'));
  32. } else if (this.selectedType === 'utf8Encode') {
  33. this.resultContent = encodeURIComponent(this.sourceContent);
  34. } else if (this.selectedType === 'utf8Decode') {
  35. this.resultContent = decodeURIComponent(this.sourceContent);
  36. } else if (this.selectedType === 'base64Encode') {
  37. this.resultContent = tools.base64Encode(tools.utf8Encode(this.sourceContent));
  38. } else if (this.selectedType === 'base64Decode') {
  39. this.resultContent = tools.utf8Decode(tools.base64Decode(this.sourceContent));
  40. } else if (this.selectedType === 'md5Encode') {
  41. this.resultContent = tools.md5(this.sourceContent);
  42. } else if (this.selectedType === 'html2js') {
  43. this.resultContent = this.html2js(this.sourceContent);
  44. }
  45. });
  46. },
  47. clear: function() {
  48. this.sourceContent = '';
  49. this.resultContent = '';
  50. },
  51. html2js: function (txt) {
  52. let htmlArr = txt.replace(/\\/g, "\\\\").replace(/\\/g, "\\/").replace(/\'/g, "\\\'").split('\n');
  53. let len = htmlArr.length;
  54. let outArr = [];
  55. outArr.push("var htmlCodes = [\n");
  56. htmlArr.forEach((value, index) => {
  57. if (value !== "") {
  58. if (index === len - 1) {
  59. outArr.push("\'" + value + "\'");
  60. } else {
  61. outArr.push("\'" + value + "\',\n");
  62. }
  63. }
  64. });
  65. outArr.push("\n].join(\"\");");
  66. return outArr.join("");
  67. },
  68. getResult: function () {
  69. this.$refs.rstCode.select();
  70. }
  71. }
  72. });