Realtime Auction Countdown Timer

Show realtime countdown timers next to all auction threads in the WTS forum

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            Realtime Auction Countdown Timer
// @description     Show realtime countdown timers next to all auction threads in the WTS forum
// @grant           none
// @include         /https?://forums\.e-hentai\.org/index\.php\?showforum=77\b/
// @version 0.0.1.20161218195024
// @namespace https://greasyfork.org/users/2233
// ==/UserScript==

/*** Settings ***/

var update_interval = 60    // The interval (in seconds) for the timers to be updated.
var timer_height    = 45    // The height of timers (in pixels).

/****************/

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

var init_timer = function(timer, thread_url) {
    var timer_url = ''
    var xhr = new XMLHttpRequest()
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            var container = doc.implementation.createHTMLDocument().documentElement
            container.innerHTML = xhr.responseText
            var timer_img = container.querySelector('img.linked-image[src^="http://reasoningtheory.net/countdown.png?"]')
            if(timer_img) {
                timer_url = timer_img.src.replace(/([?&]?)title=[^&]*/i, '$1')
            } else {
                timer_img = container.querySelector('img.linked-image[src^="http://e-hentai-countdown.darknessfall.com/EH-Cdwn.png?"]')
                if(timer_img) {
                    timer_url = timer_img.src.replace(/([?&]?)auctionname=[^&]*/i, '$1').replace(/([?&]?)expire=[^&]*/i, '$1')
                }
                else {
                    timer_img = container.querySelector('img[src*="countdown"]')
                    if(timer_img) {
                        var yy = mm = dd = hh = mi = ss = offset = ''
                        try { yy = timer_img.src.match(/year=([-0-9]*)/i)[1] } catch(e) {}
                        try { mm = timer_img.src.match(/month=([-0-9]*)/i)[1] } catch(e) {}
                        try { dd = timer_img.src.match(/day=([-0-9]*)/i)[1] } catch(e) {}
                        try { hh = timer_img.src.match(/hour=([-0-9]*)/i)[1] } catch(e) {}
                        try { mi = timer_img.src.match(/minute=([-0-9]*)/i)[1] } catch(e) {}
                        try { ss = timer_img.src.match(/second=([-0-9]*)/i)[1] } catch(e) {}
                        try { offset = timer_img.src.match(/offset=([-0-9]*)/i)[1] } catch(e) {}
                        timer_url = 'http://reasoningtheory.net/countdown.png?'
                        timer_url += '&year=' + yy
                        timer_url += '&month=' + mm
                        timer_url += '&day=' + dd
                        timer_url += '&hour=' + hh
                        timer_url += '&minute=' + mi
                        timer_url += '&second=' + ss
                        timer_url += '&offset=' + offset
                    }
                }
            }
            timer.src = timer_url
        }
    }
    xhr.open('GET', thread_url, true)
    xhr.send(null)
}

var threads = doc.querySelectorAll('table.ipbtable>tbody>tr>td.row1>div:last-child')
for(var i=threads.length-1; i>=0; i--) {
    var title = threads[i].textContent
    //if(/\[(auction|lottery|free)\b/i.test(title)) {
    if(/\[auction\b/i.test(title)) {
        (function() {
            var thread_url = threads[i].querySelector('span>a:first-child').href
            var timer = doc.createElement('IFRAME')
            timer.height = timer_height + 'px'
            timer.frameBorder = 0
            setInterval(function() { timer.src = timer.src }, update_interval*1000)
            threads[i].appendChild(timer)
            init_timer(timer, thread_url)
        })()
    }
}