diff --git a/public/node_modules/clipboard-event/index.js b/public/node_modules/clipboard-event/index.js index 2439737..86e615d 100644 --- a/public/node_modules/clipboard-event/index.js +++ b/public/node_modules/clipboard-event/index.js @@ -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' } diff --git a/public/node_modules/clipboard-event/platform/clipboard-event-handler-mac b/public/node_modules/clipboard-event/platform/clipboard-event-handler-mac deleted file mode 100644 index d0eee8e..0000000 Binary files a/public/node_modules/clipboard-event/platform/clipboard-event-handler-mac and /dev/null differ diff --git a/public/preload.js b/public/preload.js index 281993e..d9052e7 100644 --- a/public/preload.js +++ b/public/preload.js @@ -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 } diff --git a/public/time.js b/public/time.js new file mode 100644 index 0000000..d25c03a --- /dev/null +++ b/public/time.js @@ -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; \ No newline at end of file diff --git a/public/time.worker.js b/public/time.worker.js new file mode 100644 index 0000000..97b632d --- /dev/null +++ b/public/time.worker.js @@ -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}))}; \ No newline at end of file diff --git a/src/global/initPlugin.js b/src/global/initPlugin.js index 3c2e9ce..ed042f5 100644 --- a/src/global/initPlugin.js +++ b/src/global/initPlugin.js @@ -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() } diff --git a/src/views/Main.vue b/src/views/Main.vue index 1f0aee1..26e126b 100644 --- a/src/views/Main.vue +++ b/src/views/Main.vue @@ -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))