39 lines
1.2 KiB
JavaScript

const { chmodSync } = require('fs');
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;
if (platform === 'win32') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-win32.exe'));
}
else if (platform === 'linux') {
const p = path.join(__dirname, 'platform/clipboard-event-handler-linux');
chmodSync(p, 0o111); // modify file permission: everyone can execute, or it will fail
this.child = execFile();
}
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');
}
});
}
stopListening() {
const res = this.child.kill();
return res;
}
}
module.exports = new ClipboardEventListener();