refactor init script

pull/2019/head
amrbashir 9 months ago
parent bcc6c092f2
commit e0264cca3c
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

@ -5,36 +5,39 @@
import { invoke } from '@tauri-apps/api/core'
// open <a href="..."> 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)
}
})

@ -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}))}))}();

Loading…
Cancel
Save