@@ -1814,6 +1814,83 @@ const WebviewPrint = registerPlugin("WebviewPrint", {
1814
1814
const Preferences = registerPlugin ( "Preferences" , {
1815
1815
web : ( ) => __vitePreload ( ( ) => import ( "./web2.js" ) , true ? [ ] : void 0 ) . then ( ( m ) => new m . PreferencesWeb ( ) )
1816
1816
} ) ;
1817
+ class ClipboardWeb extends WebPlugin {
1818
+ async write ( options ) {
1819
+ if ( typeof navigator === "undefined" || ! navigator . clipboard ) {
1820
+ throw this . unavailable ( "Clipboard API not available in this browser" ) ;
1821
+ }
1822
+ if ( options . string !== void 0 ) {
1823
+ await this . writeText ( options . string ) ;
1824
+ } else if ( options . url ) {
1825
+ await this . writeText ( options . url ) ;
1826
+ } else if ( options . image ) {
1827
+ if ( typeof ClipboardItem !== "undefined" ) {
1828
+ try {
1829
+ const blob = await ( await fetch ( options . image ) ) . blob ( ) ;
1830
+ const clipboardItemInput = new ClipboardItem ( { [ blob . type ] : blob } ) ;
1831
+ await navigator . clipboard . write ( [ clipboardItemInput ] ) ;
1832
+ } catch ( err ) {
1833
+ throw new Error ( "Failed to write image" ) ;
1834
+ }
1835
+ } else {
1836
+ throw this . unavailable ( "Writing images to the clipboard is not supported in this browser" ) ;
1837
+ }
1838
+ } else {
1839
+ throw new Error ( "Nothing to write" ) ;
1840
+ }
1841
+ }
1842
+ async read ( ) {
1843
+ if ( typeof navigator === "undefined" || ! navigator . clipboard ) {
1844
+ throw this . unavailable ( "Clipboard API not available in this browser" ) ;
1845
+ }
1846
+ if ( typeof ClipboardItem !== "undefined" ) {
1847
+ try {
1848
+ const clipboardItems = await navigator . clipboard . read ( ) ;
1849
+ const type = clipboardItems [ 0 ] . types [ 0 ] ;
1850
+ const clipboardBlob = await clipboardItems [ 0 ] . getType ( type ) ;
1851
+ const data = await this . _getBlobData ( clipboardBlob , type ) ;
1852
+ return { value : data , type } ;
1853
+ } catch ( err ) {
1854
+ return this . readText ( ) ;
1855
+ }
1856
+ } else {
1857
+ return this . readText ( ) ;
1858
+ }
1859
+ }
1860
+ async readText ( ) {
1861
+ if ( typeof navigator === "undefined" || ! navigator . clipboard || ! navigator . clipboard . readText ) {
1862
+ throw this . unavailable ( "Reading from clipboard not supported in this browser" ) ;
1863
+ }
1864
+ const text = await navigator . clipboard . readText ( ) ;
1865
+ return { value : text , type : "text/plain" } ;
1866
+ }
1867
+ async writeText ( text ) {
1868
+ if ( typeof navigator === "undefined" || ! navigator . clipboard || ! navigator . clipboard . writeText ) {
1869
+ throw this . unavailable ( "Writting to clipboard not supported in this browser" ) ;
1870
+ }
1871
+ await navigator . clipboard . writeText ( text ) ;
1872
+ }
1873
+ _getBlobData ( clipboardBlob , type ) {
1874
+ return new Promise ( ( resolve , reject ) => {
1875
+ const reader = new FileReader ( ) ;
1876
+ if ( type . includes ( "image" ) ) {
1877
+ reader . readAsDataURL ( clipboardBlob ) ;
1878
+ } else {
1879
+ reader . readAsText ( clipboardBlob ) ;
1880
+ }
1881
+ reader . onloadend = ( ) => {
1882
+ const r = reader . result ;
1883
+ resolve ( r ) ;
1884
+ } ;
1885
+ reader . onerror = ( e ) => {
1886
+ reject ( e ) ;
1887
+ } ;
1888
+ } ) ;
1889
+ }
1890
+ }
1891
+ const Clipboard = registerPlugin ( "Clipboard" , {
1892
+ web : ( ) => new ClipboardWeb ( )
1893
+ } ) ;
1817
1894
const Browser = registerPlugin ( "Browser" , {
1818
1895
web : ( ) => __vitePreload ( ( ) => import ( "./web3.js" ) , true ? [ ] : void 0 ) . then ( ( m ) => new m . BrowserWeb ( ) )
1819
1896
} ) ;
@@ -1833,6 +1910,10 @@ async function selectStorage(key) {
1833
1910
async function insertStorage ( key , value ) {
1834
1911
return await Preferences . set ( { key, value } ) ;
1835
1912
}
1913
+ async function selectClipboard ( ) {
1914
+ const { value } = await Clipboard . read ( ) ;
1915
+ return value ;
1916
+ }
1836
1917
async function openBrowserPage ( url ) {
1837
1918
try {
1838
1919
return await Browser . open ( { url, windowName : "_blank" } ) ;
@@ -1855,6 +1936,7 @@ defineCustomElements();
1855
1936
globalThis . printCurrentPage = printCurrentPage ;
1856
1937
globalThis . selectStorage = selectStorage ;
1857
1938
globalThis . insertStorage = insertStorage ;
1939
+ globalThis . selectClipboard = selectClipboard ;
1858
1940
globalThis . openBrowserPage = openBrowserPage ;
1859
1941
globalThis . shareText = shareText ;
1860
1942
globalThis . popupText = popupText ;
0 commit comments