29 lines
904 B
JavaScript

const { EventEmitter } = require('events');
const path = require('path');
const { execFile } = require('child_process');
class ClipboardEventListener extends EventEmitter {
constructor() {
super();
this.child = null;
}
startListening() {
const { platform } = process;
const file = `platform/clipboard-event-handler-${platform}${platform === 'win32' ? '.exe' : ''}`
if(platform !== 'win32' && platform !== 'darwin' && platform !== 'linux') {
throw new Error(`ClipboardEventListener is not supported on ${platform}`);
}
this.child = execFile(path.join(__dirname, file));
this.child.stdout.on('data', (data) => {
if (data.trim() === 'CLIPBOARD_CHANGE') {
this.emit('change');
}
});
}
stopListening() {
const res = this.child.kill();
return res;
}
}
module.exports = new ClipboardEventListener();