format-lib.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. // 这个版本包含基本的JSON格式化功能
  410. let workerCode = `
  411. // 处理主线程消息
  412. self.onmessage = function(event) {
  413. // 处理设置bigint路径的消息
  414. if (event.data.type === 'SET_BIGINT_PATH') {
  415. return; // 我们不需要外部的bigint.js,内部处理即可
  416. }
  417. // 格式化JSON
  418. if (event.data.jsonString) {
  419. // 发送格式化中的消息
  420. self.postMessage(['FORMATTING']);
  421. try {
  422. let jsonObj = JSON.parse(event.data.jsonString);
  423. // 如果是简单主题,直接返回格式化的JSON
  424. if (event.data.skin && event.data.skin === 'theme-simple') {
  425. let formatted = JSON.stringify(jsonObj, null, 4);
  426. let html = '<div id="formattedJson"><pre class="rootItem">' +
  427. formatted.replace(/&/g, '&amp;')
  428. .replace(/</g, '&lt;')
  429. .replace(/>/g, '&gt;')
  430. .replace(/"/g, '&quot;')
  431. .replace(/'/g, '&#039;') +
  432. '</pre></div>';
  433. self.postMessage(['FORMATTED', html]);
  434. return;
  435. }
  436. // 默认主题 - 创建更丰富的HTML结构
  437. let html = '<div id="formattedJson">' +
  438. formatJsonToHtml(jsonObj) +
  439. '</div>';
  440. self.postMessage(['FORMATTED', html]);
  441. } catch (e) {
  442. // 处理错误情况
  443. self.postMessage(['FORMATTED', '<div id="formattedJson"><div class="error">格式化失败: ' + e.message + '</div></div>']);
  444. }
  445. }
  446. };
  447. // HTML特殊字符格式化
  448. function htmlspecialchars(str) {
  449. str = str.replace(/&/g, '&amp;');
  450. str = str.replace(/</g, '&lt;');
  451. str = str.replace(/>/g, '&gt;');
  452. str = str.replace(/"/g, '&quot;');
  453. str = str.replace(/'/g, '&#039;');
  454. return str;
  455. }
  456. // 格式化JSON为HTML
  457. function formatJsonToHtml(json) {
  458. return createNode(json).getHTML();
  459. }
  460. // 创建节点
  461. function createNode(value) {
  462. let node = {
  463. type: getType(value),
  464. value: value,
  465. children: [],
  466. getHTML: function() {
  467. switch(this.type) {
  468. case 'string':
  469. return '<div class="item item-line"><span class="string">"' +
  470. htmlspecialchars(this.value) +
  471. '"</span></div>';
  472. case 'number':
  473. return '<div class="item item-line"><span class="number">' +
  474. this.value +
  475. '</span></div>';
  476. case 'boolean':
  477. return '<div class="item item-line"><span class="bool">' +
  478. this.value +
  479. '</span></div>';
  480. case 'null':
  481. return '<div class="item item-line"><span class="null">null</span></div>';
  482. case 'object':
  483. return this.getObjectHTML();
  484. case 'array':
  485. return this.getArrayHTML();
  486. default:
  487. return '';
  488. }
  489. },
  490. getObjectHTML: function() {
  491. if (!this.value || Object.keys(this.value).length === 0) {
  492. return '<div class="item item-object"><span class="brace">{</span><span class="brace">}</span></div>';
  493. }
  494. let html = '<div class="item item-object">' +
  495. '<span class="expand"></span>' +
  496. '<span class="brace">{</span>' +
  497. '<span class="ellipsis"></span>' +
  498. '<div class="kv-list">';
  499. let keys = Object.keys(this.value);
  500. keys.forEach((key, index) => {
  501. let prop = this.value[key];
  502. let childNode = createNode(prop);
  503. html += '<div class="item">' +
  504. '<span class="quote">"</span>' +
  505. '<span class="key">' + htmlspecialchars(key) + '</span>' +
  506. '<span class="quote">"</span>' +
  507. '<span class="colon">: </span>';
  508. // 添加值
  509. if (childNode.type === 'object' || childNode.type === 'array') {
  510. html += childNode.getHTML();
  511. } else {
  512. html += childNode.getHTML().replace(/^<div class="item item-line">/, '').replace(/<\\/div>$/, '');
  513. }
  514. // 如果不是最后一个属性,添加逗号
  515. if (index < keys.length - 1) {
  516. html += '<span class="comma">,</span>';
  517. }
  518. html += '</div>';
  519. });
  520. html += '</div><span class="brace">}</span></div>';
  521. return html;
  522. },
  523. getArrayHTML: function() {
  524. if (!this.value || this.value.length === 0) {
  525. return '<div class="item item-array"><span class="brace">[</span><span class="brace">]</span></div>';
  526. }
  527. let html = '<div class="item item-array">' +
  528. '<span class="expand"></span>' +
  529. '<span class="brace">[</span>' +
  530. '<span class="ellipsis"></span>' +
  531. '<div class="kv-list">';
  532. this.value.forEach((item, index) => {
  533. let childNode = createNode(item);
  534. html += '<div class="item item-block">';
  535. // 添加值
  536. if (childNode.type === 'object' || childNode.type === 'array') {
  537. html += childNode.getHTML();
  538. } else {
  539. html += childNode.getHTML().replace(/^<div class="item item-line">/, '').replace(/<\\/div>$/, '');
  540. }
  541. // 如果不是最后一个元素,添加逗号
  542. if (index < this.value.length - 1) {
  543. html += '<span class="comma">,</span>';
  544. }
  545. html += '</div>';
  546. });
  547. html += '</div><span class="brace">]</span></div>';
  548. return html;
  549. }
  550. };
  551. return node;
  552. }
  553. // 获取值类型
  554. function getType(value) {
  555. if (value === null) return 'null';
  556. if (value === undefined) return 'undefined';
  557. let type = typeof value;
  558. if (type === 'object') {
  559. if (Array.isArray(value)) return 'array';
  560. }
  561. return type;
  562. }
  563. `;
  564. // 创建Blob URL并实例化Worker
  565. let blob = new Blob([workerCode], {type: 'application/javascript'});
  566. let workerUrl = URL.createObjectURL(blob);
  567. workerInstance = new Worker(workerUrl);
  568. // 添加错误处理
  569. workerInstance.onerror = function(e) {
  570. // 如果Worker出错,清空实例允许下次重试
  571. workerInstance = null;
  572. // 避免URL内存泄漏
  573. URL.revokeObjectURL(workerUrl);
  574. };
  575. return workerInstance;
  576. } catch (e) {
  577. // 出现任何错误,返回null
  578. workerInstance = null;
  579. return null;
  580. }
  581. };
  582. /**
  583. * 执行代码格式化
  584. */
  585. let format = function (jsonStr, skin) {
  586. cachedJsonString = JSON.stringify(JSON.parse(jsonStr), null, 4);
  587. _initElements();
  588. jfPre.html(htmlspecialchars(cachedJsonString));
  589. // 获取Worker实例
  590. let worker = _getWorkerInstance();
  591. if (worker) {
  592. // 设置消息处理程序
  593. worker.onmessage = function (evt) {
  594. let msg = evt.data;
  595. switch (msg[0]) {
  596. case 'FORMATTING':
  597. formattingMsg.show();
  598. break;
  599. case 'FORMATTED':
  600. formattingMsg.hide();
  601. jfContent.html(msg[1]);
  602. _buildOptionBar();
  603. // 事件绑定
  604. _addEvents();
  605. // 支持文件下载
  606. _downloadSupport(cachedJsonString);
  607. break;
  608. }
  609. };
  610. // 添加bigint.js的路径信息
  611. let bigintPath = '';
  612. if (chrome && chrome.runtime && chrome.runtime.getURL) {
  613. bigintPath = chrome.runtime.getURL('json-format/json-bigint.js');
  614. }
  615. // 第一条消息发送bigint.js的路径
  616. worker.postMessage({
  617. type: 'SET_BIGINT_PATH',
  618. path: bigintPath
  619. });
  620. // 发送格式化请求
  621. worker.postMessage({
  622. jsonString: jsonStr,
  623. skin: skin
  624. });
  625. } else {
  626. // Worker创建失败,回退到同步方式
  627. formatSync(jsonStr, skin);
  628. }
  629. };
  630. // 同步的方式格式化
  631. let formatSync = function (jsonStr, skin) {
  632. cachedJsonString = JSON.stringify(JSON.parse(jsonStr), null, 4);
  633. _initElements();
  634. jfPre.html(htmlspecialchars(cachedJsonString));
  635. // 显示格式化进度
  636. formattingMsg.show();
  637. try {
  638. // 回退方案:使用简单模式直接显示格式化的JSON
  639. let formattedJson = JSON.stringify(JSON.parse(jsonStr), null, 4);
  640. jfContent.html(`<div id="formattedJson"><pre class="rootItem">${htmlspecialchars(formattedJson)}</pre></div>`);
  641. // 隐藏进度提示
  642. formattingMsg.hide();
  643. // 构建操作栏
  644. _buildOptionBar();
  645. // 事件绑定
  646. _addEvents();
  647. // 支持文件下载
  648. _downloadSupport(cachedJsonString);
  649. return;
  650. } catch (e) {
  651. jfContent.html(`<div class="error">JSON格式化失败: ${e.message}</div>`);
  652. // 隐藏进度提示
  653. formattingMsg.hide();
  654. }
  655. };
  656. return {
  657. format: format,
  658. formatSync: formatSync
  659. }
  660. })();