fe-jsonformat.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * JSON格式化
  3. */
  4. baidu.jsonformat = (function () {
  5. "use strict";
  6. /**
  7. * 执行format操作
  8. * @private
  9. */
  10. var _format = function () {
  11. $('#errorMsg').html('');
  12. $('#modJsonResult').hide();
  13. var source = $('#jsonSource').val();
  14. if (!source) {
  15. return;
  16. }
  17. // JSONP形式下的callback name
  18. var funcName = null;
  19. // json对象
  20. var jsonObj = null;
  21. // 下面校验给定字符串是否为一个合法的json
  22. try {
  23. // 再看看是不是jsonp的格式
  24. var reg = /^([\w\.]+)\(\s*([\s\S]*)\s*\)$/igm;
  25. var matches = reg.exec(source);
  26. if (matches != null) {
  27. funcName = matches[1];
  28. var newSource = matches[2];
  29. jsonObj = new Function("return " + newSource)();
  30. }
  31. } catch (ex) {
  32. $('#errorMsg').html(ex.message);
  33. return;
  34. }
  35. try {
  36. if (jsonObj == null || typeof jsonObj != 'object') {
  37. jsonObj = new Function("return " + source)();
  38. // 还要防止下面这种情况: "{\"ret\":\"0\", \"msg\":\"ok\"}"
  39. if (typeof jsonObj == "string") {
  40. // 再来一次
  41. jsonObj = new Function("return " + jsonObj)();
  42. }
  43. }
  44. } catch (ex) {
  45. $('#errorMsg').html(ex.message);
  46. return;
  47. }
  48. // 是json格式,可以进行JSON自动格式化
  49. if (jsonObj != null && typeof jsonObj == "object") {
  50. try {
  51. // 要尽量保证格式化的东西一定是一个json,所以需要把内容进行JSON.stringify处理
  52. source = JSON.stringify(jsonObj);
  53. } catch (ex) {
  54. // 通过JSON反解不出来的,一定有问题
  55. return;
  56. }
  57. JsonFormatEntrance.clear();
  58. JsonFormatEntrance.format(source);
  59. $('#modJsonResult').show();
  60. // 如果是JSONP格式的,需要把方法名也显示出来
  61. if (funcName != null) {
  62. $('#jfCallbackName_start').html(funcName + '(');
  63. $('#jfCallbackName_end').html(')');
  64. }
  65. }
  66. };
  67. /**
  68. * 事件绑定
  69. * @private
  70. */
  71. var _bindEvents = function () {
  72. $('#btnFormat').click(function (e) {
  73. _format();
  74. });
  75. // 点击区块高亮
  76. $('#jfContent').delegate('.kvov', 'click', function (e) {
  77. $('#jfContent .kvov').removeClass('x-outline');
  78. $(this).removeClass('x-hover').addClass('x-outline');
  79. if (!$(e.target).is('.kvov .e')) {
  80. e.stopPropagation();
  81. } else {
  82. $(e.target).parent().trigger('click');
  83. }
  84. }).delegate('.kvov', 'mouseover', function (e) {
  85. $(this).addClass('x-hover');
  86. return false;
  87. }).delegate('.kvov', 'mouseout', function (e) {
  88. $(this).removeClass('x-hover');
  89. });
  90. };
  91. var _init = function () {
  92. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  93. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  94. if (request.type == MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event == 'jsonformat') {
  95. if (request.content) {
  96. document.getElementById('jsonSource').value = (request.content);
  97. _format();
  98. }
  99. }
  100. });
  101. $(function () {
  102. //输入框聚焦
  103. jQuery("#jsonSource").focus();
  104. _bindEvents();
  105. });
  106. };
  107. return {
  108. init: _init
  109. };
  110. })();
  111. baidu.jsonformat.init();