`.txt` 纯文本页面被 JSON 自动格式化误接管:全文搜到 6 字符片段 `[1.01]` 就判定整页是 JSON

Open Beginner friendly
#688 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js
Domain
devtools

Research direction

Start with json-format/content-script.js and trace the non-HTML branch into json-format/json-auto-utils.js, especially parseJSONLike. Reproduce with the supplied three-line text and compare parsing with fragment extraction enabled and disabled. Done means plain text containing [1.01] stays readable, while a complete valid JSON document is still recognized.

Written by the indexing model from the issue text.

Description

环境

  • 浏览器:CentBrowser 5.3.1184.31(Chromium 内核)
  • FeHelper:2026.11.25(扩展目录 pkgccpejnmalmdinmhkkfafefagiiiad/2026.11.25_0)
  • 系统:Windows 11
  • 相关设置:JSON 自动格式化 已开启

现象

打开一个 Content-Type: text/plain 的纯文本链接,页面先正常渲染出文字,加载完成后随即被替换成 JSON 视图。而 JSON 视图里的内容只有 6 个字符:

[1.01]

原文 45 万字,被"接管"的只是一个数组。

出问题的页面(526 KB / 16,620 行 / GBK 编码,服务端 text/plain 且未声明 charset):

https://nuduseng.com/%E4%B8%8B%E8%BD%BD%E5%9C%B0%E5%9D%80%E6%B1%87%E6%80%BB_%E8%80%81%E5%85%89%E7%9B%98%E7%BE%A4.txt

最小复现(不依赖上面那个站点)

本地新建 test.txt,内容仅 3 行:

老软件分享
[1.01]轩辕剑3简体中文CD1.rar
解压密码:123456

浏览器打开 file:///.../test.txt → 先显示 3 行文字,随后整页变成 [1.01] 的 JSON 视图。

期望 / 实际

  • 期望:text/plain 页面在整段无法完整解析为 JSON 时,不做任何接管,保持原文可读。
  • 实际:整段解析失败后回落到"片段提取",在全文任意位置命中一个 [...] / {...} 就接管整页。

根因分析

json-format/content-script.js 的两条分支,传参不一致:

// ① text/html 分支:显式关闭片段提取(安全)
return t.getStandaloneHTMLJSONCandidate(
  { directText: n, preTexts: o.map(...), otherElementTexts: r },
  h({ allowExtractJSONFragment: !1, allowJSONP: !1 })
);

// ② 非 html 分支(含 text/plain):未传 options
var a = document.querySelectorAll("body>pre")[0] || { textContent: "" };
return re(a);   // re 内部调用 parseJSONLike(text, undefined)

json-format/json-auto-utils.js 里 parseJSONLike 的默认行为:

!1 === e.allowExtractJSONFragment || /^[\{\[]/.test(a) || ... || (i(_(a)), i(_(o)))

e 为 undefined 时 allowExtractJSONFragment 等价于 true,于是走 _():在全文搜索第一个 { 或 [,截取括号平衡片段作为候选。

本文件的第一个 [ 出现在第 3,865 字符处:

[1.01]轩辕剑3简体中文CD1+CD2.rar

[1.01] 恰好是合法 JSON 数组 → 判定整页为 JSON → 格式化视图替换原文。

两个放大因素:

  1. 无长度/覆盖率下限:6 字符的片段足以接管 452,987 字符的页面。
  2. 无 MIME 白名单:text/plain 与 application/json 被同等对待。

与 #650 属同一类问题

#650(.md 文件内容被当成 JSON 自动解析)已于 2026-09-03 修复,做法是把 .md/.markdown 排除出自动接管范围。.txt / text/plain 仍未覆盖,是同一缺口的剩余部分。

建议修复(按优先级)

  1. 对齐分支参数:非 application/json / text/json 的页面,统一传 { allowExtractJSONFragment: false, allowJSONP: false }。一行改动即可止血。
  2. 增加覆盖率阈值:normalizedSource.length / 全文长度 < 0.5,或 normalizedSource.length < 200 时不接管。
  3. 按 MIME / 扩展名白名单:仅 .json/.jsonp/.geojson 或 JSON MIME 才自动接管;.txt 必须整段严格 JSON.parse 通过才接管。
  4. 提供"恢复原文"入口:误接管后能一键还原,而不是只能刷新页面(大文件刷新成本高)。

离线验证(不依赖浏览器)

把扩展里的 json-auto-utils.js 直接 require 进 Node,用同一份文本跑两种参数:

const u = require('<扩展目录>/json-format/json-auto-utils.js');
const text = fs.readFileSync('test.txt', 'utf8');

u.parseJSONLike(text, { allowExtractJSONFragment: false, allowJSONP: false });
// → null(不判定,符合预期)

u.parseJSONLike(text, undefined);
// → { value: [1.01], normalizedSource: '[1.01]' }(命中,即线上 bug)

对照:纯文本里不含 [ / { 时不判定;整段本身就是合法 JSON 时两条分支都判定。可见问题就是"非 html 分支未传 allowExtractJSONFragment:false"。

Dominant language
JavaScript
Stars
5.7k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zxlie/FeHelper

All issues in zxlie/FeHelper

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.