You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

55 lines
1.9 KiB

using System.IO;
using AngleSharp.Html;
using Microsoft.AspNetCore.Http;
4 years ago
using System.Linq;
using AngleSharp.Html.Dom;
namespace Ksp.WebServer
{
public class KspPageRewriter
{
public string RewriteHtml(string source, HttpContext context)
{
var p = new AngleSharp.Html.Parser.HtmlParser();
var document = p.ParseDocument(source);
4 years ago
ModifyTree(document, context.Request.Path.Value.Trim('/'));
var outputHtml = new StringWriter();
document.ToHtml(outputHtml, new PrettyMarkupFormatter() { Indentation = "\t", NewLine = "\n" });
return outputHtml.ToString();
}
public void ModifyTree(IHtmlDocument document, string path)
{
foreach (var form in document.QuerySelectorAll("form"))
{
if (form.QuerySelector("input[type=password]") is null)
continue;
var warning = document.CreateElement("div");
warning.SetAttribute("style", "color: red; font-size: 3em; font-weight: bold");
warning.TextContent = "Web běží na magické proxy, které byste měli věřit!!!";
form.Prepend(warning);
}
4 years ago
var encyklopedie = document.QuerySelectorAll("#menu ul li:not(.active) a").FirstOrDefault(x => x.TextContent.Trim() == "Encyklopedie");
if (encyklopedie is object)
{
encyklopedie.TextContent = "Kurzy";
encyklopedie.SetAttribute("href", "/grafik");
if ("grafik" == path)
{
encyklopedie.ParentElement.ClassList.Add("active");
}
}
4 years ago
var logo = document.QuerySelector("#logo > h1:nth-child(1) > a:nth-child(1)");
if (logo is object)
{
logo.TextContent = "KSP Hacked Edition";
}
}
}
}