Vote Reminder

Get a notification when the timer of the opened page expire

目前為 2024-04-27 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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