Countdown Timer for the Random Encounter Event on E-Hentai

Adds a countdown timer for the Random Encounter event on E-Hentai.org and its subdomains.

Tính đến 12-08-2014. Xem phiên bản mới nhất.

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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 E-Hentai.org and its subdomains.
// @include         http://e-hentai.org/*
// @include         http://*.e-hentai.org/*
// @version 0.0.1.20140812054442
// @namespace https://greasyfork.org/users/2233
// ==/UserScript==

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

if(/(\.e-hentai\.org\/)|(^e-hentai.org\/)/.test(loc.hostname+'/'))
{
    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 unescape(cookies[i].substring(k.length+1)) } }
    }
    var set_cookie = function(k, v) { doc.cookie = k + '=' + escape(v) + '; expires=Fri, 31 Dec 9999 23:59:59 GMT; domain=.e-hentai.org; path=/;' }

    if(isNaN(get_cookie('event'))) { console.log('The "event" cookie does not exist or is invalid.'); throw 'exit' }
    if(isNaN(get_cookie('re_cnt'))) { set_cookie('re_cnt', 0) }
    if(!get_cookie('re_lst')) { set_cookie('re_lst', '[]') }

    var timer_box = doc.createElement('DIV')
    timer_box.id = 'countdown_timer'
    timer_box.onclick = function() { if(/\bReady\b/i.test(this.textContent)) { wnd.open('http://e-hentai.org/', href=='http://e-hentai.org/'?'_self':'_blank') } }

    var toggle_re_lst = function() {
        //alert(get_cookie('re_lst'))
        var re_lst_box = doc.getElementById('re_lst_box')
        if(re_lst_box) { re_lst_box.parentNode.removeChild(re_lst_box); return }
        re_lst_box = doc.createElement('DIV')
        re_lst_box.id = 're_lst_box'
        re_lst_box.style.cssText = 'top:15px; right:0px; position:fixed; z-index:2147483647; background:rgba(0,255,0,0.2); color:#ff0000;'
        re_lst_box.innerHTML = '[List of RE Events Occurred Today]<BR>'

        var decode_hv_b64 = function(hv_b64) {
            var e = hv_b64
            var d = atob(e)
            var m = /([^-]+?)-([^-]+?)-([^-]+)/.exec(d)
            if(m == null) { return }
            var uid = m[1]
            var epoch = m[2]
            var hash = m[3]
            var da = new Date()
            da.setTime(parseInt(epoch)*1000)
            da = da.toLocaleTimeString()
            var a = doc.createElement('A')
            a.href = 'http://hentaiverse.org/?s=Battle&ss=ba&encounter=' + e
            a.target = '_blank'
            a.text = i + '. ' + da
            a.style.cssText = 'color:#ff0000; text-decoration:underline;'
            return a
        }
        var re_lst = JSON.parse(get_cookie('re_lst'))
        for(var i=0, len=re_lst.length; i<len; i++) {
            var a = decode_hv_b64(re_lst[i])
            if(i != 0) { re_lst_box.appendChild(doc.createElement('BR')) }
            re_lst_box.appendChild(a)
        }
        doc.body.appendChild(re_lst_box)
    }
    addEventListener('keydown', function(evt) { if((evt.target.tagName!='INPUT') && (evt.target.tagName!='TEXTAREA') && (evt.keyCode == 76)) { toggle_re_lst() } }, false)

    if(/\/e-hentai\./.test(href)) {
        timer_box.style.color = '#ff0000'
        doc.getElementById('newshead').appendChild(timer_box)
    } else {
        timer_box.style.cssText = 'line-height:15px; top:0px; right:0px; position:fixed; z-index:2147483647; background:rgba(0,255,0,0.2); color:#ff0000;'
        doc.body.appendChild(timer_box)
    }

    var update_timer = function() {
        if(href == 'http://e-hentai.org/') {
            var da = new Date()
            if((da.getUTCHours()==0) && (da.getUTCMinutes()==0) && (da.getUTCSeconds()<=3)) { setTimeout(function() {loc.reload()}, 3000) }
        }
        var now = Math.floor(new Date().getTime()/1000)
        var diff = parseInt(get_cookie('event')) + 1800 - now
        if(diff <= 0) {
            timer_box.textContent = 'Ready! re_cnt=' + get_cookie('re_cnt')
            if(href == 'http://e-hentai.org/') { loc.reload() }
        } 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)
            timer_box.textContent = mm + ':' + ss + ', re_cnt=' + get_cookie('re_cnt')
        }
        setTimeout(update_timer, 1000)
    }
    update_timer()

    var eventpane = doc.getElementById('eventpane')
    if(eventpane == null) { throw 'exit' }
    var re_evt = eventpane.querySelector('a[onclick*="http://hentaiverse.org/"]')
    if(re_evt) {
        var hv_lnk = /.*window\.open\(['"]?([^'"]+)['"]?/.exec(re_evt.onclick.toString().split('\n').join(''))
        if(hv_lnk) {
            hv_lnk = hv_lnk[1]
            hv_b64 = hv_lnk.replace(/.+?&encounter=([^&]*).*/, '$1') // the base64 encoded part
            var re_lst = JSON.parse(get_cookie('re_lst'))
            if(re_lst.length == 0) { re_lst = [] }
            if(re_lst.indexOf(hv_b64) == -1) { re_lst.push(hv_b64) }
            re_lst = JSON.stringify(re_lst, null, ' ')
            set_cookie('re_lst', re_lst)
        }
        else {
            console.log('Error: There is a random encounter event but the HentaiVerse link cannot be found.')
            throw 'exit'
        }
        re_evt.addEventListener('click', function() {
            eventpane.style.display = 'block'
            if(re_evt.text != 'HentaiVerse') { set_cookie('re_cnt', parseInt(get_cookie('re_cnt'))+1) }
            re_evt.text = 'HentaiVerse'
        }, false)
    }
    else if(/\bdawn\b/i.test(eventpane.textContent)) {
        set_cookie('re_cnt', 0)
        set_cookie('re_lst', '[]')
    }
}