PicList/src/main/utils/clipboardPoll.ts
2023-04-10 19:50:36 +08:00

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()