Browse Source

json自动格式化脚本更新,上一个版本在某些corner Case下会格式化text/html页面

zxlie 8 months ago
parent
commit
08914b4187
4 changed files with 47 additions and 44 deletions
  1. 46 43
      apps/json-format/content-script.js
  2. 1 1
      apps/manifest.json
  3. BIN
      output-edge/fehelper.zip
  4. BIN
      output/fehelper.zip

+ 46 - 43
apps/json-format/content-script.js

@@ -389,37 +389,11 @@ window.JsonAutoFormat = (() => {
         });
     };
 
-
     /**
-     * 从页面提取JSON文本
-     * @returns {string}
-     * @private
+     * 从一个dom节点去获取json内容,这里面有很多的判断
      */
-    let _getJsonText = function () {
-        // 如果是js内容,则不进行json格式化
-        let isJs = /\.js$/.test(new URL(location.href).pathname);
-        isJs = isJs && document.contentType === 'application/javascript';
-        if (isJs) {
-            return false;
-        }
-
-        // 如果是 HTML 页面,也要看一下内容是不是明显就是个JSON,如果不是,则也不进行 json 格式化
-        if (document.contentType === 'text/html') {
-            // 使用 DOMParser 解析 HTML
-            const parser = new DOMParser();
-            const doc = parser.parseFromString(document.body.outerHTML, "text/html");
-            // 移除不需要的标签
-            doc.querySelectorAll('style, script').forEach(el => el.remove());
-            // 获取清理后的文本
-            const cleanText = doc.body.textContent;
-            let jsonObj = _getJsonObject(cleanText);
-            if(!jsonObj) {
-                return false;
-            }
-        }
-
-        let pre = document.querySelectorAll('body>pre')[0] || {textContent: ""};
-        let source = pre.textContent.trim();
+    let _getJsonContentFromDOM = function (dom) {
+        let source = dom.textContent.trim();
 
         if (!source) {
             source = (document.body.textContent || '').trim()
@@ -475,6 +449,39 @@ window.JsonAutoFormat = (() => {
         return (jsonText || '').trim() || source;
     };
 
+    /**
+     * 从页面提取JSON文本
+     * @returns {string}
+     * @private
+     */
+    let _getJsonText = function () {
+        // 如果是js内容,则不进行json格式化
+        let isJs = /\.js$/.test(new URL(location.href).pathname);
+        isJs = isJs && document.contentType === 'application/javascript';
+        if (isJs) {
+            return false;
+        }
+
+        // 如果是 HTML 页面,也要看一下内容是不是明显就是个JSON,如果不是,则也不进行 json 格式化
+        if (document.contentType === 'text/html') {
+            // 使用 DOMParser 解析 HTML
+            const parser = new DOMParser();
+            const doc = parser.parseFromString(document.body.outerHTML, "text/html");
+            // 移除不需要的标签
+            doc.querySelectorAll('style, script').forEach(el => el.remove());
+            // 获取清理后的文本
+            const cleanText = doc.body.textContent;
+            let jsonObj = _getJsonObject(cleanText);
+            if(!jsonObj) {
+                return false;
+            }
+        }
+
+        let pre = document.querySelectorAll('body>pre')[0] || {textContent: ""};
+        
+        return _getJsonContentFromDOM(pre);
+    };
+
     /**
      * 获取一个JSON的所有Key数量
      * @param json
@@ -626,17 +633,13 @@ window.JsonAutoFormat = (() => {
      * 执行format操作
      * @private
      */
-    let _format = function (loadHtmlFragmentFromLocal) {
+    let _format = function () {
 
-        let source = '';
-        if(loadHtmlFragmentFromLocal) {
-            source = _getJsonText();
-            if (!source) {
-                return _format(false);
-            }
+        let source = _getJsonText();
+        if (source) {
             _formatTheSource(source);
         }else{
-            return fetch(location.href)
+            fetch(location.href)
             .then(response => response.text())
             .then(html => {
                 // 使用 DOMParser 解析 HTML
@@ -645,10 +648,10 @@ window.JsonAutoFormat = (() => {
 
                 // 移除不需要的标签
                 doc.querySelectorAll('style, script').forEach(el => el.remove());
-
-                // 获取清理后的文本
-                const cleanText = doc.body.textContent;
-                _formatTheSource(cleanText);
+                const text = _getJsonContentFromDOM(doc.body);
+                if(text){
+                    _formatTheSource(text);
+                }
             })
             .catch();
         }
@@ -657,11 +660,11 @@ window.JsonAutoFormat = (() => {
     return {
         format: () => _getAllOptions(options => {
             if(options.JSON_PAGE_FORMAT) {
-                let intervalId = setInterval(() => {
+                let intervalId = setTimeout(() => {
                     if(typeof Formatter !== 'undefined') {
                         clearInterval(intervalId);
                         _extendsOptions(options);
-                        _format(true);
+                        _format();
                     }
                 },pleaseLetJsLoaded);
             }

+ 1 - 1
apps/manifest.json

@@ -1,7 +1,7 @@
 {
   "name": "FeHelper(前端助手)-Dev",
   "short_name": "FeHelper",
-  "version": "2025.03.2614",
+  "version": "2025.03.2818",
   "manifest_version": 3,
   "description": "JSON自动格式化、手动格式化,支持排序、解码、下载等,更多功能可在配置页按需安装!",
   "icons": {

BIN
output-edge/fehelper.zip


BIN
output/fehelper.zip