automatic.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Json Page Automatic Format Via FeHelper
  3. * @author zhaoxianlie
  4. */
  5. // json with bigint supported
  6. Tarp.require('../static/vendor/json-bigint/index');
  7. module.exports = (() => {
  8. "use strict";
  9. let _htmlFragment = [
  10. '<style type="text/css">.mod-contentscript #formattingMsg{position:absolute;top:0;font-size:14px;color:#333;margin:5px;}#formattingMsg .x-loading{width:12px;height:12px;border:1px solid #f00;border-radius:50%;box-shadow:0 0 10px 2px;color:#c00;border-right-color:transparent;border-top-color:transparent;animation:spin-right 1s linear infinite normal;animation-delay:0s;margin:0 5px 0 0;display:inline-block}#formattingMsg .x-loading:before{display:block;width:8px;height:8px;margin:1px;border:2px solid #f00;content:" ";border-radius:50%;border-left-color:transparent;border-bottom-color:transparent}@keyframes spin-right{from{transform:rotate(0deg);opacity:.2}50%{transform:rotate(180deg);opacity:1.0}to{transform:rotate(360deg);opacity:.2}}</style>',
  11. '<div class="mod-json mod-contentscript"><div class="rst-item">',
  12. '<div id="formattingMsg"><span class="x-loading"></span>格式化中...</div>',
  13. '<div id="jfCallbackName_start" class="callback-name"></div>',
  14. '<div id="jfContent"></div>',
  15. '<pre id="jfContent_pre"></pre>',
  16. '<div id="jfCallbackName_end" class="callback-name"></div>',
  17. '</div></div>'
  18. ].join('');
  19. let _loadCss = function () {
  20. let cssUrl = chrome.extension.getURL('json-format/without-ui.css');
  21. $('<link id="_fehelper_fcp_css_" href="' + cssUrl + '" rel="stylesheet" type="text/css" />').appendTo('head');
  22. };
  23. /**
  24. * 自动消失的Alert弹窗
  25. * @param content
  26. */
  27. window.alert = function (content) {
  28. window.clearTimeout(window.feHelperAlertMsgTid);
  29. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  30. if (!elAlertMsg) {
  31. let elWrapper = document.createElement('div');
  32. elWrapper.innerHTML = '<div id="fehelper_alertmsg" style="position:fixed;top:5px;right:5px;z-index:1000000">' +
  33. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  34. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + content + '</p></div>';
  35. elAlertMsg = elWrapper.childNodes[0];
  36. document.body.appendChild(elAlertMsg);
  37. } else {
  38. elAlertMsg.querySelector('p').innerHTML = content;
  39. elAlertMsg.style.display = 'block';
  40. }
  41. window.feHelperAlertMsgTid = window.setTimeout(function () {
  42. elAlertMsg.style.display = 'none';
  43. }, 3000);
  44. };
  45. /**
  46. * 从页面提取JSON文本
  47. * @returns {string}
  48. * @private
  49. */
  50. let _getJsonText = function () {
  51. let pre = $('body>pre:eq(0)')[0] || {textContent: ""};
  52. let source = $.trim(pre.textContent);
  53. if (!source) {
  54. source = $.trim(document.body.textContent || '')
  55. }
  56. if (!source) {
  57. return false;
  58. }
  59. // 如果body的内容还包含HTML标签,肯定不是合法的json了
  60. // 如果是合法的json,也只可能有一个text节点
  61. let nodes = document.body.childNodes;
  62. let newSource = '';
  63. for (let i = 0, len = nodes.length; i < len; i++) {
  64. if (nodes[i].nodeType === Node.TEXT_NODE) {
  65. newSource += nodes[i].textContent;
  66. } else if (nodes[i].nodeType === Node.ELEMENT_NODE) {
  67. let tagName = nodes[i].tagName.toLowerCase();
  68. let html = $.trim(nodes[i].textContent);
  69. // 如果是pre标签,则看内容是不是和source一样,一样则continue
  70. if (tagName === 'pre' && html === source) {
  71. } else if ((nodes[i].offsetWidth === 0 || nodes[i].offsetHeight === 0 || !html) && ['script', 'link'].indexOf(tagName) === -1) {
  72. // 如果用户安装迅雷或者其他的插件,也回破坏页面结构,需要兼容一下
  73. } else {
  74. return false;
  75. }
  76. } else {
  77. return false;
  78. }
  79. }
  80. return $.trim(newSource || '') || source;
  81. };
  82. /**
  83. * 此方法用于将Unicode码解码为正常字符串
  84. * @param {Object} text
  85. */
  86. let _uniDecode = function (text) {
  87. try{
  88. text = decodeURIComponent(text);
  89. }catch(e){}
  90. text = text.replace(/(\\)?\\u/gi, "%u").replace('%u0025', '%25');
  91. text = unescape(text.toString().replace(/%2B/g, "+"));
  92. let matches = text.match(/(%u00([0-9A-F]{2}))/gi);
  93. if (matches) {
  94. for (let matchid = 0; matchid < matches.length; matchid++) {
  95. let code = matches[matchid].substring(1, 3);
  96. let x = Number("0x" + code);
  97. if (x >= 128) {
  98. text = text.replace(matches[matchid], code);
  99. }
  100. }
  101. }
  102. text = unescape(text.toString().replace(/%2B/g, "+"));
  103. return text;
  104. };
  105. /**
  106. * 获取一个JSON的所有Key数量
  107. * @param json
  108. * @returns {number}
  109. * @private
  110. */
  111. let _getAllKeysCount = function (json) {
  112. let count = 0;
  113. if (typeof json === 'object') {
  114. let keys = Object.keys(json);
  115. count += keys.length;
  116. keys.forEach(key => {
  117. if (json[key] && typeof json[key] === 'object') {
  118. count += _getAllKeysCount(json[key]);
  119. }
  120. });
  121. }
  122. return count;
  123. };
  124. /**
  125. * 执行format操作
  126. * @private
  127. */
  128. let _format = function (options) {
  129. let source = _getJsonText();
  130. if (!source) {
  131. return;
  132. }
  133. if (options && options['AUTO_TEXT_DECODE']) {
  134. source = _uniDecode(source);
  135. }
  136. // JSONP形式下的callback name
  137. let funcName = null;
  138. let jsonObj = null;
  139. let fnTry = null;
  140. let fnCatch = null;
  141. // 下面校验给定字符串是否为一个合法的json
  142. try {
  143. // 再看看是不是jsonp的格式
  144. let reg = /^([\w\.]+)\(\s*([\s\S]*)\s*\)$/gm;
  145. let reTry = /^(try\s*\{\s*)?/g;
  146. let reCatch = /(\}\s*catch\s*\(\s*\S+\s*\)\s*\{([\s\S])*\})?$/g;
  147. // 检测是否有try-catch包裹
  148. let sourceReplaced = source.replace(reTry, function () {
  149. fnTry = fnTry ? fnTry : arguments[1];
  150. return '';
  151. }).replace(reCatch, function () {
  152. fnCatch = fnCatch ? fnCatch : arguments[1];
  153. return '';
  154. }).trim();
  155. let matches = reg.exec(sourceReplaced);
  156. if (matches != null && (fnTry && fnCatch || !fnTry && !fnCatch)) {
  157. funcName = matches[1];
  158. source = matches[2];
  159. } else {
  160. reg = /^([\{\[])/;
  161. if (!reg.test(source)) {
  162. return;
  163. }
  164. }
  165. // 这里可能会throw exception
  166. jsonObj = JSON.parse(source);
  167. } catch (ex) {
  168. // new Function的方式,能自动给key补全双引号,但是不支持bigint,所以是下下策,放在try-catch里搞
  169. try {
  170. jsonObj = new Function("return " + source)();
  171. } catch (exx) {
  172. try {
  173. // 再给你一次机会,是不是下面这种情况: "{\"ret\":\"0\", \"msg\":\"ok\"}"
  174. jsonObj = new Function("return '" + source + "'")();
  175. if (typeof jsonObj === 'string') {
  176. // 最后给你一次机会,是个字符串,老夫给你再转一次
  177. jsonObj = new Function("return " + jsonObj)();
  178. }
  179. } catch (exxx) {
  180. return;
  181. }
  182. }
  183. }
  184. // 是json格式,可以进行JSON自动格式化
  185. if (jsonObj != null && typeof jsonObj === "object") {
  186. try {
  187. // 要尽量保证格式化的东西一定是一个json,所以需要把内容进行JSON.stringify处理
  188. source = JSON.stringify(jsonObj);
  189. } catch (ex) {
  190. // 通过JSON反解不出来的,一定有问题
  191. return;
  192. }
  193. // JSON的所有key不能超过预设的值,比如 10000 个,要不然自动格式化会比较卡
  194. if (options && options['MAX_JSON_KEYS_NUMBER']) {
  195. let keysCount = _getAllKeysCount(jsonObj);
  196. if (keysCount > options['MAX_JSON_KEYS_NUMBER']) {
  197. let msg = '当前JSON共 <b style="color:red">' + keysCount + '</b> 个Key,大于预设值' + options['MAX_JSON_KEYS_NUMBER'] + ',已取消自动格式化;可到FeHelper设置页调整此配置!';
  198. return alert(msg);
  199. }
  200. }
  201. _loadCss();
  202. $('body').html(_htmlFragment);
  203. // 格式化
  204. Tarp.require('../json-format/format-lib').format(source);
  205. // 如果是JSONP格式的,需要把方法名也显示出来
  206. if (funcName != null) {
  207. if (fnTry && fnCatch) {
  208. $('#jfCallbackName_start').html('<pre style="padding:0">' + fnTry + '</pre>' + funcName + '(');
  209. $('#jfCallbackName_end').html(')<br><pre style="padding:0">' + fnCatch + '</pre>');
  210. } else {
  211. $('#jfCallbackName_start').html(funcName + '(');
  212. $('#jfCallbackName_end').html(')');
  213. }
  214. }
  215. }
  216. };
  217. return {
  218. format: _format
  219. };
  220. })();