mirror of
https://github.com/koloml/furbooru-tagging-assistant.git
synced 2025-12-24 07:12:57 +00:00
Implemented routing to be more compatible for extension popup
This commit is contained in:
48
src/lib/popup-links.ts
Normal file
48
src/lib/popup-links.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
function resolveReplaceableLink(target: EventTarget | null = null): HTMLAnchorElement | null {
|
||||
if (!(target instanceof HTMLElement)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const closestLink = target.closest('a');
|
||||
|
||||
if (
|
||||
closestLink instanceof HTMLAnchorElement
|
||||
&& !closestLink.search
|
||||
&& closestLink.origin === location.origin
|
||||
) {
|
||||
return closestLink;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function replaceLink(linkElement: HTMLAnchorElement) {
|
||||
const params = new URLSearchParams([
|
||||
['path', linkElement.pathname]
|
||||
]);
|
||||
|
||||
linkElement.search = params.toString();
|
||||
linkElement.pathname = "/index.html";
|
||||
}
|
||||
|
||||
export function initializeLinksReplacement(): () => void {
|
||||
const abortController = new AbortController();
|
||||
const replacementHandler = (event: Event) => {
|
||||
const closestLink = resolveReplaceableLink(event.target);
|
||||
|
||||
if (closestLink) {
|
||||
replaceLink(closestLink);
|
||||
}
|
||||
}
|
||||
|
||||
// Dynamically replace the links from the Svelte default links to the links usable for the popup.
|
||||
document.body.addEventListener('mousedown', replacementHandler, {
|
||||
signal: abortController.signal,
|
||||
});
|
||||
|
||||
document.body.addEventListener('click', replacementHandler, {
|
||||
signal: abortController.signal,
|
||||
})
|
||||
|
||||
return () => abortController.abort();
|
||||
}
|
||||
Reference in New Issue
Block a user