Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions Patchright.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
PatchLocator();
PatchPage();
PatchBrowserContext();
PatchRouteInitScriptFallback();
PatchClock();
PatchTracing();
PatchOptionsClasses();
Expand Down Expand Up @@ -410,7 +411,7 @@ await RouteAsync("**/*", async route =>
request.Url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
var protocol = request.Url.Split(':')[0];
await route.FallbackAsync(new RouteFallbackOptions { Url = protocol + "://patchright-init-script-inject.internal/" }).ConfigureAwait(false);
await route.FallbackAsync(new RouteFallbackOptions { Url = protocol + "://patchright-init-script-inject.internal/", PatchrightInitScript = true }).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -467,6 +468,53 @@ await RouteAsync("**/*", async route =>
File.WriteAllText(pageSupplementsInterfacePath, AddIsolatedContextToMethods(pageSupplementsInterfaceCode, "IPage", isolatedContextDefaultValue, methodNamesToPatch));
}

// Backport patchright-python#115: mark init-script fallback requests so the patched
// driver strips the sentinel URL before proxy resolution.
void PatchRouteInitScriptFallback()
{
var routeFallbackOptionsPath = Path.Combine(playwrightPath, "src", "Playwright", "API", "Generated", "Options", "RouteFallbackOptions.cs");
var routeFallbackOptionsCode = File.ReadAllText(routeFallbackOptionsPath);

Console.WriteLine($"Patching RouteFallbackOptions file: {routeFallbackOptionsPath}");

if (!routeFallbackOptionsCode.Contains("PatchrightInitScript"))
{
routeFallbackOptionsCode = routeFallbackOptionsCode
.Replace(" Url = clone.Url;", " PatchrightInitScript = clone.PatchrightInitScript;\n Url = clone.Url;")
.Replace(" /// <summary>\n /// <para>\n /// If set changes the request URL.", " [JsonPropertyName(\"patchrightInitScript\")]\n public bool? PatchrightInitScript { get; set; }\n\n /// <summary>\n /// <para>\n /// If set changes the request URL.");

File.WriteAllText(routeFallbackOptionsPath, routeFallbackOptionsCode);
}

var requestPath = Path.Combine(playwrightPath, "src", "Playwright", "Core", "Request.cs");
var requestCode = File.ReadAllText(requestPath);

Console.WriteLine($"Patching Request file: {requestPath}");

if (!requestCode.Contains("_fallbackOverrides.PatchrightInitScript"))
{
requestCode = requestCode.Replace(
" _fallbackOverrides.PostData = overrides?.PostData ?? _fallbackOverrides.PostData;\n }",
" _fallbackOverrides.PostData = overrides?.PostData ?? _fallbackOverrides.PostData;\n _fallbackOverrides.PatchrightInitScript = overrides?.PatchrightInitScript ?? _fallbackOverrides.PatchrightInitScript;\n }");

File.WriteAllText(requestPath, requestCode);
}

var routePath = Path.Combine(playwrightPath, "src", "Playwright", "Core", "Route.cs");
var routeCode = File.ReadAllText(routePath);

Console.WriteLine($"Patching Route file: {routePath}");

if (!routeCode.Contains("[\"patchrightInitScript\"]"))
{
routeCode = routeCode.Replace(
" [\"isFallback\"] = isFallback,\n })).ConfigureAwait(false);",
" [\"isFallback\"] = isFallback,\n [\"patchrightInitScript\"] = options.PatchrightInitScript == true ? true : null,\n })).ConfigureAwait(false);");

File.WriteAllText(routePath, routeCode);
}
}

// Add route injection to BrowserContext class and call from relevant methods.
void PatchBrowserContext()
{
Expand Down Expand Up @@ -506,7 +554,7 @@ await RouteAsync("**/*", async route =>
request.Url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
var protocol = request.Url.Split(':')[0];
await route.FallbackAsync(new RouteFallbackOptions { Url = protocol + "://patchright-init-script-inject.internal/" }).ConfigureAwait(false);
await route.FallbackAsync(new RouteFallbackOptions { Url = protocol + "://patchright-init-script-inject.internal/", PatchrightInitScript = true }).ConfigureAwait(false);
}
else
{
Expand Down