index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * FeHelper Json Format Tools
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. defaultResultTpl: '<div class="x-placeholder"><img src="./json-demo.jpg" alt="json-placeholder"></div>',
  8. resultContent: '',
  9. jsonSource: '',
  10. errorMsg: '',
  11. jfCallbackName_start: '',
  12. jfCallbackName_end: ''
  13. },
  14. mounted: function () {
  15. this.resultContent = this.defaultResultTpl;
  16. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  17. chrome.runtime.onMessage.addListener((request, sender, callback) => {
  18. let MSG_TYPE = Tarp.require('../static/js/msg_type');
  19. if (request.type === MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event === MSG_TYPE.JSON_FORMAT) {
  20. if (request.content) {
  21. this.jsonSource = request.content || this.defaultResultTpl;
  22. this.format();
  23. }
  24. }
  25. });
  26. //输入框聚焦
  27. this.$refs.jsonBox.focus();
  28. },
  29. methods: {
  30. format: function () {
  31. this.errorMsg = '';
  32. this.resultContent = this.defaultResultTpl;
  33. let source = this.jsonSource.replace(/\n/gm, ' ');
  34. if (!source) {
  35. return;
  36. }
  37. // JSONP形式下的callback name
  38. let funcName = null;
  39. // json对象
  40. let jsonObj = null;
  41. // 下面校验给定字符串是否为一个合法的json
  42. try {
  43. // 再看看是不是jsonp的格式
  44. let reg = /^([\w\.]+)\(\s*([\s\S]*)\s*\)$/igm;
  45. let matches = reg.exec(source);
  46. if (matches != null) {
  47. funcName = matches[1];
  48. let newSource = matches[2];
  49. jsonObj = new Function("return " + newSource)();
  50. }
  51. if (jsonObj == null || typeof jsonObj !== 'object') {
  52. jsonObj = new Function("return " + source)();
  53. // 还要防止下面这种情况: "{\"ret\":\"0\", \"msg\":\"ok\"}"
  54. if (typeof jsonObj === "string") {
  55. // 再来一次
  56. jsonObj = new Function("return " + jsonObj)();
  57. }
  58. }
  59. } catch (ex) {
  60. this.errorMsg = ex.message;
  61. return;
  62. }
  63. // 是json格式,可以进行JSON自动格式化
  64. if (jsonObj != null && typeof jsonObj === "object") {
  65. try {
  66. // 要尽量保证格式化的东西一定是一个json,所以需要把内容进行JSON.stringify处理
  67. source = JSON.stringify(jsonObj);
  68. } catch (ex) {
  69. // 通过JSON反解不出来的,一定有问题
  70. return;
  71. }
  72. // 格式化
  73. Tarp.require('./format-lib').format(source);
  74. // 如果是JSONP格式的,需要把方法名也显示出来
  75. if (funcName != null) {
  76. this.jfCallbackName_start = funcName + '(';
  77. this.jfCallbackName_end = ')';
  78. } else {
  79. this.jfCallbackName_start = '';
  80. this.jfCallbackName_end = '';
  81. }
  82. }
  83. },
  84. setDemo: function () {
  85. let demo = {
  86. date: "20180322",
  87. message: "Success !",
  88. status: 200,
  89. city: "北京",
  90. count: 632,
  91. data: {
  92. shidu: "34%",
  93. pm25: 73,
  94. pm10: 91,
  95. quality: "良",
  96. wendu: "5",
  97. ganmao: "极少数敏感人群应减少户外活动",
  98. yesterday: {
  99. date: "21日星期三",
  100. sunrise: "06:19",
  101. high: "高温 11.0℃",
  102. low: "低温 1.0℃",
  103. sunset: "18:26",
  104. aqi: 85,
  105. fx: "南风",
  106. fl: "<3级",
  107. type: "多云",
  108. notice: "阴晴之间,谨防紫外线侵扰"
  109. },
  110. forecast: [{
  111. date: "22日星期四",
  112. sunrise: "06:17",
  113. high: "高温 17.0℃",
  114. low: "低温 1.0℃",
  115. sunset: "18:27",
  116. aqi: 98,
  117. fx: "西南风",
  118. fl: "<3级",
  119. type: "晴",
  120. notice: "愿你拥有比阳光明媚的心情"
  121. }, {
  122. date: "23日星期五",
  123. sunrise: "06:16",
  124. high: "高温 18.0℃",
  125. low: "低温 5.0℃",
  126. sunset: "18:28",
  127. aqi: 118,
  128. fx: "无持续风向",
  129. fl: "<3级",
  130. type: "多云",
  131. notice: "阴晴之间,谨防紫外线侵扰"
  132. }, {
  133. date: "24日星期六",
  134. sunrise: "06:14",
  135. high: "高温 21.0℃",
  136. low: "低温 7.0℃",
  137. sunset: "18:29",
  138. aqi: 52,
  139. fx: "西南风",
  140. fl: "<3级",
  141. type: "晴",
  142. notice: "愿你拥有比阳光明媚的心情"
  143. }, {
  144. date: "25日星期日",
  145. sunrise: "06:13",
  146. high: "高温 22.0℃",
  147. low: "低温 7.0℃",
  148. sunset: "18:30",
  149. aqi: 71,
  150. fx: "西南风",
  151. fl: "<3级",
  152. type: "晴",
  153. notice: "愿你拥有比阳光明媚的心情"
  154. }, {
  155. date: "26日星期一",
  156. sunrise: "06:11",
  157. high: "高温 21.0℃",
  158. low: "低温 8.0℃",
  159. sunset: "18:31",
  160. aqi: 97,
  161. fx: "西南风",
  162. fl: "<3级",
  163. type: "多云",
  164. notice: "阴晴之间,谨防紫外线侵扰"
  165. }]
  166. }
  167. };
  168. this.jsonSource = JSON.stringify(demo);
  169. this.$nextTick(this.format)
  170. }
  171. }
  172. });