Drink Water

notify drink water, for testing. Default 2 hours mention.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Drink Water
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  notify drink water, for testing. Default 2 hours mention.
// @author       luvSeohyun
// @match        https://*/*
// @match        http://*/*
// @grant        GM_log
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    const drinkMention = 2 * 60 * 60 * 1000; // ms

    function crossTime(a, b) {
        if (a.getFullYear() !== b.getFullYear() || a.getMonth() !== b.getMonth() || a.getDate() !== b.getDate()) {
            GM_log(`another day ${a.getTime()}, ${b.getTime()}`);
            return -1;
        }
        GM_log(`cross time ${a.getTime() - b.getTime()}`);
        return a.getTime() - b.getTime();
    }

    function mention_set(str, now, timeout) {
        if (timeout) {
            GM_log('create new time');
            now = new Date();
        }
        if (!document.hidden) {
            alert(str);
            GM_setValue('drinkTime', now);
        }
        setTimeout(mention_set, drinkMention, 'drink water', now);
    }

    if (location.host.startsWith('www')) {
        let now = new Date();
        let saved = new Date(GM_getValue('drinkTime', new Date('2020-6-6')));
        const cross = crossTime(now, saved);
        if (cross === -1) {
            mention_set('new day', now, false);
        } else if(cross >= drinkMention) {
            mention_set('drink water', now, false);
        } else {
            setTimeout(mention_set, drinkMention - cross, 'drink water', now, true);
        }
    }
})();