Greasy Fork is available in English.

visibilityChangeListener

Waits until the tab is focused, executing a callback function when it happens.

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greasyfork.org/scripts/480373/1283462/visibilityChangeListener.js

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         visibilityChangeListener
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Waits until the tab is focused, executing a callback function when it happens.
// @author       IgnaV
// @grant        none
// ==/UserScript==

const visibilityChangeListener = (onFocus, onBlur, minTime = 0) => {
    let lastExecutionFocusTime = 0;
    let lastExecutionBlurTime = 0;

    document.addEventListener('visibilitychange', function () {
        const currentTime = Date.now();

        if (document.visibilityState === 'visible') {
            if (onFocus && currentTime - lastExecutionFocusTime >= minTime) {
                onFocus();
                lastExecutionFocusTime = currentTime;
            }
        } else if (onBlur && currentTime - lastExecutionBlurTime >= minTime) {
            onBlur();
            lastExecutionBlurTime = currentTime;
        }
    });
};