fe-jsonformat.js 4.0 KB

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