Drink Water

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);
        }
    }
})();