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 path = require('path');
const { execFile } = require('child_process');
const { execFile, exec } = require('child_process');
class ClipboardEventListener extends EventEmitter {
constructor() {
@ -11,12 +10,11 @@ class ClipboardEventListener extends EventEmitter {
startListening() {
const { platform } = process;
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') {
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(p);
// 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'));
@ -29,6 +27,12 @@ class ClipboardEventListener extends EventEmitter {
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();

View File

@ -192,7 +192,11 @@ export default function initPlugin() {
const toTop = () => (document.scrollingElement.scrollTop = 0)
const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click()
listener.startListening()
try {
listener.startListening()
} catch (error) {
utools.showNotification(error)
}
listener.on('change', () => {
const item = pbpaste()
@ -208,6 +212,21 @@ export default function initPlugin() {
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(() => {
toTop()
resetNav()