1- import { app , BrowserWindow , clipboard , ipcMain , nativeImage , nativeTheme , screen , shell } from 'electron'
1+ import { app , BrowserWindow , clipboard , ClipboardItem , ipcMain , nativeImage , nativeTheme , screen , shell } from 'electron'
22import { execSync } from 'node:child_process'
33import { existsSync } from 'node:fs'
44import { resolve } from 'node:path'
@@ -191,24 +191,35 @@ ipcMain.handle(
191191)
192192
193193const getClipboardText = ( ) => {
194- return clipboard . readText ( 'clipboard' )
194+ return clipboard . readText ( )
195195}
196196
197197ipcMain . handle ( 'app:getClipboardText' , ( event ) => {
198198 return getClipboardText ( )
199199} )
200200
201201const setClipboardText = ( text : string ) => {
202- clipboard . writeText ( text , 'clipboard' )
202+ return clipboard . writeText ( text )
203203}
204204
205205ipcMain . handle ( 'app:setClipboardText' , ( event , text : string ) => {
206- setClipboardText ( text )
206+ return setClipboardText ( text )
207207} )
208208
209- const getClipboardImage = ( ) => {
210- const image = clipboard . readImage ( 'clipboard' )
211- return image . isEmpty ( ) ? '' : image . toDataURL ( )
209+ const getClipboardImage = async ( ) => {
210+ const items = await clipboard . read ( )
211+ for ( const item of items ) {
212+ const type = item . types . find ( ( t ) => t . startsWith ( 'image/' ) )
213+ if ( ! type ) {
214+ continue
215+ }
216+ const blob = ( await item . getType ( type ) ) as Blob
217+ const image = nativeImage . createFromBuffer ( Buffer . from ( await blob . arrayBuffer ( ) ) )
218+ if ( ! image . isEmpty ( ) ) {
219+ return image . toDataURL ( )
220+ }
221+ }
222+ return ''
212223}
213224
214225ipcMain . handle ( 'app:getClipboardImage' , ( event ) => {
@@ -217,11 +228,12 @@ ipcMain.handle('app:getClipboardImage', (event) => {
217228
218229const setClipboardImage = ( image : string ) => {
219230 const img = nativeImage . createFromDataURL ( image )
220- clipboard . writeImage ( img , 'clipboard' )
231+ const png = new Uint8Array ( img . toPNG ( ) )
232+ return clipboard . write ( [ new ClipboardItem ( { 'image/png' : new Blob ( [ png ] , { type : 'image/png' } ) } ) ] )
221233}
222234
223235ipcMain . handle ( 'app:setClipboardImage' , ( event , image : string ) => {
224- setClipboardImage ( image )
236+ return setClipboardImage ( image )
225237} )
226238
227239const isDarkMode = ( ) => {
0 commit comments