Browse Source

json自动格式化bug fix

有unicode编码的时候,自动格式化会有些许问题
Alien 9 years ago
parent
commit
27e67ae4c5

+ 1 - 1
chrome/manifest.json

@@ -1,6 +1,6 @@
 {
     "name": "WEB前端助手(FeHelper)",
-    "version": "7.2",
+    "version": "7.3",
     "manifest_version": 2,
 
     "default_locale": "zh_CN",

+ 1 - 1
chrome/online.manifest.json

@@ -1,6 +1,6 @@
 {
     "name": "WEB前端助手(FeHelper)",
-    "version": "7.2",
+    "version": "7.3",
     "manifest_version": 2,
 
     "default_locale": "zh_CN",

+ 25 - 1
chrome/static/js/jsonformat/contentscript-jsonformat.js

@@ -68,6 +68,29 @@ baidu.csJsonFormat = (function () {
         return newSource || source;
     };
 
+    /**
+     * 此方法用于将Unicode码解码为正常字符串
+     * @param {Object} text
+     */
+    var _uniDecode = function (text) {
+        text = text.replace(/\\/g, "%").replace('%U','%u').replace('%u0025', '%25');
+
+        text = unescape(text.toString().replace(/%2B/g, "+"));
+        var matches = text.match(/(%u00([0-9A-F]{2}))/gi);
+        if (matches) {
+            for (var matchid = 0; matchid < matches.length; matchid++) {
+                var code = matches[matchid].substring(1, 3);
+                var x = Number("0x" + code);
+                if (x >= 128) {
+                    text = text.replace(matches[matchid], code);
+                }
+            }
+        }
+        text = unescape(text.toString().replace(/%2B/g, "+"));
+
+        return text;
+    };
+
     /**
      * 执行format操作
      * @private
@@ -118,7 +141,8 @@ baidu.csJsonFormat = (function () {
                 // 要尽量保证格式化的东西一定是一个json,所以需要把内容进行JSON.stringify处理
                 newSource = JSON.stringify(jsonObj);
                 // 如果newSource的长度比原source长度短很多的话,猜测应该是格式化错了,需要撤销操作
-                if(newSource.length * 2 < source.length) {
+                // 这里一定要unicode decode一下,要不然会出现误判
+                if(newSource.length * 2 < (_uniDecode(source)).length) {
                     return ;
                 }
             } catch (ex) {