Countdown Timer for the Random Encounter event on E-Hentai

Adds a countdown timer for the Random Encounter event on the E-Hentai.org front page.

Fra og med 05.08.2014. Se den nyeste version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name            Countdown Timer for the Random Encounter event on E-Hentai
// @description     Adds a countdown timer for the Random Encounter event on the E-Hentai.org front page.
// @include         http://e-hentai.org/
// @version 0.0.1.20140805102102
// @namespace https://greasyfork.org/users/2233
// ==/UserScript==

var wnd = window
var doc = wnd.document
var loc = location
var href = loc.href

if(/^http:\/\/e-hentai\.org\/$/.test(href))
{
    var get_cookie = function(k) {
        var cookies = doc.cookie.split('; ')
        for(var i=cookies.length-1; i>=0; i--) { if(new RegExp(k+'=').test(cookies[i])) { return cookies[i].substring(k.length+1) } }
    }
    var set_cookie = function(k, v) {
        doc.cookie = k + '=' + v + '; expires=Fri, 31 Dec 9999 23:59:59 GMT; domain=.e-hentai.org'
    }

    var re_cnt = get_cookie('re_cnt')
    if(typeof re_cnt == 'undefined') { set_cookie('re_cnt', 0) }

    var timer_box = doc.createElement('DIV')
    timer_box.id = 'countdown_timer'
    timer_box.style.color = '#ff0000'
    doc.getElementById('newshead').appendChild(timer_box)

    var update_timer = function()
    {
        var evt = parseInt(get_cookie('event'))
        if(isNaN(evt)) { alert('The "event" cookie does not exist.'); return }

        var now = Math.floor(new Date().getTime()/1000)
        var diff = evt + 1800 - now
        if(diff <= 0) { doc.getElementById('countdown_timer').innerHTML = 'Ready! re_cnt=' + get_cookie('re_cnt') }
        else
        {
            var mm = Math.floor(diff / 60) + ''
            mm = (mm.length >= 2 ? mm : '0' + mm)
            var ss = Math.floor(diff % 60) + ''
            ss = (ss.length >= 2 ? ss : '0' + ss)
            doc.getElementById('countdown_timer').innerHTML = mm + ':' + ss + ', re_cnt=' + get_cookie('re_cnt')
            setTimeout(update_timer, 1000)
        }
    }
    addEventListener('DOMContentLoaded', update_timer, false)

    var eventpane = doc.getElementById('eventpane')
    if(eventpane)
    {
        var hv_lnk = doc.querySelector('#eventpane a[onclick*="http://hentaiverse.org/"]')
        if(hv_lnk)
        {
            hv_lnk.addEventListener('click', function() {
                eventpane.style.display = 'block'
                if(hv_lnk.text != 'HentaiVerse') { set_cookie('re_cnt', parseInt(get_cookie('re_cnt'))+1) }
                hv_lnk.text = 'HentaiVerse'
            }, false)
        }
        else if(/\bdawn\b/i.test(eventpane.innerHTML)) { set_cookie('re_cnt', 0) }
    }
}