Skip to content

htmlPrepend

在现有响应内容体前面面插入指定内容(仅对响应类型 content-type 包含 html,且包含响应内容体的状态码(如 200/500 等)有才效)

⚠️ 注意:204、304 等无响应内容体的请求不受影响

规则语法

txt
pattern htmlPrepend://value [filters...]
参数描述详细文档
pattern匹配请求 URL 的表达式匹配模式文档
value文本或二进制内容,支持以下类型:
• 目录/文件路径
• 远程 URL
• 内联/内嵌/Values内容
操作指令文档
filters可选过滤器,支持匹配:
• 请求URL/方法/头部/内容
• 响应状态码/头部
过滤器文档

配置示例

内联方式

txt
www.example.com/path htmlPrepend://(Hello) file://(-test-)
www.example.com/path2 htmlPrepend://(Hello) file://(-test-) resType://js
  • 请求 https://www.example.com/path/to 响应内容变成
    html
    <!DOCTYPE html>
    Hello-test-
  • 请求 https://www.example.com/path2/to 响应内容变成 -test-

内嵌/Values方式

txt
``` body.txt
Hello world.
```
www.example.com/path htmlPrepend://{body.txt} file://(-test-)
www.example.com/path2 htmlPrepend://{body.txt} file://(-test-) resType://css
  • 请求 https://www.example.com/path/to 响应内容变成
    html
    <!DOCTYPE html>
    Hello world.-test-
  • 请求 https://www.example.com/path2/to 响应内容变成 -test-

规避不规范的请求

当接口响应类型(Content-Type)不规范地返回为 text/html 时,可能导致:

  • 前端误将接口数据当作 HTML 解析
  • 注入内容破坏原始数据结构
  • 引发前端解析错误

使用 enable://strictHtml 或 enable://safeHtml 模式保护非 HTML 内容:

txt
www.example.com/path1 htmlPrepend://(test) file://(-test-) enable://strictHtml
www.example.com/path2 htmlPrepend://(test) file://([-test-])  enable://strictHtml
www.example.com/path3 htmlPrepend://(test) file://([-test-])  enable://safeHtml
www.example.com/path4 htmlPrepend://(test) file://(<div>Test</div>) enable://strictHtml
  • 请求 https://www.example.com/path1/to 响应内容变成 -test-
  • 请求 https://www.example.com/path2/to 响应内容变成 [-test-]
  • 请求 https://www.example.com/path3/to 响应内容变成 [-test-]
  • 请求 https://www.example.com/path4/to 响应内容变成
    html
    <!DOCTYPE html>
    test<div>Test</div>

safeHtml/strictHtml 功能参考:enable://safeHtmllineProps://strictHtml

本地/远程资源

txt
www.example.com/path1 htmlPrepend:///User/xxx/test.txt
www.example.com/path2 htmlPrepend://https://www.xxx.com/xxx/params.txt
# 通过编辑临时文件
www.example.com/path3 htmlPrepend://temp/blank.txt
  1. 在响应内容前注入内容(Prepend To Body):resPrepend
  2. 在响应内容前注入 HTML 内容(Prepend HTML To Body,响应类型必须为 text/html):htmlPrepend
  3. 在响应内容前注入 CSS 内容(Prepend CSS To Body,响应类型必须为 text/htmltext/css):cssPrepend
  4. 在响应内容前注入 JS 内容(Prepend JS To Body,响应类型必须为 text/htmltext/cssapplication/javascript):jsPrepend
  5. 替换响应内容(Replace Body):resBody
  6. 用 HTML 内容替换响应内容(Replace Body,响应类型必须为 text/html):htmlBody
  7. 用 CSS 内容替换响应内容(Replace Body,响应类型必须为 text/htmltext/css):cssBody
  8. 用 JS 内容替换响应内容(Replace Body,响应类型必须为 text/htmltext/cssapplication/javascript):jsBody
  9. 在响应内容后追加内容(Append To Body):resAppend
  10. 在响应内容后追加 HTML 内容(Append HTML To Body,响应类型必须为 text/html):htmlAppend
  11. 在响应内容后追加 CSS 内容(Append CSS To Body,响应类型必须为 text/htmltext/css):cssAppend
  12. 在响应内容后追加 JS 内容(Append JS To Body,响应类型必须为 text/htmltext/cssapplication/javascript):jsAppend
  13. 使用关键字或正则表达式替换响应内容(Modify Body Text):resReplace
  14. 覆盖响应内容中的 JSON/Form 对象(Modify Form/JSON):resMerge
  15. 删除响应内容中的 JSON/Form 对象属性(Delete Form/JSON):delete://resBody.xxx
  16. 校验 HTML 内容格式:enable://safeHtmllineProps://strictHtml