New Fluxzy v2 just shipped. Electron is out, Tauri is in. gRPC ready, 3x smaller install. Learn more

injectHtmlTagAction Action

Stream a response body and inject text right after a chosen HTML tag, with full support for gzip, deflate, brotli, and chunked transfer encoding.

Rewriting HTML traditionally requires a custom middleware or a content rewriting proxy. injectHtmlTagAction collapses that into one declarative rule that handles streaming, compression, and chunked transfer correctly. It is ideal for adding scripts, banners, or stylesheets to a site without touching its source.

When to use this action

Use injectHtmlTagAction when you want to inject HTML, CSS, or JavaScript into a server response without modifying the upstream code. The action streams the response, decompresses common encodings on the fly, and writes the new content immediately after the first match of the chosen tag.

Typical situations include:

  • Injecting a debugging script into every page of a third party site.
  • Adding a banner that warns testers they are in a non production environment.
  • Loading a CSS override for accessibility testing.
  • Inserting a tracking pixel into a vendor application that you cannot modify directly.

The action runs on the responseHeaderReceivedFromRemote scope. Supported transfer encodings include chunked. Supported content encodings include gzip, deflate, brotli, and lzw. Set restrictToHtml: true to skip non HTML responses automatically.

Real world examples

Inject a debugging script after the opening head tag

Adds a script to every HTML page on the target host without touching the upstream.

rules:
- filter:
    typeKind: HostFilter
    pattern: api.internal.example.com
  actions:
  - typeKind: InjectHtmlTagAction
    tag: head
    htmlContent: '<script src="https://localhost:8080/debug.js"></script>'
    restrictToHtml: true

Add an environment banner to staging traffic

Makes it obvious to testers that they are looking at a non production site.

rules:
- filter:
    typeKind: HostFilter
    pattern: staging.example.com
  actions:
  - typeKind: InjectHtmlTagAction
    tag: body
    htmlContent: '<div style="position:fixed;top:0;left:0;background:orange;padding:4px;z-index:9999;">STAGING</div>'
    restrictToHtml: true

Inject content from a file

Lets you keep larger fragments out of the YAML rules and edit them with your normal HTML tooling.

rules:
- filter:
    typeKind: HtmlResponseFilter
  actions:
  - typeKind: InjectHtmlTagAction
    tag: head
    fromFile: true
    fileName: /etc/fluxzy/injected.html
    restrictToHtml: true

Reference

injectHtmlTagAction

Description

This action stream a response body and inject a text after the first specified html tag.This action can be used to inject a html code snippet after opening <head> tag in any traversing html page.This action supports chunked transfer stream and the following body encodings: gzip, deflate, brotli and lzw.

Evaluation scope

Evaluation scope defines the timing where this filter will be applied.

responseHeaderReceivedFromRemote This scope occurs the moment fluxzy has done parsing the response header.

YAML configuration name

injectHtmlTagAction

Settings

This action has no specific characteristic

Example of usage

The following examples apply this action to any exchanges

Inject a CSS style tag after <head> that sets the document body color to red.

rules:
- filter:
    typeKind: AnyFilter
  actions:
  - typeKind: InjectHtmlTagAction
    tag: head
    htmlContent: '<style>body { background-color: red !important; }</style>'
    restrictToHtml: true

Inject a file after <head>.

rules:
- filter:
    typeKind: AnyFilter
  actions:
  - typeKind: InjectHtmlTagAction
    tag: head
    fromFile: true
    fileName: injected.html
    restrictToHtml: true

.NET reference

View definition of InjectHtmlTagAction for .NET integration.

See also

The following actions are related to this action:

Frequently asked questions

Will it inject into JSON or binary responses?

Not when restrictToHtml is true. The action checks the Content-Type before parsing. Leave the flag enabled unless you really want to attempt injection on any payload.

Does it handle compressed responses?

Yes. gzip, deflate, brotli, and lzw are decompressed in flight, the snippet is inserted, and the response is re emitted to the client with consistent headers.

What happens if the tag does not appear in the page?

The response passes through unchanged. The action only fires on the first occurrence of the tag.

Can I inject before the tag rather than after it?

No. Injection happens immediately after the chosen tag. Use forwardAction or a custom mock if you need finer control over the response shape.

Learn more about Fluxzy rules