Files
Clipboard/lib/poll-guard.js
2026-09-12 13:59:12 +08:00

26 lines
953 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
/**
* 轮询守卫:决定本 tick 是否进入图片分支。
*
* 背景:默认 pollClipboard 每 tick 都跑 `clipboard.readImage().getBitmap()`
* + sha1剪贴板上有 4K 截图时约 33 MB memcpy + ~40ms。先用 availableFormats()
* 探一下,文本剪贴板直接跳过图片分支,零开销。
*
* 已知残留:当剪贴板确实有大图时仍然每 tick 重哈希。彻底解决需要启发式
* 风险(尺寸相同可能漏抓变更)或订阅 clipboard-sequence-numberElectron
* 不暴露)。本次只做最稳妥的格式守卫。
*
* @param {unknown} formats 来自 clipboard.availableFormats() 的列表
* @returns {boolean} 是否需要读图片
*/
function shouldReadImage(formats) {
if (!Array.isArray(formats)) return false;
for (const f of formats) {
if (typeof f === 'string' && f.startsWith('image/')) return true;
}
return false;
}
module.exports = { shouldReadImage };