Greasy Fork is available in English.

onVisibilityChange

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

이 스크립트는 직접 설치해서 쓰는 게 아닙니다. 다른 스크립트가 메타 명령 // @require https://update.greasyfork.org/scripts/480374/1283358/onVisibilityChange.js(으)로 포함하여 쓰는 라이브러리입니다.

질문, 리뷰하거나, 이 스크립트를 신고하세요.
// ==UserScript==
// @name         onVisibilityChange
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Waits until the tab is focused, executing a callback function when it happens.
// @author       IgnaV
// @grant        none
// ==/UserScript==

const onVisibilityChange = (onFocus, onBlur, minTime = 0) => {
    let lastExecutionTime = 0;

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

        if (currentTime - lastExecutionTime >= minTime * 1000) {}
            if (document.visibilityState === 'visible') {
                onFocus?.();
            } else {
                onBlur?.();
            }

            lastExecutionTime = currentTime;
        });
};