From 466600dc487f52dfffaf4e4d9b908799c3f3cde2 Mon Sep 17 00:00:00 2001 From: Tw93 Date: Thu, 20 Jul 2023 18:06:26 +0800 Subject: [PATCH] :art: Optimize the handling of URLs --- src-tauri/src/inject/event.js | 44 +++++++++++++++-------------------- src-tauri/src/inject/style.js | 1 + 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js index 962fda3..0346441 100644 --- a/src-tauri/src/inject/event.js +++ b/src-tauri/src/inject/event.js @@ -94,42 +94,36 @@ document.addEventListener('DOMContentLoaded', () => { } }); + const isExternalLink = (url, host) => window.location.host !== host; + const isDownloadRequired = (url, anchorElement, e) => + anchorElement.download || e.metaKey || e.ctrlKey || isDownloadLink(url); + + const handleExternalLink = (e, url) => { + e.preventDefault(); + tauri.shell.open(url); + }; + + const handleDownloadLink = (e, url, filename) => { + e.preventDefault(); + invoke('download_file', { params: { url, filename } }); + }; + const detectAnchorElementClick = (e) => { const anchorElement = e.target.closest('a'); if (anchorElement && anchorElement.href) { - const target = anchorElement.target; - anchorElement.target = '_self'; const hrefUrl = new URL(anchorElement.href); const absoluteUrl = hrefUrl.href; + let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl); // Handling external link redirection. - if ( - window.location.host !== hrefUrl.host && - (target === '_blank' || target === '_new' || externalTargetLink()) - ) { - e.preventDefault && e.preventDefault(); - tauri.shell.open(absoluteUrl); + if (isExternalLink(absoluteUrl, hrefUrl.host) && (['_blank', '_new'].includes(anchorElement.target) || externalTargetLink())) { + handleExternalLink(e, absoluteUrl); return; } - let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl); - // Process download links for Rust to handle. - // If the download attribute is set, the download attribute is used as the file name. - if ( - (anchorElement.download || - e.metaKey || - e.ctrlKey || - isDownloadLink(absoluteUrl)) && - !externalDownLoadLink() - ) { - e.preventDefault(); - invoke('download_file', { - params: { - url: absoluteUrl, - filename, - }, - }); + if (isDownloadRequired(absoluteUrl, anchorElement, e) && !externalDownLoadLink()) { + handleDownloadLink(e, absoluteUrl, filename); } } }; diff --git a/src-tauri/src/inject/style.js b/src-tauri/src/inject/style.js index 03730c3..9792638 100644 --- a/src-tauri/src/inject/style.js +++ b/src-tauri/src/inject/style.js @@ -171,6 +171,7 @@ window.addEventListener('DOMContentLoaded', _event => { #react-root [data-testid="AppTabBar_Explore_Link"], #react-root a[href*="/lists"][role="link"][aria-label], #react-root a[href*="/i/communitynotes"][role="link"][aria-label], + #react-root a[role="link"][aria-label="Communities"], #react-root a[href*="/i/verified-orgs-signup"][role="link"][aria-label] { display: none !important; }