mirror of
https://github.com/ZiuChen/ClipboardManager.git
synced 2025-09-27 05:43:21 +08:00
refactor: 改善剪贴板监听性能 修复大图卡顿与CPU高占用的问题
This commit is contained in:
21
public/node_modules/clipboard-event/LICENSE
generated
vendored
Normal file
21
public/node_modules/clipboard-event/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
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.
|
29
public/node_modules/clipboard-event/index.js
generated
vendored
Normal file
29
public/node_modules/clipboard-event/index.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
const { EventEmitter } = require('events');
|
||||
const path = require('path');
|
||||
const { execFile } = require('child_process');
|
||||
|
||||
class ClipboardEventListener extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.child = null;
|
||||
}
|
||||
startListening() {
|
||||
const { platform } = process;
|
||||
const file = `platform/clipboard-event-handler-${platform}${platform === 'win32' ? '.exe' : ''}`
|
||||
if(platform !== 'win32' && platform !== 'darwin' && platform !== 'linux') {
|
||||
throw new Error(`ClipboardEventListener is not supported on ${platform}`);
|
||||
}
|
||||
this.child = execFile(path.join(__dirname, file));
|
||||
this.child.stdout.on('data', (data) => {
|
||||
if (data.trim() === 'CLIPBOARD_CHANGE') {
|
||||
this.emit('change');
|
||||
}
|
||||
});
|
||||
}
|
||||
stopListening() {
|
||||
const res = this.child.kill();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new ClipboardEventListener();
|
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-darwin
generated
vendored
Normal file
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-darwin
generated
vendored
Normal file
Binary file not shown.
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-linux
generated
vendored
Normal file
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-linux
generated
vendored
Normal file
Binary file not shown.
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-win32.exe
generated
vendored
Normal file
BIN
public/node_modules/clipboard-event/platform/clipboard-event-handler-win32.exe
generated
vendored
Normal file
Binary file not shown.
@@ -6,19 +6,13 @@
|
||||
|
||||
const fs = require('fs')
|
||||
const crypto = require('crypto')
|
||||
const listener = require('clipboard-event')
|
||||
const { clipboard } = require('electron')
|
||||
const time = require('./time')
|
||||
|
||||
const homePath = utools.getPath('home')
|
||||
const userDataPath = utools.getPath('userData')
|
||||
const dbName = '_utools_clipboard_manager_storage'
|
||||
|
||||
const isMacOs = utools.isMacOs()
|
||||
const isWindows = utools.isWindows()
|
||||
const sep = isWindows ? '\\' : '/'
|
||||
const DBPath = `${isMacOs ? userDataPath : homePath}${sep}${dbName}`
|
||||
|
||||
let globalImageOversize = false
|
||||
const sep = utools.isWindows() ? '\\' : '/'
|
||||
const DBPath = `${
|
||||
utools.isMacOs() ? utools.getPath('userData') : utools.getPath('home')
|
||||
}${sep}_utools_clipboard_manager_storage`
|
||||
|
||||
class DB {
|
||||
constructor(path) {
|
||||
@@ -133,7 +127,6 @@ const pbpaste = () => {
|
||||
// image
|
||||
const image = clipboard.readImage() // 大图卡顿来源
|
||||
const data = image.toDataURL()
|
||||
globalImageOversize = data.length > 3e5
|
||||
if (!image.isEmpty()) {
|
||||
return {
|
||||
type: 'image',
|
||||
@@ -142,24 +135,6 @@ const pbpaste = () => {
|
||||
}
|
||||
}
|
||||
|
||||
const watchClipboard = async (db, fn) => {
|
||||
let prev = db.dataBase.data[0] || {}
|
||||
function loop() {
|
||||
time.sleep(250).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
|
||||
fn(item)
|
||||
} else {
|
||||
// 剪切板元素 与上次复制内容相同
|
||||
}
|
||||
}
|
||||
loop()
|
||||
}
|
||||
|
||||
const copy = (item, isHideMainWindow = true) => {
|
||||
switch (item.type) {
|
||||
case 'text':
|
||||
@@ -221,9 +196,12 @@ const focus = (isBlur = false) => {
|
||||
const toTop = () => (document.scrollingElement.scrollTop = 0)
|
||||
const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click()
|
||||
|
||||
watchClipboard(db, (item) => {
|
||||
// 此函数不断执行
|
||||
listener.startListening()
|
||||
|
||||
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
|
||||
@@ -235,10 +213,6 @@ watchClipboard(db, (item) => {
|
||||
})
|
||||
|
||||
utools.onPluginEnter(() => {
|
||||
if (globalImageOversize) {
|
||||
utools.copyText('ImageOverSized')
|
||||
globalImageOversize = false
|
||||
}
|
||||
toTop()
|
||||
resetNav()
|
||||
})
|
||||
|
@@ -1,2 +0,0 @@
|
||||
// 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;
|
@@ -1,2 +0,0 @@
|
||||
// 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}))};
|
Reference in New Issue
Block a user