Optimize mouseover & click callback

This commit is contained in:
Mimi 2023-01-02 02:03:07 +08:00
parent ff687ea3de
commit ae27493c29

View File

@ -66,7 +66,8 @@ function loadWidget(config) {
// 检测用户活动状态,并在空闲时显示消息 // 检测用户活动状态,并在空闲时显示消息
let userAction = false, let userAction = false,
userActionTimer, userActionTimer,
messageArray = result.message.default; messageArray = result.message.default,
lastHoverElement;
window.addEventListener("mousemove", () => userAction = true); window.addEventListener("mousemove", () => userAction = true);
window.addEventListener("keydown", () => userAction = true); window.addEventListener("keydown", () => userAction = true);
setInterval(() => { setInterval(() => {
@ -83,7 +84,9 @@ function loadWidget(config) {
showMessage(welcomeMessage(result.time), 7000, 11); showMessage(welcomeMessage(result.time), 7000, 11);
window.addEventListener("mouseover", event => { window.addEventListener("mouseover", event => {
for (let { selector, text } of result.mouseover) { 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 = randomSelection(text);
text = text.replace("{text}", event.target.innerText); text = text.replace("{text}", event.target.innerText);
showMessage(text, 4000, 8); showMessage(text, 4000, 8);
@ -92,7 +95,7 @@ function loadWidget(config) {
}); });
window.addEventListener("click", event => { window.addEventListener("click", event => {
for (let { selector, text } of result.click) { for (let { selector, text } of result.click) {
if (!event.target.matches(selector)) continue; if (!event.target.closest(selector)) continue;
text = randomSelection(text); text = randomSelection(text);
text = text.replace("{text}", event.target.innerText); text = text.replace("{text}", event.target.innerText);
showMessage(text, 4000, 8); showMessage(text, 4000, 8);