feat: 添加监听状态标志 进入插件重启监听 仅在首次运行时执行copy

This commit is contained in:
ZiuChen 2022-09-20 19:22:21 +08:00
parent 4dd5888a7f
commit f3c7d3899b
2 changed files with 54 additions and 35 deletions

View File

@ -8,6 +8,7 @@ class ClipboardEventListener extends EventEmitter {
constructor() { constructor() {
super(); super();
this.child = null; this.child = null;
this.listening = false;
} }
startListening() { startListening() {
const { platform } = process; const { platform } = process;
@ -17,14 +18,15 @@ class ClipboardEventListener extends EventEmitter {
else if (platform === 'linux') { else if (platform === 'linux') {
// linux: cant execFile without chmod, and cant chmod in app.asar // linux: cant execFile without chmod, and cant chmod in app.asar
// so we need to copy the file to /usr/bin // 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') const p = path.join(__dirname, 'platform/clipboard-event-handler-linux')
if(!existsSync(target)) { if(!existsSync(target)) {
mkdirSync(target) mkdirSync(targetPath)
copyFileSync(p, target)
chmodSync(target, 0o755)
} }
copyFileSync(p, path.join(target, 'clipboard-event-handler-linux')) this.child = execFile(target);
chmodSync(path.join(target, 'clipboard-event-handler-linux'), 0o755)
this.child = execFile(path.join(target, 'clipboard-event-handler-linux'));
} }
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'));
@ -39,13 +41,17 @@ class ClipboardEventListener extends EventEmitter {
}); });
this.child.stdout.on('close', () => { this.child.stdout.on('close', () => {
this.emit('close'); this.emit('close');
this.listening = false;
}); });
this.child.stdout.on('exit', () => { this.child.stdout.on('exit', () => {
this.emit('exit'); this.emit('exit');
this.listening = false;
}); });
this.listening = true;
} }
stopListening() { stopListening() {
const res = this.child.kill(); const res = this.child.kill();
this.listening = false;
return res; return res;
} }
} }

View File

@ -192,13 +192,16 @@ 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 { const registerClipEvent = (listener) => {
listener.startListening() const errorHandler = () => {
} catch (error) { const info = '如监听失效 请手动安装 clipboard-event-handler-linux 到 ~/.local/bin'
utools.showNotification(error) utools.showNotification(
'剪贴板监听异常退出 请重启插件以开启监听' + (utools.isLinux() ? info : '')
)
utools.outPlugin()
} }
listener
listener.on('change', () => { .on('change', () => {
const item = pbpaste() const item = pbpaste()
if (!item) return if (!item) return
item.id = crypto.createHash('md5').update(item.data).digest('hex') item.id = crypto.createHash('md5').update(item.data).digest('hex')
@ -211,28 +214,38 @@ export default function initPlugin() {
item.updateTime = new Date().getTime() item.updateTime = new Date().getTime()
db.addItem(item) db.addItem(item)
}) })
.on('close', errorHandler)
const callBack = () => { .on('exit', errorHandler)
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) => { .on('error', (error) => {
utools.showNotification('剪贴板监听出错' + error) utools.showNotification('剪贴板监听出错' + error)
utools.outPlugin() utools.outPlugin()
}) })
}
try {
// 首次启动插件 即开启监听
listener.startListening()
registerClipEvent(listener)
} catch (error) {
utools.showNotification(error)
}
utools.onPluginEnter(() => { utools.onPluginEnter(() => {
if (!listener.listening) {
// 进入插件后 如果监听已关闭 则重新开启监听
listener.startListening()
registerClipEvent(listener)
}
toTop() toTop()
resetNav() resetNav()
}) })
utools.onPluginOut((processExit) => {
if (processExit) {
listener.stopListening()
}
})
window.db = db window.db = db
window.copy = copy window.copy = copy
window.paste = paste window.paste = paste