Skip to content

Instantly share code, notes, and snippets.

@vcsjones
Last active March 1, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vcsjones/ad4a8c195655c6a59b77 to your computer and use it in GitHub Desktop.
Save vcsjones/ad4a8c195655c6a59b77 to your computer and use it in GitHub Desktop.
Manipulating HTML with Fiddler
using Fiddler;
using HtmlAgilityPack;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class LinkAutoTamper : IAutoTamper2
{
public void AutoTamperRequestAfter(Session oSession)
{
}
public void AutoTamperRequestBefore(Session oSession)
{
}
public void AutoTamperResponseAfter(Session oSession)
{
}
public void AutoTamperResponseBefore(Session oSession)
{
if (!oSession.ResponseHeaders.ExistsAndContains("Content-Type", "text/html"))
{
return;
}
var body = oSession.GetResponseBodyAsString();
var encoding = oSession.GetResponseBodyEncoding();
var doc = new HtmlDocument();
doc.LoadHtml(body);
var anchors = doc.DocumentNode?.SelectNodes("//a")?.ToList() ?? new List<HtmlNode>();
var httpAnchors = anchors.Where(node => node.GetAttributeValue("href", "").OICStartsWith("http:"));
foreach(var node in httpAnchors)
{
node.SetAttributeValue("style", node.GetAttributeValue("style", "") + "; border: 1px solid red");
}
using (var ms = new MemoryStream())
{
doc.Save(ms, encoding);
oSession.ResponseBody = ms.ToArray();
}
}
public void OnBeforeReturningError(Session oSession)
{
}
public void OnBeforeUnload()
{
}
public void OnLoad()
{
}
public void OnPeekAtResponseHeaders(Session oSession)
{
if (oSession.ResponseHeaders.ExistsAndContains("Content-Type", "text/html"))
{
oSession.bBufferResponse = true;
}
}
}
open Fiddler
open HtmlAgilityPack
open System.IO
type LinkAutoTamper() =
interface IAutoTamper2 with
member this.AutoTamperRequestAfter(_) = ()
member this.AutoTamperRequestBefore(_) = ()
member this.AutoTamperResponseAfter(_) = ()
member this.AutoTamperResponseBefore(session) =
if not <| session.ResponseHeaders.ExistsAndContains("Content-Type", "text/html") then
()
else
let body = session.GetResponseBodyAsString()
let encoding = session.GetResponseBodyEncoding()
let doc = HtmlDocument()
doc.LoadHtml(body)
let anchors : HtmlNode list =
match doc.DocumentNode with
| null -> []
| docNode -> match docNode.SelectNodes("//a") with | null -> [] | a -> a |> Seq.toList
anchors
|> Seq.filter(fun node -> node.GetAttributeValue("href", "").OICStartsWith("http:"))
|> Seq.iter(fun node ->
node.SetAttributeValue("style", node.GetAttributeValue("style", "") + "; border: 1px solid red")
|> ignore)
use ms = new MemoryStream()
doc.Save(ms, encoding)
session.ResponseBody <- ms.ToArray()
()
member this.OnPeekAtResponseHeaders(session) =
if session.ResponseHeaders.ExistsAndContains("Content-Type", "text/html") then
session.bBufferResponse <- true
member this.OnBeforeReturningError(_) = ()
member this.OnLoad() = ()
member this.OnBeforeUnload() = ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment