🎨 Optimize the handling of URLs
This commit is contained in:
44
src-tauri/src/inject/event.js
vendored
44
src-tauri/src/inject/event.js
vendored
@@ -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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
1
src-tauri/src/inject/style.js
vendored
1
src-tauri/src/inject/style.js
vendored
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user