From e0264cca3c1e59dda591fafa4ba4b1e327bdbedc Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 12 Nov 2024 17:45:56 +0200 Subject: [PATCH] refactor init script --- plugins/opener/guest-js/init.ts | 67 +++++++++++++++++---------------- plugins/opener/src/init-iife.js | 2 +- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/plugins/opener/guest-js/init.ts b/plugins/opener/guest-js/init.ts index ef7f4554..b77c7452 100644 --- a/plugins/opener/guest-js/init.ts +++ b/plugins/opener/guest-js/init.ts @@ -5,36 +5,39 @@ import { invoke } from '@tauri-apps/api/core' // open links with the API -function openLinks(): void { - document.querySelector('body')?.addEventListener('click', function (e) { - let target: HTMLElement | null = e.target as HTMLElement - while (target) { - if (target.matches('a')) { - const t = target as HTMLAnchorElement - if ( - t.href !== '' && - ['http://', 'https://', 'mailto:', 'tel:'].some((v) => - t.href.startsWith(v) - ) && - t.target === '_blank' - ) { - void invoke('plugin:opener|open', { - path: t.href - }) - e.preventDefault() - } - break - } - target = target.parentElement - } +window.addEventListener('click', function (evt) { + if ( + evt.defaultPrevented || + evt.button !== 0 || + evt.metaKey || + evt.altKey || + evt.ctrlKey || + evt.shiftKey + ) + return + + const a = evt + .composedPath() + .find((el) => el instanceof Node && el.nodeName.toUpperCase() === 'A') as + | HTMLAnchorElement + | undefined + + // only open if supposed to be open in a new tab + if (!a || !a.href || a.target !== '_blank') return + + const url = new URL(a.href) + + if ( + // only open if not same origin + url.origin === window.location.origin || + // only open default protocols + ['http:', 'https:', 'mailto:', 'tel:'].every((p) => url.protocol !== p) + ) + return + + evt.preventDefault() + + void invoke('plugin:opener|open_url', { + path: url }) -} - -if ( - document.readyState === 'complete' || - document.readyState === 'interactive' -) { - openLinks() -} else { - window.addEventListener('DOMContentLoaded', openLinks, true) -} +}) diff --git a/plugins/opener/src/init-iife.js b/plugins/opener/src/init-iife.js index 47e1df39..4703edc8 100644 --- a/plugins/opener/src/init-iife.js +++ b/plugins/opener/src/init-iife.js @@ -1 +1 @@ -!function(){"use strict";async function e(e,t={},n){return window.__TAURI_INTERNALS__.invoke(e,t,n)}function t(){document.querySelector("body")?.addEventListener("click",(function(t){let n=t.target;for(;n;){if(n.matches("a")){const r=n;""!==r.href&&["http://","https://","mailto:","tel:"].some((e=>r.href.startsWith(e)))&&"_blank"===r.target&&(e("plugin:opener|open",{path:r.href}),t.preventDefault());break}n=n.parentElement}}))}"function"==typeof SuppressedError&&SuppressedError,"complete"===document.readyState||"interactive"===document.readyState?t():window.addEventListener("DOMContentLoaded",t,!0)}(); +!function(){"use strict";"function"==typeof SuppressedError&&SuppressedError,window.addEventListener("click",(function(e){if(e.defaultPrevented||0!==e.button||e.metaKey||e.altKey||e.ctrlKey||e.shiftKey)return;const t=e.composedPath().find((e=>e instanceof Node&&"A"===e.nodeName.toUpperCase()));if(!t||!t.href||"_blank"!==t.target)return;const n=new URL(t.href);n.origin===window.location.origin||["http:","https:","mailto:","tel:"].every((e=>n.protocol!==e))||(e.preventDefault(),async function(e,t={},n){window.__TAURI_INTERNALS__.invoke(e,t,n)}("plugin:opener|open_url",{path:n}))}))}();