Web source: CSS reset per document + (0,0) anchor — kill box-vs-content padding
This commit is contained in:
@@ -15,6 +15,20 @@ namespace ytLive.Services;
|
||||
|
||||
public sealed class WebView2Manager : IDisposable
|
||||
{
|
||||
// Eliminates the wrapper padding/whitespace between the box and the page:
|
||||
// clears UA margins/paddings, forces html/body to fill the viewport and
|
||||
// anchors content at (0,0) so the rendered frame matches the widget's size.
|
||||
private const string CssResetInjection =
|
||||
"(function(){" +
|
||||
"var s=document.createElement('style');" +
|
||||
"s.textContent='html,body{margin:0!important;padding:0!important;" +
|
||||
"width:100%!important;height:100%!important;overflow:hidden!important;" +
|
||||
"background:transparent!important}';" +
|
||||
"(document.head||document.documentElement).appendChild(s);" +
|
||||
"if(document.documentElement)document.documentElement.style.background='transparent';" +
|
||||
"if(document.body)document.body.style.background='transparent';" +
|
||||
"})();";
|
||||
|
||||
private readonly Panel _hostPanel;
|
||||
private readonly Dispatcher _dispatcher;
|
||||
private readonly Dictionary<string, WebSourceSession> _sessions = new();
|
||||
@@ -108,18 +122,11 @@ public sealed class WebView2Manager : IDisposable
|
||||
|
||||
var cws = session.Control.CoreWebView2!;
|
||||
|
||||
cws.NavigationStarting += (_, e) =>
|
||||
{
|
||||
_ = cws.ExecuteScriptAsync(
|
||||
"document.documentElement.style.background='transparent';" +
|
||||
"document.body.style.background='transparent';");
|
||||
};
|
||||
await cws.AddScriptToExecuteOnDocumentCreatedAsync(CssResetInjection);
|
||||
|
||||
cws.NavigationCompleted += (_, _) =>
|
||||
{
|
||||
_ = cws.ExecuteScriptAsync(
|
||||
"document.documentElement.style.background='transparent';" +
|
||||
"document.body.style.background='transparent';");
|
||||
_ = cws.ExecuteScriptAsync(CssResetInjection);
|
||||
_ = CaptureFrame(source.Id);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user