Skip to content

Commit

Permalink
Optimize mouseover & click callback
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Jan 1, 2023
1 parent ff687ea commit ae27493
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit ae27493

Please sign in to comment.