feat: 不再内置二进制文件 让用户自行选择监听剪贴板方式

This commit is contained in:
ZiuChen
2022-09-24 17:01:12 +08:00
parent a05e9c4742
commit 73be7b47df
8 changed files with 91 additions and 112 deletions

56
public/listener.js Normal file
View File

@@ -0,0 +1,56 @@
const { chmodSync, existsSync } = require('fs')
const { EventEmitter } = require('events')
const path = require('path')
const { execFile } = require('child_process')
class ClipboardEventListener extends EventEmitter {
constructor() {
super()
this.child = null
this.listening = false
}
startListening(dbPath) {
const targetMap = {
win32: 'clipboard-event-handler-win32.exe',
linux: 'clipboard-event-handler-linux'
}
const { platform } = process
const target = path.resolve(
dbPath.split('_utools_clipboard_manager_storage')[0],
targetMap[platform]
)
if (!existsSync(target)) {
this.emit('error', '剪贴板监听程序不存在')
return
}
if (platform === 'win32') {
this.child = execFile(target)
} else if (platform === 'linux') {
chmodSync(target, 0o755)
this.child = execFile(target)
} else {
throw 'Not yet supported'
}
this.child.stdout.on('data', (data) => {
if (data.trim() === 'CLIPBOARD_CHANGE') {
this.emit('change')
}
})
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
}
}
module.exports = new ClipboardEventListener()

View File

@@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 Sudhakar R
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,62 +0,0 @@
const { chmodSync, existsSync, mkdirSync, copyFileSync } = require('fs')
const { EventEmitter } = require('events')
const path = require('path')
const { execFile } = require('child_process')
const homeDir = require('os').homedir()
class ClipboardEventListener extends EventEmitter {
constructor() {
super()
this.child = null
this.listening = false
}
startListening() {
const { platform } = process
if (platform === 'win32') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-win32.exe'))
} 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 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')
try {
if (!existsSync(targetPath)) {
// bin dir doesnt exist, create it
mkdirSync(targetPath)
}
if (!existsSync(target)) {
// copy the file
copyFileSync(p, target)
chmodSync(target, 0o755)
}
} catch (error) {
this.emit('error', error)
}
this.child = execFile(target)
} else {
throw 'Not yet supported'
}
this.child.stdout.on('data', (data) => {
if (data.trim() === 'CLIPBOARD_CHANGE') {
this.emit('change')
}
})
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
}
}
module.exports = new ClipboardEventListener()

View File

@@ -1,6 +1,6 @@
const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs')
const crypto = require('crypto')
const listener = require('clipboard-event')
const listener = require('./listener')
const { clipboard } = require('electron')
const time = require('./time')