Vote Reminder

Get a notification when the timer of the opened page expire

当前为 2024-04-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Vote Reminder
// @description Get a notification when the timer of the opened page expire
// @namespace   Violentmonkey Scripts
// @match       https://serveur-prive.net/*/vote
// @icon        https://serveur-prive.net/img/favicon.png
// @version     2024-04-27
// @author      AFKonCore
// @grant       none
// @license     MIT
// ==/UserScript==
window.VoteReminder = {};

window.addEventListener('load', function(){ //wait for page load
    console.log('Vote Reminder: Loaded.');
    if(notificationEnabled()){ //check notifications are enabled, ask them to be allowed if not
        getVoteCooldownAndSetReminder();
    }
});

//Language:
window.VoteReminder.Language = 'English'; // 'French' | 'English'

switch(VoteReminder.Language){
    case 'French':
        window.VoteReminder.allowNotifNotice = 'Activez les notifications afin d\'être rappelé lorsque que vous pouvez à nouveau voter.';
        window.VoteReminder.allowNotifButton = 'Autoriser';
        window.VoteReminder.notifMessage = 'Vous pouvez voter à nouveau !';
        break;
    case 'English':
    default:
        window.VoteReminder.allowNotifNotice = 'Enable notifications to be reminded when you can vote again.';
        window.VoteReminder.allowNotifButton = 'Allow';
        window.VoteReminder.notifMessage = 'You can vote again!';
        break;
}


function notificationEnabled(){
    if(Notification.permission != "granted"){
        //replace the page with a request to allow notifications
        //will reload the page once notifications are allowed
        //will 'brick' if notifications are set to 'blocked'.
        console.log('Vote Reminder: Asking for notifications to be allowed.');
        var html = '<div style="margin:10%;">';
        html += '<h3 style="padding-bottom:10px;">Vote Reminder (UserScript):</h3>';
        html += '<p style="padding-bottom:10px;">'+VoteReminder.allowNotifNotice+'</p>';
        html += '<button ';
        html += 'onclick="Notification.requestPermission().then((permission)=>{location.reload();});" ';
        html += 'style="border:2px solid #90EE90; color:#90EE90; padding:2px 5px;"';
        html += '>'+VoteReminder.allowNotifButton+'</button>';
        html += '</div>';
        document.getElementById("header").innerHTML = '';
        document.getElementById("vote").innerHTML = html;
        document.getElementById("footer").innerHTML = '';
        return false;
    }else{
        return true;
    }
}

function getVoteCooldownAndSetReminder(){
    try {
        if(can_vote_date){
            if(VoteReminder.can_vote_date == undefined || VoteReminder.can_vote_date < can_vote_date){
                VoteReminder.can_vote_date = can_vote_date;
                let voteCooldownInMS = Date.parse(new Date(can_vote_date - new Date()))-500;
                setTimeout(function() {
                    sendNotification();
                }, voteCooldownInMS);

                html = '<span style="padding:10px 0; background:green; border-radius:5px; display:inline-block; width:100%; font-size:16px; text-align:center;">Vote Reminder will notify you ✔️</span>'
                document.getElementById("cooldown").querySelector('.counter').insertAdjacentHTML("afterend", html);

                return console.log('Vote Reminder: Notification in '+readableTimeFromMS(voteCooldownInMS)+'.');
            }

        }
    }catch(e){
        if(document.getElementById("voteBtn") != null){
            if(VoteReminder.can_vote_date == undefined){
                return sendNotification();
            }else{
                console.log('VoteReminder: ???');
            }

        }
        console.log('Vote Reminder: Error, this should not happen. \n getVoteCooldownAndSetReminder(): '+e);
    }
    setTimeout(function() {
				getVoteCooldownAndSetReminder();
	}, 10000);
    console.log("VoteReminder: Check again in 10s (no vote button, no 'can_vote_date')");

}

function sendNotification(){
	new Notification(VoteReminder.notifMessage, {requireInteraction:"true",icon:"https://serveur-prive.net/img/favicon.png"});
    return console.log('Vote Reminder: You can vote again!');

}

function readableTimeFromMS(ms){
    let h = Math.floor(ms/1000/60/60);
    let m = Math.floor((ms/1000/60/60 - h)*60);
    let s = Math.floor(((ms/1000/60/60 - h)*60 - m)*60);

    res = '';
    if(h > 0){
        res+= h+'h';
    }

    if(m > 0){
        if(m < 10 && h > 0) m ='0'+m;
        res+= m+'m';
    }

    if(s < 10 && (h > 0 || m > 0) ) s ='0'+s;
    res+= s+'s';

    return res;
}

//TODO: refresh the page after voting