Support force internal navigation parameter

This commit is contained in:
Tw93
2025-11-11 15:40:10 +08:00
parent aa65c7cd19
commit eb829ca85c
9 changed files with 56 additions and 3 deletions

3
src-tauri/pake.json vendored
View File

@@ -17,7 +17,8 @@
"enable_wasm": false,
"enable_drag_drop": false,
"maximize": false,
"start_to_tray": false
"start_to_tray": false,
"force_internal_navigation": false
}
],
"user_agent": {

View File

@@ -20,6 +20,8 @@ pub struct WindowConfig {
pub enable_wasm: bool,
pub enable_drag_drop: bool,
pub start_to_tray: bool,
#[serde(default)]
pub force_internal_navigation: bool,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@@ -192,6 +192,8 @@ document.addEventListener("DOMContentLoaded", () => {
const tauri = window.__TAURI__;
const appWindow = tauri.window.getCurrentWindow();
const invoke = tauri.core.invoke;
const pakeConfig = window["pakeConfig"] || {};
const forceInternalNavigation = pakeConfig.force_internal_navigation === true;
if (!document.getElementById("pake-top-dom")) {
const topDom = document.createElement("div");
@@ -394,9 +396,15 @@ document.addEventListener("DOMContentLoaded", () => {
// Handle _blank links: same domain navigates in-app, cross-domain opens new window
if (target === "_blank") {
if (forceInternalNavigation) {
e.preventDefault();
e.stopImmediatePropagation();
window.location.href = absoluteUrl;
return;
}
if (isSameDomain(absoluteUrl)) {
// For same-domain links, let the browser/SPA handle it naturally
// This prevents full page reload in SPA apps like Discord
// For same-domain links, let the browser handle it naturally
return;
}
@@ -413,6 +421,10 @@ document.addEventListener("DOMContentLoaded", () => {
}
if (target === "_new") {
if (forceInternalNavigation) {
return;
}
e.preventDefault();
handleExternalLink(absoluteUrl);
return;
@@ -435,6 +447,10 @@ document.addEventListener("DOMContentLoaded", () => {
// Handle regular links: same domain allows normal navigation, cross-domain opens new window
if (!target || target === "_self") {
if (!isSameDomain(absoluteUrl)) {
if (forceInternalNavigation) {
return;
}
e.preventDefault();
e.stopImmediatePropagation();
const newWindow = originalWindowOpen.call(
@@ -468,6 +484,10 @@ document.addEventListener("DOMContentLoaded", () => {
const absoluteUrl = hrefUrl.href;
if (!isSameDomain(absoluteUrl)) {
if (forceInternalNavigation) {
return originalWindowOpen.call(window, absoluteUrl, name, specs);
}
handleExternalLink(absoluteUrl);
return null;
}