diff --git a/public/node_modules/clipboard-event/index.js b/public/node_modules/clipboard-event/index.js index d4d8745..91b290d 100644 --- a/public/node_modules/clipboard-event/index.js +++ b/public/node_modules/clipboard-event/index.js @@ -8,6 +8,7 @@ class ClipboardEventListener extends EventEmitter { constructor() { super(); this.child = null; + this.listening = false; } startListening() { const { platform } = process; @@ -17,14 +18,15 @@ class ClipboardEventListener extends EventEmitter { else if (platform === 'linux') { // linux: cant execFile without chmod, and cant chmod in app.asar // so we need to copy the file to /usr/bin - const target = path.resolve(homeDir, '.local', 'bin') + const targetPath = path.resolve(homeDir, '.local', 'bin') + const target = path.resolve(targetPath, 'clipboard-event-handler-linux') const p = path.join(__dirname, 'platform/clipboard-event-handler-linux') if(!existsSync(target)) { - mkdirSync(target) + mkdirSync(targetPath) + copyFileSync(p, target) + chmodSync(target, 0o755) } - copyFileSync(p, path.join(target, 'clipboard-event-handler-linux')) - chmodSync(path.join(target, 'clipboard-event-handler-linux'), 0o755) - this.child = execFile(path.join(target, 'clipboard-event-handler-linux')); + this.child = execFile(target); } else if (platform === 'darwin') { this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac')); @@ -39,13 +41,17 @@ class ClipboardEventListener extends EventEmitter { }); this.child.stdout.on('close', () => { this.emit('close'); + this.listening = false; }); this.child.stdout.on('exit', () => { this.emit('exit'); + this.listening = false; }); + this.listening = true; } stopListening() { const res = this.child.kill(); + this.listening = false; return res; } } diff --git a/src/global/initPlugin.js b/src/global/initPlugin.js index 8907762..05ced4b 100644 --- a/src/global/initPlugin.js +++ b/src/global/initPlugin.js @@ -192,47 +192,60 @@ export default function initPlugin() { const toTop = () => (document.scrollingElement.scrollTop = 0) const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click() + const registerClipEvent = (listener) => { + const errorHandler = () => { + const info = '如监听失效 请手动安装 clipboard-event-handler-linux 到 ~/.local/bin' + utools.showNotification( + '剪贴板监听异常退出 请重启插件以开启监听' + (utools.isLinux() ? info : '') + ) + utools.outPlugin() + } + listener + .on('change', () => { + const item = pbpaste() + if (!item) return + item.id = crypto.createHash('md5').update(item.data).digest('hex') + if (db.updateItemViaId(item.id)) { + // 在库中 由 updateItemViaId 更新 updateTime + return + } + // 不在库中 由 addItem 添加 + item.createTime = new Date().getTime() + item.updateTime = new Date().getTime() + db.addItem(item) + }) + .on('close', errorHandler) + .on('exit', errorHandler) + .on('error', (error) => { + utools.showNotification('剪贴板监听出错' + error) + utools.outPlugin() + }) + } + try { + // 首次启动插件 即开启监听 listener.startListening() + registerClipEvent(listener) } catch (error) { utools.showNotification(error) } - listener.on('change', () => { - const item = pbpaste() - if (!item) return - item.id = crypto.createHash('md5').update(item.data).digest('hex') - if (db.updateItemViaId(item.id)) { - // 在库中 由 updateItemViaId 更新 updateTime - return - } - // 不在库中 由 addItem 添加 - item.createTime = new Date().getTime() - item.updateTime = new Date().getTime() - db.addItem(item) - }) - - const callBack = () => { - const info = '请手动安装 clipboard-event-handler-linux 到 /usr/bin' - const site = - 'https://ziuchen.gitee.io/project/ClipboardManager/guide/#如何手动安装clipboard-event-handler-linux' - utools.showNotification('剪贴板监听退出' + (utools.isLinux() ? info : '')) - utools.isLinux() ? utools.shellOpenExternal(site) : '' - utools.outPlugin() - } - listener - .on('close', callBack) - .on('exit', callBack) - .on('error', (error) => { - utools.showNotification('剪贴板监听出错' + error) - utools.outPlugin() - }) - utools.onPluginEnter(() => { + if (!listener.listening) { + // 进入插件后 如果监听已关闭 则重新开启监听 + listener.startListening() + registerClipEvent(listener) + } toTop() resetNav() }) + utools.onPluginOut((processExit) => { + if (processExit) { + listener.stopListening() + } + }) + window.db = db window.copy = copy window.paste = paste