From b696024611375c787dcad9a8e6e03ac47e46d5a9 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 14 Nov 2024 14:24:02 +0200 Subject: [PATCH] fix example, fix linux, document --- Cargo.lock | 1 + examples/api/src/views/Opener.svelte | 2 +- plugins/opener/Cargo.toml | 1 + plugins/opener/guest-js/init.ts | 34 +++++++++++++++++++++++----- plugins/opener/src/init-iife.js | 2 +- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b5b67c2..f0451beb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6574,6 +6574,7 @@ dependencies = [ "tauri", "tauri-plugin", "thiserror 2.0.3", + "url", "windows 0.58.0", "zbus", ] diff --git a/examples/api/src/views/Opener.svelte b/examples/api/src/views/Opener.svelte index 5cc16b20..6ffaca88 100644 --- a/examples/api/src/views/Opener.svelte +++ b/examples/api/src/views/Opener.svelte @@ -32,7 +32,7 @@ function openPath() { opener - .openPath(path, pathProgram === 'Default' ? undefined : urlProgram) + .openPath(path, pathProgram === 'Default' ? undefined : pathProgram) .catch(onMessage) } diff --git a/plugins/opener/Cargo.toml b/plugins/opener/Cargo.toml index 81a22036..20eee7d2 100644 --- a/plugins/opener/Cargo.toml +++ b/plugins/opener/Cargo.toml @@ -51,6 +51,7 @@ features = [ [target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"netbsd\", target_os = \"openbsd\"))".dependencies] zbus = { workspace = true } +url = { workspace = true } [target."cfg(target_os = \"macos\")".dependencies.objc2-app-kit] version = "0.2" diff --git a/plugins/opener/guest-js/init.ts b/plugins/opener/guest-js/init.ts index d2070e8e..9017c5ef 100644 --- a/plugins/opener/guest-js/init.ts +++ b/plugins/opener/guest-js/init.ts @@ -6,7 +6,19 @@ import { invoke } from '@tauri-apps/api/core' // open links with the API window.addEventListener('click', function (evt) { - if (evt.defaultPrevented || evt.button !== 0 || evt.metaKey || evt.altKey) + console.log(evt.button) + + // return early if + if ( + // event was prevented + evt.defaultPrevented || + // or not a left click or middle click + evt.button !== 0 || + // or meta key pressed + evt.metaKey || + // or al key pressed + evt.altKey + ) return const a = evt @@ -15,20 +27,30 @@ window.addEventListener('click', function (evt) { | HTMLAnchorElement | undefined + // return early if if ( + // not tirggered from element !a || + // or doesn't have a href !a.href || - // only open if supposed to be open in a new tab - !(a.target === '_blank' || evt.ctrlKey || evt.shiftKey) + // 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 ( - // only open if not same origin + // same origin (internal navigation) url.origin === window.location.origin || - // only open default protocols + // not default protocols ['http:', 'https:', 'mailto:', 'tel:'].every((p) => url.protocol !== p) ) return @@ -36,6 +58,6 @@ window.addEventListener('click', function (evt) { evt.preventDefault() void invoke('plugin:opener|open_url', { - path: url + url }) }) diff --git a/plugins/opener/src/init-iife.js b/plugins/opener/src/init-iife.js index ac75587c..34fb3ea9 100644 --- a/plugins/opener/src/init-iife.js +++ b/plugins/opener/src/init-iife.js @@ -1 +1 @@ -!function(){"use strict";"function"==typeof SuppressedError&&SuppressedError,window.addEventListener("click",(function(e){if(e.defaultPrevented||0!==e.button||e.metaKey||e.altKey)return;const t=e.composedPath().find((e=>e instanceof Node&&"A"===e.nodeName.toUpperCase()));if(!t||!t.href||"_blank"!==t.target&&!e.ctrlKey&&!e.shiftKey)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}))}))}(); +!function(){"use strict";"function"==typeof SuppressedError&&SuppressedError,window.addEventListener("click",(function(e){if(console.log(e.button),e.defaultPrevented||0!==e.button||e.metaKey||e.altKey)return;const t=e.composedPath().find((e=>e instanceof Node&&"A"===e.nodeName.toUpperCase()));if(!t||!t.href||"_blank"!==t.target&&!e.ctrlKey&&!e.shiftKey)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",{url:n}))}))}();