From 7cc7f9c763f4411f1b82a997be81313ce28d83a3 Mon Sep 17 00:00:00 2001 From: ZiuChen <457353192@qq.com> Date: Sun, 4 Sep 2022 19:36:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E8=BF=9B=E5=85=A5=E6=8F=92=E4=BB=B6=E7=9A=84=E5=8D=A1?= =?UTF-8?q?=E9=80=80=E9=97=AE=E9=A2=98=20=E6=94=B9=E5=96=84=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/preload.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/public/preload.js b/public/preload.js index 11b1f11..3fcc8fc 100644 --- a/public/preload.js +++ b/public/preload.js @@ -17,6 +17,7 @@ const isWindows = utools.isWindows() const DBPath = `${isMacOs ? userDataPath : homePath}${isWindows ? '\\' : '/'}${dbName}` let globalImageOversize = false +let globalTimmerSet = false class DB { constructor(path) { @@ -146,7 +147,7 @@ const pbpaste = async () => { const watchClipboard = async (db, fn) => { let prev = db.dataBase.data[0] || {} - setInterval(() => { + return setInterval(() => { pbpaste().then((item) => { item.id = crypto.createHash('md5').update(item.data).digest('hex') if (item && prev.id != item.id) { @@ -190,7 +191,7 @@ const focus = () => document.querySelector('.clip-search input')?.focus() const toTop = () => (document.scrollingElement.scrollTop = 0) const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click() -watchClipboard(db, (item) => { +let timmer = watchClipboard(db, (item) => { // 此函数不断执行 if (!item) return if (db.updateItemViaId(item.id)) { @@ -203,17 +204,39 @@ watchClipboard(db, (item) => { db.addItem(item) }) +globalTimmerSet = true // 计时器成功添加 + utools.onPluginEnter(() => { if (globalImageOversize) { utools.copyText('ImageOverSized') globalImageOversize = false } + if (!globalTimmerSet) { + // 定时器被清除了 重新添加计时器 + // same to code above + timmer = watchClipboard(db, (item) => { + if (!item) return + if (db.updateItemViaId(item.id)) { + return + } + item.createTime = new Date().getTime() + item.updateTime = new Date().getTime() + db.addItem(item) + }) + } document.querySelector('.clip-search input').select() // 进入插件将搜索框内容全选 focus() toTop() resetNav() }) +utools.onPluginOut((processExit) => { + // 卡顿来源: 似乎插件每次启动 uTools不会清除插件设置的 interval定时器 + // 插件重复进入/退出会产生多个计时器导致插件卡退 + // 插件退出 清除计时器 插件隐藏后台 不清除 + processExit ? (clearInterval(timmer), (globalTimmerSet = false)) : (globalTimmerSet = true) +}) + window.db = db window.copy = copy window.paste = paste