From ae27493c29391e0cf2e9c08d27ab9d466f0a9c93 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Mon, 2 Jan 2023 02:03:07 +0800 Subject: [PATCH] Optimize mouseover & click callback --- src/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index cef62d3..b1f45f6 100755 --- a/src/index.js +++ b/src/index.js @@ -66,7 +66,8 @@ function loadWidget(config) { // 检测用户活动状态,并在空闲时显示消息 let userAction = false, userActionTimer, - messageArray = result.message.default; + messageArray = result.message.default, + lastHoverElement; window.addEventListener("mousemove", () => userAction = true); window.addEventListener("keydown", () => userAction = true); setInterval(() => { @@ -83,7 +84,9 @@ function loadWidget(config) { showMessage(welcomeMessage(result.time), 7000, 11); window.addEventListener("mouseover", event => { for (let { selector, text } of result.mouseover) { - if (!event.target.matches(selector)) continue; + if (!event.target.closest(selector)) continue; + if (lastHoverElement === selector) return; + lastHoverElement = selector; text = randomSelection(text); text = text.replace("{text}", event.target.innerText); showMessage(text, 4000, 8); @@ -92,7 +95,7 @@ function loadWidget(config) { }); window.addEventListener("click", event => { for (let { selector, text } of result.click) { - if (!event.target.matches(selector)) continue; + if (!event.target.closest(selector)) continue; text = randomSelection(text); text = text.replace("{text}", event.target.innerText); showMessage(text, 4000, 8);