mirror of
https://github.com/ZiuChen/ClipboardManager.git
synced 2025-06-06 21:34:08 +08:00
fix: 区分Mac平台 采用旧的剪贴板监听方式
This commit is contained in:
parent
d5332e69af
commit
b4dc9f803b
2
public/node_modules/clipboard-event/index.js
generated
vendored
2
public/node_modules/clipboard-event/index.js
generated
vendored
@ -34,8 +34,6 @@ class ClipboardEventListener extends EventEmitter {
|
||||
this.emit('error', error)
|
||||
}
|
||||
this.child = execFile(target)
|
||||
} else if (platform === 'darwin') {
|
||||
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'))
|
||||
} else {
|
||||
throw 'Not yet supported'
|
||||
}
|
||||
|
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-mac
generated
vendored
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-mac
generated
vendored
Binary file not shown.
@ -2,6 +2,7 @@ const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs')
|
||||
const crypto = require('crypto')
|
||||
const listener = require('clipboard-event')
|
||||
const { clipboard } = require('electron')
|
||||
const time = require('./time')
|
||||
|
||||
window.exports = {
|
||||
utools,
|
||||
@ -11,5 +12,6 @@ window.exports = {
|
||||
mkdirSync,
|
||||
crypto,
|
||||
listener,
|
||||
clipboard
|
||||
clipboard,
|
||||
time
|
||||
}
|
||||
|
2
public/time.js
Normal file
2
public/time.js
Normal file
@ -0,0 +1,2 @@
|
||||
// time.js author: inu1255
|
||||
const path=require("path");function newPromise(fn){let a,b;var tmp={resolve(x){if(this.pending){a(x);this.resolved=true;this.pending=false}},reject(e){if(this.pending){b(e);this.rejectd=true;this.pending=false}},pending:true,resolved:false,rejected:false};var pms=new Promise(function(resolve,reject){a=resolve;b=reject;if(fn)fn(tmp.resolve,tmp.reject)});return Object.assign(pms,tmp)}let cbIdx=1;const cbMap=new Map;function getWorker(){if(getWorker.worker)return getWorker.worker;const worker=new Worker(path.join(__dirname,"time.worker.js"));getWorker.worker=worker;worker.onmessage=e=>{if(e.data&&cbMap.has(e.data.cb)){cbMap.get(e.data.cb).apply(null,e.data.args)}};return worker}function call(method,args){const cb=cbIdx++;let pms=newPromise();cbMap.set(cb,function(err,data){if(err)pms.reject(err);else pms.resolve(data)});getWorker().postMessage({method:method,args:args,cb:cb});return pms}function sleep(ms){return call("sleep",[ms])}exports.sleep=sleep;
|
2
public/time.worker.js
Normal file
2
public/time.worker.js
Normal file
@ -0,0 +1,2 @@
|
||||
// time.worker.js author: inu1255
|
||||
const apis={sleep(ms){return new Promise(resolve=>setTimeout(resolve,ms))}};onmessage=event=>{const data=event.data;if(!data)return;const{cb,method,args}=data;if(!apis[method]){postMessage({cb:cb,err:"no such method"});return}apis[method].apply(null,args).then(res=>postMessage({cb:cb,data:res}),err=>postMessage({cb:cb,err:err}))};
|
@ -1,5 +1,14 @@
|
||||
const { utools, existsSync, readFileSync, writeFileSync, mkdirSync, crypto, listener, clipboard } =
|
||||
window.exports
|
||||
const {
|
||||
utools,
|
||||
existsSync,
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
mkdirSync,
|
||||
crypto,
|
||||
listener,
|
||||
clipboard,
|
||||
time
|
||||
} = window.exports
|
||||
import setting from './readSetting'
|
||||
|
||||
export default function initPlugin() {
|
||||
@ -192,6 +201,19 @@ export default function initPlugin() {
|
||||
const toTop = () => (document.scrollingElement.scrollTop = 0)
|
||||
const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click()
|
||||
|
||||
const handleClipboardChange = (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 registerClipEvent = (listener) => {
|
||||
const exitHandler = () => {
|
||||
utools.showNotification('剪贴板监听异常退出 请重启插件以开启监听')
|
||||
@ -205,30 +227,37 @@ export default function initPlugin() {
|
||||
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('change', handleClipboardChange)
|
||||
.on('close', exitHandler)
|
||||
.on('exit', exitHandler)
|
||||
.on('error', (error) => errorHandler(error))
|
||||
}
|
||||
|
||||
// 首次启动插件 即开启监听
|
||||
registerClipEvent(listener)
|
||||
listener.startListening()
|
||||
if (!utools.isMacOs()) {
|
||||
// 首次启动插件 即开启监听
|
||||
registerClipEvent(listener)
|
||||
listener.startListening()
|
||||
} else {
|
||||
// macos 由于无法执行 clipboard-event-handler-mac 所以使用旧方法
|
||||
let prev = db.dataBase.data[0] || {}
|
||||
function loop() {
|
||||
time.sleep(300).then(loop)
|
||||
const item = pbpaste()
|
||||
if (!item) return
|
||||
item.id = crypto.createHash('md5').update(item.data).digest('hex')
|
||||
if (item && prev.id != item.id) {
|
||||
// 剪切板元素 与最近一次复制内容不同
|
||||
prev = item
|
||||
handleClipboardChange(item)
|
||||
} else {
|
||||
// 剪切板元素 与上次复制内容相同
|
||||
}
|
||||
}
|
||||
loop()
|
||||
}
|
||||
|
||||
utools.onPluginEnter(() => {
|
||||
if (!listener.listening) {
|
||||
if (!listener.listening && !utools.isMacOs()) {
|
||||
// 进入插件后 如果监听已关闭 则重新开启监听
|
||||
registerClipEvent(listener)
|
||||
listener.startListening()
|
||||
@ -238,7 +267,7 @@ export default function initPlugin() {
|
||||
})
|
||||
|
||||
utools.onPluginOut((processExit) => {
|
||||
if (processExit) {
|
||||
if (processExit && !utools.isMacOs()) {
|
||||
utools.showNotification('剪贴板监听异常退出 请重启插件以开启监听')
|
||||
listener.stopListening()
|
||||
}
|
||||
|
@ -226,10 +226,25 @@ onMounted(() => {
|
||||
updateShowList(activeTab.value)
|
||||
|
||||
// 定期检查更新
|
||||
window.listener.on('change', () => {
|
||||
list.value = window.db.dataBase.data
|
||||
updateShowList(activeTab.value)
|
||||
})
|
||||
if (!utools.isMacOs) {
|
||||
window.listener.on('change', () => {
|
||||
list.value = window.db.dataBase.data
|
||||
updateShowList(activeTab.value)
|
||||
})
|
||||
} else {
|
||||
// macos
|
||||
let prev = {}
|
||||
setInterval(() => {
|
||||
const now = window.db.dataBase.data[0]
|
||||
if (prev?.id === now?.id) {
|
||||
} else {
|
||||
// 有更新
|
||||
list.value = window.db.dataBase.data
|
||||
updateShowList(activeTab.value)
|
||||
prev = now
|
||||
}
|
||||
}, 800)
|
||||
}
|
||||
|
||||
// 监听搜索框
|
||||
watch(filterText, (val) => updateShowList(activeTab.value))
|
||||
|
Loading…
x
Reference in New Issue
Block a user