snapshot of https://github.com/tauri-apps/plugins-workspace.git (v2, 2026-09-23; blobs > 131072B omitted)
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
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
This commit is contained in:
99
plugins/opener/guest-js/index.ts
Normal file
99
plugins/opener/guest-js/index.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/**
|
||||
* Open files and URLs using their default application.
|
||||
*
|
||||
* ## Security
|
||||
*
|
||||
* This API has a scope configuration that forces you to restrict the files and urls to be opened.
|
||||
*
|
||||
* ### Restricting access to the {@link open | `open`} API
|
||||
*
|
||||
* On the configuration object, `open: true` means that the {@link open} API can be used with any URL,
|
||||
* as the argument is validated with the `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+` regex.
|
||||
* You can change that regex by changing the boolean value to a string, e.g. `open: ^https://github.com/`.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
|
||||
/**
|
||||
* Opens a url with the system's default app, or the one specified with {@linkcode openWith}.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { openUrl } from '@tauri-apps/plugin-opener';
|
||||
*
|
||||
* // opens the given URL on the default browser:
|
||||
* await openUrl('https://github.com/tauri-apps/tauri');
|
||||
* // opens the given URL using `firefox`:
|
||||
* await openUrl('https://github.com/tauri-apps/tauri', 'firefox');
|
||||
* ```
|
||||
*
|
||||
* @param url The URL to open.
|
||||
* @param openWith The app to open the URL with. If not specified, defaults to the system default application for the specified url type.
|
||||
* On mobile, `openWith` can be provided as `inAppBrowser` to open the URL in an in-app browser. Otherwise, it will open the URL in the system default browser.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export async function openUrl(
|
||||
url: string | URL,
|
||||
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
||||
openWith?: 'inAppBrowser' | string
|
||||
): Promise<void> {
|
||||
await invoke('plugin:opener|open_url', {
|
||||
url,
|
||||
with: openWith
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a path with the system's default app, or the one specified with {@linkcode openWith}.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { openPath } from '@tauri-apps/plugin-opener';
|
||||
*
|
||||
* // opens a file using the default program:
|
||||
* await openPath('/path/to/file');
|
||||
* // opens a file using `vlc` command on Windows.
|
||||
* await openPath('C:/path/to/file', 'vlc');
|
||||
* ```
|
||||
*
|
||||
* @param path The path to open.
|
||||
* @param openWith The app to open the path with. If not specified, defaults to the system default application for the specified path type.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export async function openPath(path: string, openWith?: string): Promise<void> {
|
||||
await invoke('plugin:opener|open_path', {
|
||||
path,
|
||||
with: openWith
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Reveal a path with the system's default explorer.
|
||||
*
|
||||
* #### Platform-specific:
|
||||
*
|
||||
* - **Android / iOS:** Unsupported.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { revealItemInDir } from '@tauri-apps/plugin-opener';
|
||||
* await revealItemInDir('/path/to/file');
|
||||
* await revealItemInDir([ '/path/to/file', '/path/to/another/file' ]);
|
||||
* ```
|
||||
*
|
||||
* @param path The path to reveal.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export async function revealItemInDir(path: string | string[]): Promise<void> {
|
||||
const paths = typeof path === 'string' ? [path] : path
|
||||
return invoke('plugin:opener|reveal_item_in_dir', { paths })
|
||||
}
|
||||
70
plugins/opener/guest-js/init.ts
Normal file
70
plugins/opener/guest-js/init.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/**
|
||||
* Webview script injected by the opener plugin on every page load.
|
||||
*
|
||||
* It installs a `click` listener on `window` that intercepts clicks on `<a>` elements whose
|
||||
* `target` is `_blank` (or that are clicked while holding `Ctrl` or `Shift`) and whose `href`
|
||||
* uses the `http:`, `https:`, `mailto:` or `tel:` protocol, cancels the navigation and opens the
|
||||
* link with the system's default browser through the `plugin:opener|open_url` command instead.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
|
||||
// open <a href="..."> links with the API
|
||||
window.addEventListener('click', function (evt) {
|
||||
// return early if
|
||||
if (
|
||||
// event was prevented
|
||||
evt.defaultPrevented
|
||||
// or not a left click
|
||||
|| evt.button !== 0
|
||||
// or meta key pressed
|
||||
|| evt.metaKey
|
||||
// or al key pressed
|
||||
|| evt.altKey
|
||||
)
|
||||
return
|
||||
|
||||
const a = evt
|
||||
.composedPath()
|
||||
.find((el) => el instanceof Node && el.nodeName.toUpperCase() === 'A') as
|
||||
| HTMLAnchorElement
|
||||
| undefined
|
||||
|
||||
// return early if
|
||||
if (
|
||||
// not tirggered from <a> element
|
||||
!a
|
||||
// or doesn't have a href
|
||||
|| !a.href
|
||||
// or not supposed to be open in a new tab
|
||||
|| !(
|
||||
a.target === '_blank'
|
||||
// or ctrl key pressed
|
||||
|| evt.ctrlKey
|
||||
// or shift key pressed
|
||||
|| evt.shiftKey
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
const url = new URL(a.href)
|
||||
|
||||
// return early if
|
||||
if (
|
||||
// not default protocols
|
||||
['http:', 'https:', 'mailto:', 'tel:'].every((p) => url.protocol !== p)
|
||||
)
|
||||
return
|
||||
|
||||
evt.preventDefault()
|
||||
|
||||
void invoke('plugin:opener|open_url', {
|
||||
url
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user