format-lib.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. * 日期格式化
  3. * @param {Object} pattern
  4. */
  5. Date.prototype.format = function (pattern) {
  6. let pad = function (source, length) {
  7. let pre = "",
  8. negative = (source < 0),
  9. string = String(Math.abs(source));
  10. if (string.length < length) {
  11. pre = (new Array(length - string.length + 1)).join('0');
  12. }
  13. return (negative ? "-" : "") + pre + string;
  14. };
  15. if ('string' !== typeof pattern) {
  16. return this.toString();
  17. }
  18. let replacer = function (patternPart, result) {
  19. pattern = pattern.replace(patternPart, result);
  20. };
  21. let year = this.getFullYear(),
  22. month = this.getMonth() + 1,
  23. date2 = this.getDate(),
  24. hours = this.getHours(),
  25. minutes = this.getMinutes(),
  26. seconds = this.getSeconds(),
  27. milliSec = this.getMilliseconds();
  28. replacer(/yyyy/g, pad(year, 4));
  29. replacer(/yy/g, pad(parseInt(year.toString().slice(2), 10), 2));
  30. replacer(/MM/g, pad(month, 2));
  31. replacer(/M/g, month);
  32. replacer(/dd/g, pad(date2, 2));
  33. replacer(/d/g, date2);
  34. replacer(/HH/g, pad(hours, 2));
  35. replacer(/H/g, hours);
  36. replacer(/hh/g, pad(hours % 12, 2));
  37. replacer(/h/g, hours % 12);
  38. replacer(/mm/g, pad(minutes, 2));
  39. replacer(/m/g, minutes);
  40. replacer(/ss/g, pad(seconds, 2));
  41. replacer(/s/g, seconds);
  42. replacer(/SSS/g, pad(milliSec, 3));
  43. replacer(/S/g, milliSec);
  44. return pattern;
  45. };
  46. /**
  47. * 自动消失的Alert弹窗
  48. * @param content
  49. */
  50. window.toast = function (content) {
  51. window.clearTimeout(window.feHelperAlertMsgTid);
  52. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  53. if (!elAlertMsg) {
  54. let elWrapper = document.createElement('div');
  55. elWrapper.innerHTML = '<div id="fehelper_alertmsg" style="position:fixed;bottom:25px;left:5px;z-index:1000000">' +
  56. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  57. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + content + '</p></div>';
  58. elAlertMsg = elWrapper.childNodes[0];
  59. document.body.appendChild(elAlertMsg);
  60. } else {
  61. elAlertMsg.querySelector('p').innerHTML = content;
  62. elAlertMsg.style.display = 'block';
  63. }
  64. window.feHelperAlertMsgTid = window.setTimeout(function () {
  65. elAlertMsg.style.display = 'none';
  66. }, 1000);
  67. };
  68. /**
  69. * FeHelper Json Format Lib,入口文件
  70. * @example
  71. * Formatter.format(jsonString)
  72. */
  73. window.Formatter = (function () {
  74. "use strict";
  75. let jfContent,
  76. jfPre,
  77. jfStyleEl,
  78. jfStatusBar,
  79. formattingMsg;
  80. let lastItemIdGiven = 0;
  81. let cachedJsonString = '';
  82. // 单例Worker实例
  83. let workerInstance = null;
  84. let _initElements = function () {
  85. jfContent = $('#jfContent');
  86. if (!jfContent[0]) {
  87. jfContent = $('<div id="jfContent" />').appendTo('body');
  88. }
  89. jfPre = $('#jfContent_pre');
  90. if (!jfPre[0]) {
  91. jfPre = $('<pre id="jfContent_pre" />').appendTo('body');
  92. }
  93. jfStyleEl = $('#jfStyleEl');
  94. if (!jfStyleEl[0]) {
  95. jfStyleEl = $('<style id="jfStyleEl" />').appendTo('head');
  96. }
  97. formattingMsg = $('#formattingMsg');
  98. if (!formattingMsg[0]) {
  99. formattingMsg = $('<div id="formattingMsg"><span class="x-loading"></span>格式化中...</div>').appendTo('body');
  100. }
  101. try {
  102. jfContent.html('').show();
  103. jfPre.html('').hide();
  104. jfStatusBar && jfStatusBar.hide();
  105. formattingMsg.hide();
  106. } catch (e) {
  107. }
  108. };
  109. /**
  110. * HTML特殊字符格式化
  111. * @param str
  112. * @returns {*}
  113. */
  114. let htmlspecialchars = function (str) {
  115. str = str.replace(/&/g, '&amp;');
  116. str = str.replace(/</g, '&lt;');
  117. str = str.replace(/>/g, '&gt;');
  118. str = str.replace(/"/g, '&quot;');
  119. str = str.replace(/'/g, '&#039;');
  120. return str;
  121. };
  122. /**
  123. * 直接下载,能解决中文乱码
  124. * @param content
  125. * @private
  126. */
  127. let _downloadSupport = function (content) {
  128. // 下载链接
  129. let dt = (new Date()).format('yyyyMMddHHmmss');
  130. let blob = new Blob([content], {type: 'application/octet-stream'});
  131. let button = $('<button class="xjf-btn xjf-btn-right">下载JSON</button>').appendTo('#optionBar');
  132. if (typeof chrome === 'undefined' || !chrome.permissions) {
  133. button.click(function (e) {
  134. let aLink = $('#aLinkDownload');
  135. if (!aLink[0]) {
  136. aLink = $('<a id="aLinkDownload" target="_blank" title="保存到本地">下载JSON数据</a>').appendTo('body');
  137. aLink.attr('download', 'FeHelper-' + dt + '.json');
  138. aLink.attr('href', URL.createObjectURL(blob));
  139. }
  140. aLink[0].click();
  141. });
  142. } else {
  143. button.click(function (e) {
  144. // 请求权限
  145. chrome.permissions.request({
  146. permissions: ['downloads']
  147. }, (granted) => {
  148. if (granted) {
  149. chrome.downloads.download({
  150. url: URL.createObjectURL(blob),
  151. saveAs: true,
  152. conflictAction: 'overwrite',
  153. filename: 'FeHelper-' + dt + '.json'
  154. });
  155. } else {
  156. toast('必须接受授权,才能正常下载!');
  157. }
  158. });
  159. });
  160. }
  161. };
  162. /**
  163. * chrome 下复制到剪贴板
  164. * @param text
  165. */
  166. let _copyToClipboard = function (text) {
  167. let input = document.createElement('textarea');
  168. input.style.position = 'fixed';
  169. input.style.opacity = 0;
  170. input.value = text;
  171. document.body.appendChild(input);
  172. input.select();
  173. document.execCommand('Copy');
  174. document.body.removeChild(input);
  175. toast('Json片段复制成功,随处粘贴可用!')
  176. };
  177. /**
  178. * 从el中获取json文本
  179. * @param el
  180. * @returns {string}
  181. */
  182. let getJsonText = function (el) {
  183. let txt = el.text().replace(/复制\|下载\|删除/gm,'').replace(/":\s/gm, '":').replace(/,$/, '').trim();
  184. if (!(/^{/.test(txt) && /\}$/.test(txt)) && !(/^\[/.test(txt) && /\]$/.test(txt))) {
  185. txt = '{' + txt + '}';
  186. }
  187. try {
  188. txt = JSON.stringify(JSON.parse(txt), null, 4);
  189. } catch (err) {
  190. }
  191. return txt;
  192. };
  193. // 添加json路径
  194. let _showJsonPath = function (curEl) {
  195. let keys = [];
  196. do {
  197. if (curEl.hasClass('item-block')) {
  198. if (!curEl.hasClass('rootItem')) {
  199. keys.unshift('[' + curEl.prevAll('.item').length + ']');
  200. } else {
  201. break;
  202. }
  203. } else {
  204. keys.unshift(curEl.find('>.key').text());
  205. }
  206. if (curEl.parent().hasClass('rootItem') || curEl.parent().parent().hasClass('rootItem')) {
  207. break;
  208. }
  209. curEl = curEl.parent().parent();
  210. } while (curEl.length && !curEl.hasClass('rootItem'));
  211. let path = keys.join('#@#').replace(/#@#\[/g, '[').replace(/#@#/g, '.');
  212. let jfPath = $('#jsonPath');
  213. if (!jfPath.length) {
  214. jfPath = $('<span id="jsonPath"/>').prependTo(jfStatusBar);
  215. }
  216. jfPath.html('当前节点:JSON.' + path);
  217. };
  218. // 给某个节点增加操作项
  219. let _addOptForItem = function (el, show) {
  220. // 下载json片段
  221. let fnDownload = function (event) {
  222. event.stopPropagation();
  223. let txt = getJsonText(el);
  224. // 下载片段
  225. let dt = (new Date()).format('yyyyMMddHHmmss');
  226. let blob = new Blob([txt], {type: 'application/octet-stream'});
  227. if (typeof chrome === 'undefined' || !chrome.permissions) {
  228. // 下载JSON的简单形式
  229. $(this).attr('download', 'FeHelper-' + dt + '.json').attr('href', URL.createObjectURL(blob));
  230. } else {
  231. // 请求权限
  232. chrome.permissions.request({
  233. permissions: ['downloads']
  234. }, (granted) => {
  235. if (granted) {
  236. chrome.downloads.download({
  237. url: URL.createObjectURL(blob),
  238. saveAs: true,
  239. conflictAction: 'overwrite',
  240. filename: 'FeHelper-' + dt + '.json'
  241. });
  242. } else {
  243. toast('必须接受授权,才能正常下载!');
  244. }
  245. });
  246. }
  247. };
  248. // 复制json片段
  249. let fnCopy = function (event) {
  250. event.stopPropagation();
  251. _copyToClipboard(getJsonText(el));
  252. };
  253. // 删除json片段
  254. let fnDel = function (event) {
  255. event.stopPropagation();
  256. if (el.parent().is('#formattedJson')) {
  257. toast('如果连最外层的Json也删掉的话,就没啥意义了哦!');
  258. return false;
  259. }
  260. toast('节点已删除成功!');
  261. el.remove();
  262. jfStatusBar && jfStatusBar.hide();
  263. };
  264. $('.boxOpt').hide();
  265. if (show) {
  266. let jfOptEl = el.children('.boxOpt');
  267. if (!jfOptEl.length) {
  268. jfOptEl = $('<b class="boxOpt">' +
  269. '<a class="opt-copy" title="复制当前选中节点的JSON数据">复制</a>|' +
  270. '<a class="opt-download" target="_blank" title="下载当前选中节点的JSON数据">下载</a>|' +
  271. '<a class="opt-del" title="删除当前选中节点的JSON数据">删除</a></b>').appendTo(el);
  272. } else {
  273. jfOptEl.show();
  274. }
  275. jfOptEl.find('a.opt-download').unbind('click').bind('click', fnDownload);
  276. jfOptEl.find('a.opt-copy').unbind('click').bind('click', fnCopy);
  277. jfOptEl.find('a.opt-del').unbind('click').bind('click', fnDel);
  278. }
  279. };
  280. // 显示当前节点的Key
  281. let _toogleStatusBar = function (curEl, show) {
  282. if (!jfStatusBar) {
  283. jfStatusBar = $('<div id="statusBar"/>').appendTo('body');
  284. }
  285. if (!show) {
  286. jfStatusBar.hide();
  287. return;
  288. } else {
  289. jfStatusBar.show();
  290. }
  291. _showJsonPath(curEl);
  292. };
  293. /**
  294. * 折叠所有
  295. * @param elements
  296. */
  297. function collapse(elements) {
  298. let el;
  299. $.each(elements, function (i) {
  300. el = $(this);
  301. if (el.children('.kv-list').length) {
  302. el.addClass('collapsed');
  303. if (!el.attr('id')) {
  304. el.attr('id', 'item' + (++lastItemIdGiven));
  305. let count = el.children('.kv-list').eq(0).children().length;
  306. // Generate comment text eg "4 items"
  307. let comment = count + (count === 1 ? ' item' : ' items');
  308. // Add CSS that targets it
  309. jfStyleEl[0].insertAdjacentHTML(
  310. 'beforeend',
  311. '\n#item' + lastItemIdGiven + '.collapsed:after{color: #aaa; content:" // ' + comment + '"}'
  312. );
  313. }
  314. }
  315. });
  316. }
  317. /**
  318. * 创建几个全局操作的按钮,置于页面右上角即可
  319. * @private
  320. */
  321. let _buildOptionBar = function () {
  322. let optionBar = $('#optionBar');
  323. if (optionBar.length) {
  324. optionBar.html('');
  325. } else {
  326. optionBar = $('<span id="optionBar" />').appendTo(jfContent.parent());
  327. }
  328. $('<span class="x-split">|</span>').appendTo(optionBar);
  329. let buttonFormatted = $('<button class="xjf-btn xjf-btn-left">元数据</button>').appendTo(optionBar);
  330. let buttonCollapseAll = $('<button class="xjf-btn xjf-btn-mid">折叠所有</button>').appendTo(optionBar);
  331. let plainOn = false;
  332. buttonFormatted.bind('click', function (e) {
  333. if (plainOn) {
  334. plainOn = false;
  335. jfPre.hide();
  336. jfContent.show();
  337. buttonFormatted.text('元数据');
  338. } else {
  339. plainOn = true;
  340. jfPre.show();
  341. jfContent.hide();
  342. buttonFormatted.text('格式化');
  343. }
  344. jfStatusBar && jfStatusBar.hide();
  345. });
  346. buttonCollapseAll.bind('click', function (e) {
  347. // 如果内容还没有格式化过,需要再格式化一下
  348. if (plainOn) {
  349. buttonFormatted.trigger('click');
  350. }
  351. if (buttonCollapseAll.text() === '折叠所有') {
  352. buttonCollapseAll.text('展开所有');
  353. collapse($('.item-object,.item-block'));
  354. } else {
  355. buttonCollapseAll.text('折叠所有');
  356. $('.item-object,.item-block').removeClass('collapsed');
  357. }
  358. jfStatusBar && jfStatusBar.hide();
  359. });
  360. };
  361. // 附加操作
  362. let _addEvents = function () {
  363. // 折叠、展开
  364. $('#jfContent span.expand').bind('click', function (ev) {
  365. ev.preventDefault();
  366. ev.stopPropagation();
  367. let parentEl = $(this).parent();
  368. parentEl.toggleClass('collapsed');
  369. if (parentEl.hasClass('collapsed')) {
  370. collapse(parentEl);
  371. }
  372. });
  373. // 点击选中:高亮
  374. $('#jfContent .item').bind('click', function (e) {
  375. let el = $(this);
  376. if (el.hasClass('x-selected')) {
  377. _toogleStatusBar(el, false);
  378. _addOptForItem(el, false);
  379. el.removeClass('x-selected');
  380. e.stopPropagation();
  381. return true;
  382. }
  383. $('.x-selected').removeClass('x-selected');
  384. el.addClass('x-selected');
  385. // 显示底部状态栏
  386. _toogleStatusBar(el, true);
  387. _addOptForItem(el, true);
  388. if (!$(e.target).is('.item .expand')) {
  389. e.stopPropagation();
  390. } else {
  391. $(e.target).parent().trigger('click');
  392. }
  393. // 触发钩子
  394. if (typeof window._OnJsonItemClickByFH === 'function') {
  395. window._OnJsonItemClickByFH(getJsonText(el));
  396. }
  397. });
  398. };
  399. /**
  400. * 初始化或获取Worker实例
  401. * 使用单例模式确保只创建一个Worker
  402. */
  403. let _getWorkerInstance = function() {
  404. if (workerInstance) {
  405. return workerInstance;
  406. }
  407. try {
  408. // 统一使用扩展内的脚本文件创建Worker
  409. let workerUrl = chrome.runtime.getURL('json-format/json-worker.js');
  410. workerInstance = new Worker(workerUrl);
  411. return workerInstance;
  412. } catch (e) {
  413. console.error('创建Worker失败:', e);
  414. // 出现任何错误,返回null
  415. workerInstance = null;
  416. return null;
  417. }
  418. };
  419. /**
  420. * 执行代码格式化
  421. */
  422. let format = function (jsonStr, skin) {
  423. cachedJsonString = JSON.stringify(JSON.parse(jsonStr), null, 4);
  424. _initElements();
  425. jfPre.html(htmlspecialchars(cachedJsonString));
  426. try {
  427. // 获取Worker实例
  428. let worker = _getWorkerInstance();
  429. if (worker) {
  430. // 设置消息处理程序
  431. worker.onmessage = function (evt) {
  432. let msg = evt.data;
  433. switch (msg[0]) {
  434. case 'FORMATTING':
  435. formattingMsg.show();
  436. break;
  437. case 'FORMATTED':
  438. formattingMsg.hide();
  439. jfContent.html(msg[1]);
  440. _buildOptionBar();
  441. // 事件绑定
  442. _addEvents();
  443. // 支持文件下载
  444. _downloadSupport(cachedJsonString);
  445. break;
  446. }
  447. };
  448. // 发送格式化请求
  449. worker.postMessage({
  450. jsonString: jsonStr,
  451. skin: skin
  452. });
  453. } else {
  454. // Worker创建失败,回退到同步方式
  455. formatSync(jsonStr, skin);
  456. }
  457. } catch (e) {
  458. console.error('Worker处理失败:', e);
  459. // 出现任何错误,回退到同步方式
  460. formatSync(jsonStr, skin);
  461. }
  462. };
  463. // 同步的方式格式化
  464. let formatSync = function (jsonStr, skin) {
  465. cachedJsonString = JSON.stringify(JSON.parse(jsonStr), null, 4);
  466. _initElements();
  467. jfPre.html(htmlspecialchars(cachedJsonString));
  468. // 显示格式化进度
  469. formattingMsg.show();
  470. try {
  471. // 回退方案:使用简单模式直接显示格式化的JSON
  472. let formattedJson = JSON.stringify(JSON.parse(jsonStr), null, 4);
  473. jfContent.html(`<div id="formattedJson"><pre class="rootItem">${htmlspecialchars(formattedJson)}</pre></div>`);
  474. // 隐藏进度提示
  475. formattingMsg.hide();
  476. // 构建操作栏
  477. _buildOptionBar();
  478. // 事件绑定
  479. _addEvents();
  480. // 支持文件下载
  481. _downloadSupport(cachedJsonString);
  482. return;
  483. } catch (e) {
  484. jfContent.html(`<div class="error">JSON格式化失败: ${e.message}</div>`);
  485. // 隐藏进度提示
  486. formattingMsg.hide();
  487. }
  488. };
  489. return {
  490. format: format,
  491. formatSync: formatSync
  492. }
  493. })();