mirror of
https://github.com/ZiuChen/ClipboardManager.git
synced 2025-06-06 21:34:08 +08:00
29 lines
904 B
JavaScript
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(); |