Files
cloakhq--cloakbrowser/dotnet/src/CloakBrowser/ViewportDefaults.cs
T
wehub-resource-sync 0af812490d
CI / python (push) Failing after 0s
CI / javascript (push) Failing after 1s
CI / dotnet (push) Failing after 1s
chore: import upstream snapshot with attribution
2026-07-13 12:12:36 +08:00

37 lines
1.6 KiB
C#

using Microsoft.Playwright;
namespace CloakBrowser;
/// <summary>
/// Shared headed-launch viewport defaulting - the single source of truth behind both
/// <see cref="CloakBrowserHandle"/> and the humanize <c>HumanizedBrowser</c> wrapper, so
/// the default applies no matter which path creates the page/context (mirrors Python's
/// single <c>_default_no_viewport</c> that monkey-patches the raw browser).
///
/// On a headed launch, a page/context the caller gave no explicit viewport defaults to
/// <see cref="ViewportSize.NoViewport"/> so it tracks the real OS window - a bare emulated
/// viewport on a headed window yields <c>outerWidth &lt; innerWidth</c> (a physically
/// impossible window / bot tell). Headless keeps Playwright's default (coherent there).
/// An explicit viewport (including <see cref="ViewportSize.NoViewport"/>) is always honored.
/// </summary>
internal static class ViewportDefaults
{
public static BrowserNewPageOptions ApplyHeadedNoViewport(
BrowserNewPageOptions? options, bool headless, bool headlessNoViewport = false)
{
var o = options ?? new BrowserNewPageOptions();
if (headless && !headlessNoViewport) return o;
o.ViewportSize ??= ViewportSize.NoViewport;
return o;
}
public static BrowserNewContextOptions ApplyHeadedNoViewport(
BrowserNewContextOptions? options, bool headless, bool headlessNoViewport = false)
{
var o = options ?? new BrowserNewContextOptions();
if (headless && !headlessNoViewport) return o;
o.ViewportSize ??= ViewportSize.NoViewport;
return o;
}
}