Files
upstream-mirror 9ad64ec987
Some checks failed
integration tests / run-integration-tests (windows-latest) (push) Has been cancelled
Test Rust / test (push) Has been cancelled
test plugins e2e (mobile) / ios (push) Has been cancelled
test plugins e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test plugins e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
integration tests / run-integration-tests (macos-latest) (push) Has been cancelled
version or publish / version-or-publish (push) Failing after 0s
integration tests / run-integration-tests (ubuntu-22.04) (push) Failing after 0s
Lint JavaScript / eslint (push) Failing after 0s
Sync / sync-to-mirrors (push) Failing after 0s
test plugins e2e (mobile) / android (push) Failing after 0s
test plugins e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
Test Rust / changes (push) Failing after 0s
Lint Rust / changes (push) Failing after 0s
Lint Rust / clippy (push) Has been skipped
Audit Rust / audit-rust (push) Failing after 1s
Audit JavaScript / audit-js (push) Failing after 1s
snapshot of https://github.com/tauri-apps/plugins-workspace.git (v2, 2026-09-23; blobs > 131072B omitted)
2026-09-23 17:17:10 +05:00

52 lines
1.4 KiB
TypeScript

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* Webview script injected by the shell plugin on every page load.
*
* It installs a `click` listener on `document.body` that intercepts clicks on
* `<a target="_blank">` elements whose `href` starts with `http://`, `https://`,
* `mailto:` or `tel:`, cancels the navigation and opens the link with the
* system's default application through the `plugin:shell|open` command instead.
*
* @module
*/
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
if (
t.href !== ''
&& ['http://', 'https://', 'mailto:', 'tel:'].some((v) =>
t.href.startsWith(v)
)
&& t.target === '_blank'
) {
void invoke('plugin:shell|open', {
path: t.href
})
e.preventDefault()
}
break
}
target = target.parentElement
}
})
}
if (
document.readyState === 'complete'
|| document.readyState === 'interactive'
) {
openLinks()
} else {
window.addEventListener('DOMContentLoaded', openLinks, true)
}