🎨 Optimize the handling of URLs

This commit is contained in:
Tw93
2023-07-20 18:06:26 +08:00
parent 69b0cc10af
commit 466600dc48
2 changed files with 20 additions and 25 deletions

View File

@@ -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 detectAnchorElementClick = (e) => {
const anchorElement = e.target.closest('a'); const anchorElement = e.target.closest('a');
if (anchorElement && anchorElement.href) { if (anchorElement && anchorElement.href) {
const target = anchorElement.target;
anchorElement.target = '_self';
const hrefUrl = new URL(anchorElement.href); const hrefUrl = new URL(anchorElement.href);
const absoluteUrl = hrefUrl.href; const absoluteUrl = hrefUrl.href;
let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl);
// Handling external link redirection. // Handling external link redirection.
if ( if (isExternalLink(absoluteUrl, hrefUrl.host) && (['_blank', '_new'].includes(anchorElement.target) || externalTargetLink())) {
window.location.host !== hrefUrl.host && handleExternalLink(e, absoluteUrl);
(target === '_blank' || target === '_new' || externalTargetLink())
) {
e.preventDefault && e.preventDefault();
tauri.shell.open(absoluteUrl);
return; return;
} }
let filename = anchorElement.download || getFilenameFromUrl(absoluteUrl);
// Process download links for Rust to handle. // Process download links for Rust to handle.
// If the download attribute is set, the download attribute is used as the file name. if (isDownloadRequired(absoluteUrl, anchorElement, e) && !externalDownLoadLink()) {
if ( handleDownloadLink(e, absoluteUrl, filename);
(anchorElement.download ||
e.metaKey ||
e.ctrlKey ||
isDownloadLink(absoluteUrl)) &&
!externalDownLoadLink()
) {
e.preventDefault();
invoke('download_file', {
params: {
url: absoluteUrl,
filename,
},
});
} }
} }
}; };

View File

@@ -171,6 +171,7 @@ window.addEventListener('DOMContentLoaded', _event => {
#react-root [data-testid="AppTabBar_Explore_Link"], #react-root [data-testid="AppTabBar_Explore_Link"],
#react-root a[href*="/lists"][role="link"][aria-label], #react-root a[href*="/lists"][role="link"][aria-label],
#react-root a[href*="/i/communitynotes"][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] { #react-root a[href*="/i/verified-orgs-signup"][role="link"][aria-label] {
display: none !important; display: none !important;
} }