瀏覽代碼

fix(json-format): 错误的解析 HTML 页面中 script 数据

不格式化 HTML 页面
fixes #285, fixes #355, fixes #360, fixes #325, fixes #311
Aruelius.L 1 年之前
父節點
當前提交
f46c50f0f8
共有 1 個文件被更改,包括 11 次插入8 次删除
  1. 11 8
      apps/json-format/content-script.js

+ 11 - 8
apps/json-format/content-script.js

@@ -397,6 +397,17 @@ window.JsonAutoFormat = (() => {
      * @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 格式化
+        if (document.contentType === 'text/html') {
+            return false;
+        }
 
         let pre = document.querySelectorAll('body>pre')[0] || {textContent: ""};
         let source = pre.textContent.trim();
@@ -405,18 +416,10 @@ window.JsonAutoFormat = (() => {
             source = (document.body.textContent || '').trim()
         }
 
-        // 如果是js内容,则不进行json格式化
-        let isJs = /\.js$/.test(new URL(location.href).pathname);
-        isJs = isJs && document.contentType === 'application/javascript';
-        if (isJs) {
-            source = '';
-        }
-
         if (!source) {
             return false;
         }
 
-
         // 1、如果body的内容还包含HTML标签,肯定不是合法的json了
         // 2、如果是合法的json,也只可能有一个text节点
         // 3、但是要兼容一下其他插件对页面的破坏情况