Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion SharpPad.Desktop/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
Opacity="{Binding WebViewOpacity}"
Background="#1E1E1E"
NavigationStarting="OnNavigationStarting"
NavigationCompleted="OnNavigationCompleted">
NavigationCompleted="OnNavigationCompleted"
WebViewNewWindowRequested="OnNewWindowRequested">
</WebView>
</Border>

Expand Down
31 changes: 30 additions & 1 deletion SharpPad.Desktop/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SharpPad.Desktop.ViewModels;
using WebViewCore.Events;
using System;
using WebViewCore.Enums;

namespace SharpPad.Desktop.Views;

Expand Down Expand Up @@ -46,4 +47,32 @@ protected override void OnClosed(EventArgs e)
base.OnClosed(e);
_bridge.Dispose();
}
}

private void OnNewWindowRequested(object? sender, WebViewNewWindowEventArgs e)
{
// 获取当前应用的基础URL
if (DataContext is MainWindowViewModel viewModel)
{
var appBaseUrl = viewModel.WebUrl;
var navigationUrl = e.Url?.ToString() ?? string.Empty;

// 如果导航URL不是空的,并且不是应用的基础URL(或其子路径)
if (!string.IsNullOrEmpty(navigationUrl) &&
!string.IsNullOrEmpty(appBaseUrl) &&
Uri.TryCreate(appBaseUrl, UriKind.Absolute, out var appUri) &&
Uri.TryCreate(navigationUrl, UriKind.Absolute, out var navUri))
{
// 如果是外部链接(不同的host或scheme)
if (navUri.Host != appUri.Host || navUri.Scheme != appUri.Scheme)
{
// 取消WebView导航
if (Uri.TryCreate(appBaseUrl, UriKind.Absolute, out var tempAppUri))
{
e.Url = tempAppUri;
e.UrlLoadingStrategy = UrlRequestStrategy.OpenExternally;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: External Links Redirected to Base URL

In OnNewWindowRequested, when an external link is detected, e.Url is incorrectly overwritten with the app's base URL before setting UrlLoadingStrategy to OpenExternally. This results in the app's base URL opening externally instead of the intended external link.

Fix in Cursor Fix in Web

}
}
}
}
}
}
Loading