43 lines
1.2 KiB
JavaScript

const { EventEmitter } = require('events');
const path = require('path');
const { execFile, exec } = require('child_process');
class ClipboardEventListener extends EventEmitter {
constructor() {
super();
this.child = null;
}
startListening() {
const { platform } = process;
if (platform === 'win32') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'));
}
else if (platform === 'linux') {
// manually install clipboard-event-handler-linux to /usr/local/bin
this.child = exec('clipboard-event-handler-linux')
}
else if (platform === 'darwin') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'));
}
else {
throw 'Not yet supported';
}
this.child.stdout.on('data', (data) => {
if (data.trim() === 'CLIPBOARD_CHANGE') {
this.emit('change');
}
});
this.child.stdout.on('close', (code) => {
this.emit('close', code);
});
this.child.stdout.on('exit', (code) => {
this.emit('exit', code);
});
}
stopListening() {
const res = this.child.kill();
return res;
}
}
module.exports = new ClipboardEventListener();