You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tauri-plugins-workspace/plugins/opener/guest-js/init.ts

42 lines
1020 B

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { invoke } from '@tauri-apps/api/core'
// open <a href="..."> links with the API
window.addEventListener('click', function (evt) {
if (evt.defaultPrevented || evt.button !== 0 || evt.metaKey || evt.altKey)
return
const a = evt
.composedPath()
.find((el) => el instanceof Node && el.nodeName.toUpperCase() === 'A') as
| HTMLAnchorElement
| undefined
if (
!a ||
!a.href ||
// only open if supposed to be open in a new tab
!(a.target === '_blank' || evt.ctrlKey || evt.shiftKey)
)
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
})
})