fix: linux用户改用不同的监听机制 监听异常时抛出错误

This commit is contained in:
ZiuChen 2022-09-20 13:40:49 +08:00
parent 5af526ed3e
commit e57c0aff71
2 changed files with 30 additions and 7 deletions

View File

@ -1,7 +1,6 @@
const { chmodSync } = require('fs');
const { EventEmitter } = require('events'); const { EventEmitter } = require('events');
const path = require('path'); const path = require('path');
const { execFile } = require('child_process'); const { execFile, exec } = require('child_process');
class ClipboardEventListener extends EventEmitter { class ClipboardEventListener extends EventEmitter {
constructor() { constructor() {
@ -11,12 +10,11 @@ class ClipboardEventListener extends EventEmitter {
startListening() { startListening() {
const { platform } = process; const { platform } = process;
if (platform === 'win32') { if (platform === 'win32') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-win32.exe')); this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'));
} }
else if (platform === 'linux') { else if (platform === 'linux') {
const p = path.join(__dirname, 'platform/clipboard-event-handler-linux'); // manually install clipboard-event-handler-linux to /usr/local/bin
chmodSync(p, 0o111); // modify file permission: everyone can execute, or it will fail this.child = exec('clipboard-event-handler-linux')
this.child = execFile(p);
} }
else if (platform === 'darwin') { else if (platform === 'darwin') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac')); this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'));
@ -29,6 +27,12 @@ class ClipboardEventListener extends EventEmitter {
this.emit('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() { stopListening() {
const res = this.child.kill(); const res = this.child.kill();

View File

@ -192,7 +192,11 @@ export default function initPlugin() {
const toTop = () => (document.scrollingElement.scrollTop = 0) const toTop = () => (document.scrollingElement.scrollTop = 0)
const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click() const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click()
try {
listener.startListening() listener.startListening()
} catch (error) {
utools.showNotification(error)
}
listener.on('change', () => { listener.on('change', () => {
const item = pbpaste() const item = pbpaste()
@ -208,6 +212,21 @@ export default function initPlugin() {
db.addItem(item) db.addItem(item)
}) })
const info = '请手动安装 clipboard-event-handler-linux 到 /usr/bin'
const site =
'https://ziuchen.gitee.io/project/ClipboardManager/guide/#如何手动安装clipboard-event-handler-linux'
listener
.on('close', () => {
utools.showNotification('剪贴板监听异常关闭' + (utools.isLinux() ? info : ''))
utools.isLinux() ? utools.shellOpenExternal(site) : ''
utools.outPlugin()
})
.on('exit', () => {
utools.showNotification('剪贴板监听异常退出' + (utools.isLinux() ? info : ''))
utools.isLinux() ? utools.shellOpenExternal(site) : ''
utools.outPlugin()
})
utools.onPluginEnter(() => { utools.onPluginEnter(() => {
toTop() toTop()
resetNav() resetNav()