mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-03 11:38:13 -05:00
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { clipboard, nativeImage } from 'electron'
|
|
import { EventEmitter } from 'events'
|
|
|
|
class ClipboardWatcher extends EventEmitter {
|
|
lastImage: Electron.NativeImage | null
|
|
timer: NodeJS.Timeout | null
|
|
|
|
constructor () {
|
|
super()
|
|
this.lastImage = null
|
|
this.timer = null
|
|
}
|
|
|
|
startListening (watchDelay = 500) {
|
|
this.stopListening()
|
|
|
|
const image = clipboard.readImage()
|
|
if (!image.isEmpty()) {
|
|
const dataUrl = image.toDataURL()
|
|
this.lastImage = nativeImage.createFromDataURL(dataUrl)
|
|
}
|
|
|
|
this.timer = setInterval(() => {
|
|
const image = clipboard.readImage()
|
|
if (image.isEmpty()) {
|
|
return
|
|
}
|
|
|
|
const dataUrl = image.toDataURL()
|
|
const currentImage = nativeImage.createFromDataURL(dataUrl)
|
|
|
|
if (this.lastImage) {
|
|
const lastDataUrl = this.lastImage.toDataURL()
|
|
if (lastDataUrl === dataUrl) {
|
|
return
|
|
}
|
|
}
|
|
|
|
this.lastImage = currentImage
|
|
this.emit('change', currentImage)
|
|
}, watchDelay)
|
|
}
|
|
|
|
stopListening () {
|
|
if (this.timer) {
|
|
clearInterval(this.timer)
|
|
this.timer = null
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new ClipboardWatcher()
|