AutoPager for E-Hentai

Provides AutoPager-like function for E-Hentai.

As of 2014-08-03. See the latest 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            AutoPager for E-Hentai
// @description     Provides AutoPager-like function for E-Hentai.
// @grant           GM_xmlhttpRequest
// @grant           unsafeWindow
// @include         http://g.e-hentai.org/*
// @version 0.0.1.20140803090251
// @namespace https://greasyfork.org/users/2233
// ==/UserScript==

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

if(/^http:\/\/g\.e-hentai\.org\//.test(href))
{
    if(!/\/[gs]\//.test(href)){ throw 'exit' }

    var gid = gtoken = ''
    if(/\/g\//.test(href))
    {
        var m = /\/g\/([^\/]+)\/([^\/]+)/.exec(href)
        if(m == null){ alert('Invalid gallery.'); throw 'exit' }
        gid = m[1], gtoken = m[2]
    }
    else if(/\/s\//.test(href))
    {
        var m = /\/s\/([^\/]+)\/([^\/]+)-([0-9]+)/.exec(href)
        if(m == null){ alert('Invalid image page.'); throw 'exit' }
        var imgkey = m[1], gid = m[2], page = m[3]
        if(imgkey.length > 10) { imgkey = imgkey.substring(0, 10) }
        var gtoken = GM_xmlhttpRequest({
            method: 'POST',
            url: 'http://g.e-hentai.org/api.php',
            data: JSON.stringify({'method':'gtoken', 'pagelist':[[gid, imgkey, page]]}),
            synchronous: true,
        })
        gtoken = JSON.parse(gtoken.responseText)['tokenlist'][0]['token']
    }
    if(gid == '' || gtoken == ''){ alert('Cannot find gid or gtoken.'); throw 'exit' }

    var filecount = ''
    filecount = GM_xmlhttpRequest({
        method: 'POST',
        url: 'http://g.e-hentai.org/api.php',
        data: JSON.stringify({'method':'gdata', 'gidlist':[[gid, gtoken]]}),
        synchronous: true,
    })
    filecount = parseInt(JSON.parse(filecount.responseText)['gmetadata'][0]['filecount'])
    if(isNaN(filecount)){ alert('Cannot find file count.'); throw 'exit' }

    if(/\/g\//.test(href))
    {
        var page = imgkey = ''
        page = parseInt(doc.querySelector('#gdt a').href.replace(/^.*-([0-9]+)/, '$1')), page_url = ''
        lnks = doc.getElementsByTagName('A')
        for(var i=lnks.length-1; i>=0; i--)
        {
            if(new RegExp('/s/[^/]+/'+gid+'-'+page+'$').test(lnks[i].href))
            {
                try {
                    page_url = lnks[i].href
                    imgkey = /\/s\/([^\/]+)/.exec(lnks[i].href)[1]
                    if(imgkey.length > 10) { imgkey = imgkey.substring(0, 10) }
                } catch(e){}
                break
            }
        }
    }
    else if(/\/s\//.test(href)){ page_url = href.replace(/\?.*/, '') }
    if(imgkey == ''){ alert('Cannot find the first imgkey.'); throw 'exit' }

    var showkey = ''
    try {
        showkey = /\bshowkey=['"]?([-0-9a-z]+)/.exec(GM_xmlhttpRequest({
            method: 'GET',
            url: page_url,
            synchronous: true,
        }).responseText)[1]
    } catch(e){}
    if(showkey == ''){ alert('Cannot find the first showkey.'); throw 'exit' }

    var b = doc.body
    var append_img = function()
    {
        GM_xmlhttpRequest({
            method: 'POST',
            url: 'http://g.e-hentai.org/api.php',
            data: JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}),
            onload: function(response)
            {
                var i3 = JSON.parse(response.responseText)['i3']
                var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')

                var img = new Image()
                img.id = page
                img.onerror = function(){ this.onerror = null; this.style.display = 'none' }
                img.src = img_src
                img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
                img.width = wnd.innerWidth * 0.70
                b.appendChild(doc.createTextNode('p. '+page))
                b.appendChild(doc.createElement('BR'))
                b.appendChild(img)
                b.appendChild(doc.createElement('BR'))

                if(page >= filecount){ return }
                var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
                if(m == null){ alert('An error happened when parsing p. ' + page); return }
                imgkey = m[1]
                page = parseInt(m[2])
                setTimeout(append_img, 3000)
            }
        })
    }
    append_img()
}