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

44 lines
1023 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 ||
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
})
})