Installations
npm install tauri-plugin-sharetarget-api
Developer Guide
Typescript
Yes
Module System
ESM
Score
66.9
Supply Chain
98
Quality
75.9
Maintenance
100
Vulnerability
80.9
License
Releases
Unable to fetch releases
Download Statistics
Total Downloads
508
Last Day
1
Last Week
25
Last Month
170
Last Year
508
Bundle Size
2.24 kB
Minified
1.00 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.1.6
Package Id
tauri-plugin-sharetarget-api@0.1.6
Unpacked Size
7.88 kB
Size
3.10 kB
File Count
11
Publised On
02 Nov 2024
Total Downloads
Cumulative downloads
Total Downloads
508
Last day
0%
1
Compared to previous day
Last week
2,400%
25
Compared to previous week
Last month
-35.1%
170
Compared to previous month
Last year
0%
508
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
5
Tauri Plugin sharetarget
A plugin for Tauri applications to appear as a share target under Android. Desktop OSes are unsupported (they lack the feature). Behaviour on iOs is indeterminate.
Installation
In src-tauri/Cargo.toml
:
1[dependencies] 2tauri-plugin-sharetarget = "LATEST_VERSION_HERE"
In src-tauri/src/lib.rs
, add the plugin entry :
1#[cfg_attr(mobile, tauri::mobile_entry_point)] 2pub fn run() { 3 tauri::Builder::default() 4 .plugin(tauri_plugin_sharetarget::init()) 5 .run(tauri::generate_context!()) 6 .expect("error while running tauri application"); 7}
To build for Android, you must first tauri android init
successfully. This gets some files
generated. To signal your app as a share target to Android, you then need to modify your
AndroidManifest.xml
.
In src-tauri/gen/android/app/src/main/AndroidManifest.xml
, add your intent-filter
s :
1<?xml version="1.0" encoding="utf-8"> 2<manifest ...> 3 ... 4 <application ...> 5 ... 6 <activity ...> 7 <intent-filter> 8 <!-- Support receiving share events. --> 9 <action android:name="android.intent.action.SEND" /> 10 <category android:name="android.intent.category.DEFAULT" /> 11 <!-- You can scope any MIME type here. You'll see what Intent Android returns. --> 12 <data android:mimeType="text/*" /> 13 </intent-filter> 14 </activity ...> 15 ...
Permissions
First you need permissions in tauri, just to get ipc events in javascript.
In src-tauri/capabilities/default.json
, add sharetarget
to the permissions :
1{ 2 "$schema": "../gen/schemas/desktop-schema.json", 3 "identifier": "anything_you_like", 4 "windows": ["main"], 5 "permissions": [ 6 ... 7 "sharetarget:default" 8 ] 9}
Usage
Use the provided API in javascript/typescript. For example in React, in src/main.tsx
:
1import { useEffect, useState } from 'react'; 2import { listenForShareEvents, type ShareEvent } from 'tauri-plugin-sharetarget-api'; 3import { PluginListener } from '@tauri-apps/api/core'; 4 5function App() { 6 const [logs, setLogs] = useState(''); 7 useEffect(() => { 8 let listener: PluginListener; 9 const setupListener = async () => { 10 listener = await listenForShareEvents((intent: ShareEvent) => { 11 setLogs(intent.uri); 12 }); 13 }; 14 return () => { listener?.unregister(); }; 15 }; 16 return (<> 17 <h3>Share this</h3> 18 <p>{ logs }</p> 19 <button onClick={ yourCallbackFunction }>share</button> 20 </>); 21}
Receive attached stream (images, etc)
To receive shared images, you need
- an
intent
targetingimage/*
@tauri-apps/plugin-fs
inpackage.json
dependencies to read the sent datafs:default
in the capabilities of your app- in javascript, use
readFile()
from@tauri-apps/plugin-fs
on the intent'sstream
.
Here is the previous example revamped to fetch binary contents. Upload({ file })
is not implemented because users may do whatever they like with the File
object.
This just showcases how to grab the binary data.
1import { useEffect, useState } from 'react'; 2import { listenForShareEvents, type ShareEvent } from 'tauri-plugin-sharetarget-api'; 3import { PluginListener } from '@tauri-apps/api/core'; 4import { readFile } from '@tauri-apps/plugin-fs'; 5 6function App() { 7 const [logs, setLogs] = useState(''); 8 const [file, setFile] = useState<File>(); 9 useEffect(() => { 10 let listener: PluginListener; 11 const setupListener = async () => { 12 listener = await listenForShareEvents(async (intent: ShareEvent) => { 13 if(event.stream) { 14 const contents = await readFile(intent.stream).catch((error: Error) => { 15 console.warn('fetching shared content failed:'); 16 throw error; 17 }); 18 setFile(new File([contents], intent.name, { type: intent.content_type })); 19 } else { 20 // This intent contains no binary bundle. 21 console.warn('unused share intent', intent.uri); 22 } 23 setLogs(intent.uri); 24 }); 25 }; 26 setupListener(); 27 return () => { listener?.unregister(); }; 28 }; 29 return (<> 30 <h3>Sharing { intent.name }</h3> 31 <Upload file={ file } /> 32 </>); 33}
Caveats
Unfortunately, multiple files in a single share intent are not supported right now. iOs is also unsupported - I don't have the platform. PRs welcome !
No vulnerabilities found.
No security vulnerabilities found.