Inject code snippets in HTML pages
InjectHtmlTagAction
is a built-in action that inject code snippet in the response body of processed request. It's a fully streamed implementation that doesn't need extra buffering regardless of the size of the response body.
This action is effective on :
- chunked transfer encoded bodies
- content encoded bodies in the following list: gzip, deflate and brotli.
Use this action to inject style and script tags to intercepted HTML pages.
InjectHtmlTagAction
detects the <head>
tag and inserts the snippet right after it allowing your code snippet to be executed as soon as possible.
The following sample shows how to force the body style to be red:
// See https://aka.ms/new-console-template for more information
using Fluxzy;
using Fluxzy.Rules.Actions.HighLevelActions;
using Fluxzy.Rules.Filters.RequestFilters;
var fluxzySetting = FluxzySetting.CreateDefault();
fluxzySetting.ConfigureRule()
// When the content type is text/html
.When(new RequestHeaderFilter("text/html", "content-type"))
// Inject the following html tag into the head tag
.Do(new InjectHtmlTagAction()
{
HtmlContent = "<style>body { background-color: red !important; }</style>",
Tag = "head",
});
await using var proxy = new Proxy(fluxzySetting);
Console.WriteLine("Press any key to exit");
Console.ReadKey();
Note
This action has no effect on content security policy (CSP) and other security policies that prevent inline scripts and styles. If the captured HTTP flow have active CSP rule, you must handle it yourself by removing/editing the matching header.