()
| 1246 | * @private |
| 1247 | */ |
| 1248 | let _getJsonText = function () { |
| 1249 | if (_isYamlLikeResource()) { |
| 1250 | return false; |
| 1251 | } |
| 1252 | |
| 1253 | // 如果是js内容,则不进行json格式化 |
| 1254 | let isJs = /\.js$/.test(new URL(location.href).pathname); |
| 1255 | isJs = isJs && document.contentType === 'application/javascript'; |
| 1256 | if (isJs) { |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
| 1260 | // 如果是 HTML 页面,也要看一下内容是不是明显就是个JSON,如果不是,则也不进行 json 格式化 |
| 1261 | if (document.contentType === 'text/html' && document.body) { |
| 1262 | // 使用 DOMParser 解析 HTML |
| 1263 | const parser = new DOMParser(); |
| 1264 | const doc = parser.parseFromString(document.body.outerHTML, "text/html"); |
| 1265 | // 移除不需要的标签 |
| 1266 | doc.querySelectorAll('style, script').forEach(el => el.remove()); |
| 1267 | // 获取清理后的文本 |
| 1268 | const cleanText = doc.body.textContent; |
| 1269 | const htmlParseOptions = { |
| 1270 | allowExtractJSONFragment: false, |
| 1271 | allowJSONP: false |
| 1272 | }; |
| 1273 | let jsonObj = _getJsonObject(cleanText, htmlParseOptions); |
| 1274 | if(!jsonObj) { |
| 1275 | return false; |
| 1276 | } |
| 1277 | let pre = document.querySelectorAll('body>pre')[0] || {textContent: ""}; |
| 1278 | return _getJsonContentFromDOM(pre, htmlParseOptions); |
| 1279 | } |
| 1280 | |
| 1281 | let pre = document.querySelectorAll('body>pre')[0] || {textContent: ""}; |
| 1282 | |
| 1283 | return _getJsonContentFromDOM(pre); |
| 1284 | }; |
| 1285 | |
| 1286 | /** |
| 1287 | * 获取一个JSON的所有Key数量 |
no test coverage detected