Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ItemGroup>
<PackageVersion Include="AsyncImageLoader.Avalonia" Version="3.8.0" />
<PackageVersion Include="Avalonia" Version="12.1.1" />
<PackageVersion Include="Avalonia.Controls.WebView" Version="12.1.0" />
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="12.1.1" />
<PackageVersion Include="Avalonia.Desktop" Version="12.1.1" />
<PackageVersion Include="Cogwheel" Version="2.1.1" />
Expand All @@ -25,8 +26,6 @@
<PackageVersion Include="PowerKit" Version="2.3.1" />
<PackageVersion Include="TagLibSharp" Version="2.3.0" />
<PackageVersion Include="ThisAssembly.Project" Version="2.1.5" />
<PackageVersion Include="WebView.Avalonia" Version="11.0.0.1" />
<PackageVersion Include="WebView.Avalonia.Desktop" Version="11.0.0.1" />
<PackageVersion Include="YoutubeExplode" Version="6.6.2" />
<PackageVersion Include="YoutubeExplode.Converter" Version="6.6.2" />
</ItemGroup>
Expand Down
8 changes: 0 additions & 8 deletions YoutubeDownloader/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Platform;
using AvaloniaWebView;
using Material.Styles.Themes;
using Microsoft.Extensions.DependencyInjection;
using PowerKit.Extensions;
Expand Down Expand Up @@ -96,13 +95,6 @@ public override void Initialize()
AvaloniaXamlLoader.Load(this);
}

public override void RegisterServices()
{
base.RegisterServices();

AvaloniaWebViewBuilder.Initialize(config => config.IsInPrivateModeEnabled = true);
}

public override void OnFrameworkInitializationCompleted()
{
// Load settings
Expand Down
3 changes: 1 addition & 2 deletions YoutubeDownloader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Reflection;
using Avalonia;
using Avalonia.WebView.Desktop;
using YoutubeDownloader.Utils;

namespace YoutubeDownloader;
Expand All @@ -23,7 +22,7 @@ public static class Program
public static string ProjectReleasesUrl { get; } = $"{ProjectUrl}/releases";

public static AppBuilder BuildAvaloniaApp() =>
AppBuilder.Configure<App>().UsePlatformDetect().LogToTrace().UseDesktopWebView();
AppBuilder.Configure<App>().UsePlatformDetect().LogToTrace();

[STAThread]
public static int Main(string[] args)
Expand Down
6 changes: 3 additions & 3 deletions YoutubeDownloader/Views/Dialogs/AuthSetupView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@
Text="{Binding LocalizationManager.LoadingText}" />

<!-- Browser -->
<WebView
<NativeWebView
x:Name="WebBrowser"
IsVisible="{Binding !IsAuthenticated}"
EnvironmentRequested="WebBrowser_OnEnvironmentRequested"
Loaded="WebBrowser_OnLoaded"
NavigationStarting="WebBrowser_OnNavigationStarting"
WebViewCreated="WebBrowser_OnWebViewCreated" />
NavigationStarted="WebBrowser_OnNavigationStarted" />
</Panel>
</Border>

Expand Down
73 changes: 42 additions & 31 deletions YoutubeDownloader/Views/Dialogs/AuthSetupView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Linq;
using System.Net;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.WebView.Windows.Core;
using Microsoft.Web.WebView2.Core;
using WebViewCore.Events;
using Avalonia.Platform;
using YoutubeDownloader.Framework;
using YoutubeDownloader.ViewModels.Dialogs;

Expand All @@ -16,11 +16,19 @@ public partial class AuthSetupView : UserControl<AuthSetupViewModel>
$"https://accounts.google.com/ServiceLogin?continue={Uri.EscapeDataString(HomePageUri.AbsoluteUri)}"
);

private CoreWebView2? _coreWebView2;

public AuthSetupView() => InitializeComponent();

private void NavigateToLoginPage() => WebBrowser.Url = LoginPageUri;
private void NavigateToLoginPage() => WebBrowser.Source = LoginPageUri;

// Cookie domains may be scoped to a parent domain (e.g. ".youtube.com"), so check
// that the home page host matches the cookie's domain exactly or as a subdomain
private static bool IsApplicableToHomePage(Cookie cookie)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
var domain = cookie.Domain.TrimStart('.');

return string.Equals(domain, HomePageUri.Host, StringComparison.OrdinalIgnoreCase)
|| HomePageUri.Host.EndsWith('.' + domain, StringComparison.OrdinalIgnoreCase);
}

private void LogOutButton_OnClick(object sender, RoutedEventArgs args)
{
Expand All @@ -30,56 +38,59 @@ private void LogOutButton_OnClick(object sender, RoutedEventArgs args)

private void WebBrowser_OnLoaded(object sender, RoutedEventArgs args) => NavigateToLoginPage();

private void WebBrowser_OnWebViewCreated(object sender, WebViewCreatedEventArgs args)
private void WebBrowser_OnEnvironmentRequested(
object? sender,
WebViewEnvironmentRequestedEventArgs args
)
{
if (!args.IsSucceed)
return;

var platformWebView = WebBrowser.PlatformWebView as WebView2Core;
var coreWebView2 = platformWebView?.CoreWebView2;

if (coreWebView2 is null)
return;
// Prevent DevTools from being accessible through the default context menu or
// keyboard shortcuts, which would otherwise allow inspecting the authentication session
args.EnableDevTools = false;

coreWebView2.Settings.AreDefaultContextMenusEnabled = false;
coreWebView2.Settings.AreDevToolsEnabled = false;
coreWebView2.Settings.IsGeneralAutofillEnabled = false;
coreWebView2.Settings.IsPasswordAutosaveEnabled = false;
coreWebView2.Settings.IsStatusBarEnabled = false;
coreWebView2.Settings.IsSwipeNavigationEnabled = false;

_coreWebView2 = coreWebView2;
switch (args)
{
case WindowsWebView2EnvironmentRequestedEventArgs windowsArgs:
windowsArgs.IsInPrivateModeEnabled = true;
break;
Comment thread
Tyrrrz marked this conversation as resolved.
case AppleWKWebViewEnvironmentRequestedEventArgs appleArgs:
appleArgs.NonPersistentDataStore = true;
break;
case GtkWebViewEnvironmentRequestedEventArgs gtkArgs:
gtkArgs.EphemeralDataManager = true;
break;
}
}
Comment thread
Tyrrrz marked this conversation as resolved.

private async void WebBrowser_OnNavigationStarting(
private async void WebBrowser_OnNavigationStarted(
object? sender,
WebViewUrlLoadingEventArg args
WebViewNavigationStartingEventArgs args
)
{
if (_coreWebView2 is null)
if (WebBrowser.TryGetCookieManager() is not { } cookieManager)
return;

// Reset existing browser cookies if the user is attempting to log in (again)
if (
string.Equals(
args.Url?.AbsoluteUri,
args.Request?.AbsoluteUri,
LoginPageUri.AbsoluteUri,
StringComparison.OrdinalIgnoreCase
)
)
{
_coreWebView2.CookieManager.DeleteAllCookies();
foreach (var cookie in await cookieManager.GetCookiesAsync())
cookieManager.DeleteCookie(cookie);
Comment thread
Tyrrrz marked this conversation as resolved.
}

// Extract the cookies after being redirected to the home page (i.e., after logging in)
if (
args.Url is { } url
args.Request is { } url
&& string.Equals(url.Scheme, HomePageUri.Scheme, StringComparison.OrdinalIgnoreCase)
&& string.Equals(url.Host, HomePageUri.Host, StringComparison.OrdinalIgnoreCase)
)
{
var cookies = await _coreWebView2!.CookieManager.GetCookiesAsync(url.AbsoluteUri);
DataContext.Cookies = cookies.Select(c => c.ToSystemNetCookie()).ToArray();
var cookies = await cookieManager.GetCookiesAsync();
DataContext.Cookies = cookies.Where(IsApplicableToHomePage).ToArray();
}
}
}
16 changes: 3 additions & 13 deletions YoutubeDownloader/YoutubeDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<PackageReference Include="AsyncImageLoader.Avalonia" />
<PackageReference Include="Avalonia" />
<PackageReference Include="Avalonia.Controls.DataGrid" />
<PackageReference Include="Avalonia.Controls.WebView" />
<PackageReference Include="Avalonia.Desktop" />
<PackageReference Include="Cogwheel" />
<PackageReference Include="CommunityToolkit.Mvvm" />
Expand All @@ -49,25 +50,14 @@
<PackageReference Include="Onova" />
<PackageReference Include="PowerKit" />
<PackageReference Include="ThisAssembly.Project" PrivateAssets="all" />
<PackageReference Include="WebView.Avalonia" />
<PackageReference Include="WebView.Avalonia.Desktop" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../YoutubeDownloader.Core/YoutubeDownloader.Core.csproj" />
</ItemGroup>

<!-- Avalonia.WebView is completely incompatible with trimming -->
<!-- Avalonia.Controls.WebView is completely incompatible with trimming -->
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
<ItemGroup>
<TrimmerRootAssembly Include="Avalonia.WebView" />
<TrimmerRootAssembly Include="Avalonia.WebView.Desktop" />
<TrimmerRootAssembly Include="Avalonia.WebView.Linux" />
<TrimmerRootAssembly Include="Avalonia.WebView.MacCatalyst" />
<TrimmerRootAssembly Include="Avalonia.WebView.Windows" />
<TrimmerRootAssembly Include="AvaloniaWebView.Shared" />
<TrimmerRootAssembly Include="Linux.WebView.Core" />
<TrimmerRootAssembly Include="Microsoft.Web.WebView2.Core" />
<TrimmerRootAssembly Include="WebView.Avalonia" />
<TrimmerRootAssembly Include="WebView.Core" />
<TrimmerRootAssembly Include="Avalonia.Controls.WebView" />
</ItemGroup>
</Project>