AutoPager for E-Hentai

Provides an AutoPager-like function for g.E-Hentai.org.

  1. // ==UserScript==
  2. // @name AutoPager for E-Hentai
  3. // @description Provides an AutoPager-like function for g.E-Hentai.org.
  4. // @grant GM_xmlhttpRequest
  5. // @include http://g.e-hentai.org/*
  6. // @include http://exhentai.org/*
  7. // @version 0.0.1.20150802205409
  8. // @namespace https://greasyfork.org/users/2233
  9. // ==/UserScript==
  10.  
  11. /*** Settings ***/
  12.  
  13. var freq = [5, 10] // min seconds ~ max seconds
  14.  
  15. /*** End of Settings ***/
  16.  
  17. var wnd = window
  18. var doc = wnd.document
  19. var loc = location
  20. var href = loc.href
  21.  
  22. var $ = function(e, css) { if(!css) { css=e; e=doc }; return e.querySelector(css) }
  23. var $$ = function(e, css) { if(!css) { css=e; e=doc }; return e.querySelectorAll(css) }
  24.  
  25. if(((/^http:\/\/g\.e-hentai\.org\//.test(href)) || (/^http:\/\/exhentai\.org\//.test(href))) && (/\/[gs]\//.test(href))) {
  26. var api_url = ''
  27. if(/^http:\/\/g\.e-hentai\.org\//.test(href)) { api_url = 'http://g.e-hentai.org/api.php' }
  28. else if(/^http:\/\/exhentai\.org\//.test(href)) { api_url = 'http://exhentai.org/api.php' }
  29. if(!api_url) { throw 'exit' }
  30.  
  31. // g: gid, gtoken; s: gid, gtoken, imgkey
  32. var gid = gtoken = ''
  33. if(/\/g\//.test(href)) {
  34. //if(!/\??\bp=/.test(href)) { loc.assign(href + '?p=0') }
  35. var m = /\/g\/([^\/]+)\/([^\/]+)/.exec(href)
  36. if(m == null) { console.log('Invalid gallery.'); throw 'exit' }
  37. gid = m[1], gtoken = m[2]
  38. } else if(/\/s\//.test(href)) {
  39. var m = /\/s\/([^\/]+)\/([^\/]+)-([0-9]+)/.exec(href)
  40. if(m == null) { console.log('Invalid image page.'); throw 'exit' }
  41. var imgkey = m[1], gid = m[2], page = m[3]
  42. //if(imgkey.length > 10) { imgkey = imgkey.substring(0, 10) }
  43. if(typeof GM_xmlhttpRequest != 'undefined') {
  44. var xhr = GM_xmlhttpRequest({
  45. method: 'POST',
  46. url: api_url,
  47. data: JSON.stringify({'method':'gtoken', 'pagelist':[[gid, imgkey, page]]}),
  48. synchronous: true
  49. })
  50. }
  51. else {
  52. var xhr = new XMLHttpRequest()
  53. xhr.open('POST', api_url, false)
  54. xhr.send(JSON.stringify({'method':'gtoken', 'pagelist':[[gid, imgkey, page]]}))
  55. }
  56. console.log('xhr.responseText = ' + xhr.responseText)
  57. gtoken = JSON.parse(xhr.responseText)['tokenlist'][0]['token']
  58. }
  59. console.log('gid = ' + gid + ', gtoken = ' + gtoken)
  60. if(gid == '') { console.log('Cannot find gid.') }
  61. if(gtoken == '') { console.log('Cannot find gtoken.') }
  62. if((gid == '') || (gtoken == '')) { throw 'exit' }
  63.  
  64. // g: filecount; s: filecount
  65. var filecount = ''
  66. if(typeof GM_xmlhttpRequest != 'undefined') {
  67. var xhr = GM_xmlhttpRequest({
  68. method: 'POST',
  69. url: api_url,
  70. data: JSON.stringify({'method':'gdata', 'gidlist':[[gid, gtoken]]}),
  71. synchronous: true
  72. })
  73. }
  74. else {
  75. var xhr = new XMLHttpRequest()
  76. xhr.open('POST', api_url, false)
  77. xhr.send(JSON.stringify({'method':'gdata', 'gidlist':[[gid, gtoken]]}))
  78. }
  79. console.log('xhr.responseText = ' + xhr.responseText)
  80. filecount = parseInt(JSON.parse(xhr.responseText)['gmetadata'][0]['filecount'])
  81. console.log('filecount = ' + filecount)
  82. if(isNaN(filecount)) { console.log('Cannot find filecount.'); throw 'exit' }
  83.  
  84. // g: page_url, imgkey; s: page_url
  85. if(/\/g\//.test(href)) {
  86. var imgkey = page_url = ''
  87. //var page = $$('.ip')[0].textContent.match(/\d+/)[0], page_url = ''
  88. //var page = parseInt($('#gdt a').href.replace(/^.*-([0-9]+)/, '$1')), page_url = ''
  89. var page = parseInt($('.gpc').textContent.match(/\d+/)[0])
  90. console.log('page = ' + page)
  91.  
  92. var lnks = $$('A[href*="/s/"]')
  93. var url_pattern = new RegExp('/s/[^/]+/'+gid+'-'+page+'$')
  94. for(var i=lnks.length-1; i>=0; i--) {
  95. if(url_pattern.test(lnks[i].href)) {
  96. page_url = lnks[i].href
  97. imgkey = /\/s\/([^\/]+)/.exec(lnks[i].href)[1]
  98. //if(imgkey.length > 10) { imgkey = imgkey.substring(0, 10) }
  99. break
  100. }
  101. }
  102. }
  103. else if(/\/s\//.test(href)) { page_url = href.replace(/\?.*/, '') }
  104. if((page_url == '') || (imgkey == '')) {
  105. if(/\/g\//.test(href)) {
  106. var mpv_url = ''
  107. var lnks = $$('A[href*="/mpv/"]')
  108. var url_pattern = new RegExp('/mpv/'+gid+'/[^/]+/#page'+page+'$')
  109. for(var i=lnks.length-1; i>=0; i--) {
  110. if(url_pattern.test(lnks[i].href)) {
  111. mpv_url = lnks[i].href
  112. if(typeof GM_xmlhttpRequest != 'undefined') {
  113. var xhr = GM_xmlhttpRequest({
  114. method: 'GET',
  115. url: mpv_url,
  116. synchronous: true
  117. })
  118. }
  119. else {
  120. var xhr = new XMLHttpRequest()
  121. xhr.open('GET', mpv_url, false)
  122. xhr.send(null)
  123. }
  124. var pa = xhr.responseText.match(/var +imagelist *= *\[([^\]]+)\]/)[1].split('},').map(function(s) { return s + '}' })
  125. imgkey = JSON.parse(pa[page-1]).k
  126. page_url = loc.protocol + '//' + loc.hostname + '/s/' + imgkey + '/' + gid + '-' + page
  127. break
  128. }
  129. }
  130. }
  131. }
  132. console.log('page_url = ' + page_url + ', imgkey = ' + imgkey)
  133. if(page_url == '') { console.log('Cannot find page_url.') }
  134. if(imgkey == '') { console.log('Cannot find the first imgkey.') }
  135. if((page_url == '') || (imgkey == '')) { throw 'exit' }
  136.  
  137. // g: showkey; s: showkey
  138. var showkey = ''
  139. if(typeof GM_xmlhttpRequest != 'undefined') {
  140. var xhr = GM_xmlhttpRequest({
  141. method: 'GET',
  142. url: page_url,
  143. synchronous: true
  144. })
  145. }
  146. else {
  147. var xhr = new XMLHttpRequest()
  148. xhr.open('GET', page_url, false)
  149. xhr.send(null)
  150. }
  151. //console.log('xhr.responseText = ' + xhr.responseText)
  152. try{ showkey = /\bshowkey=['"]?([-0-9a-z]+)/.exec(xhr.responseText)[1] } catch(e) { showkey = '' }
  153. console.log('showkey = ' + showkey)
  154. if(showkey == '') { console.log('Cannot find the first showkey.'); throw 'exit' }
  155.  
  156. var get_rand = function(range) {
  157. var min = range[0]
  158. var max = range[1]
  159. return Math.floor(Math.random() * (max - min + 1)) + min
  160. }
  161.  
  162. var b = doc.body
  163. var append_img = function() {
  164. if(typeof GM_xmlhttpRequest != 'undefined') {
  165. GM_xmlhttpRequest({
  166. method: 'POST',
  167. url: api_url,
  168. data: JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}),
  169. onload: function(response) {
  170. var res = JSON.parse(response.responseText)
  171. var i3 = res['i3']
  172. var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')
  173.  
  174. var img = new Image()
  175. img.id = page
  176. img.onerror = function() { this.onerror = null; this.style.display = 'none' }
  177. img.src = img_src
  178. img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
  179. img.width = wnd.innerWidth
  180. var a = doc.createElement('A')
  181. a.href = page_url.replace(/\/s\/.*/, '/s/' + imgkey + '/' + gid + '-' + page)
  182. a.text = 'p. ' + page + ' / ' + filecount
  183. b.appendChild(a)
  184. b.appendChild(doc.createElement('BR'))
  185. b.appendChild(img)
  186. b.appendChild(doc.createElement('BR'))
  187.  
  188. if(page >= filecount) { return }
  189. var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
  190. if(m == null) { console.log('An error happened when parsing p. ' + page); return }
  191. imgkey = m[1]
  192. page = parseInt(m[2])
  193. setTimeout(append_img, get_rand(freq)*1000)
  194. }
  195. })
  196. }
  197. else {
  198. var xhr = new XMLHttpRequest()
  199. xhr.onreadystatechange = function() {
  200. if(xhr.readyState == 4 && xhr.status == 200) {
  201. var res = JSON.parse(xhr.responseText)
  202. var i3 = res['i3']
  203. var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')
  204.  
  205. var img = new Image()
  206. img.id = page
  207. img.onerror = function() { this.onerror = null; this.style.display = 'none' }
  208. img.src = img_src
  209. img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
  210. img.width = wnd.innerWidth
  211. var a = doc.createElement('A')
  212. a.href = page_url.replace(/\/s\/.*/, '/s/' + imgkey + '/' + gid + '-' + page)
  213. a.text = 'p. ' + page + ' / ' + filecount
  214. b.appendChild(a)
  215. b.appendChild(doc.createElement('BR'))
  216. b.appendChild(img)
  217. b.appendChild(doc.createElement('BR'))
  218.  
  219. if(page >= filecount) { return }
  220. var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
  221. if(m == null) { console.log('An error happened when parsing p. ' + page); return }
  222. imgkey = m[1]
  223. page = parseInt(m[2])
  224. setTimeout(append_img, get_rand(freq)*1000)
  225. }
  226. }
  227. xhr.open('POST', api_url, true)
  228. xhr.send(JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}))
  229. }
  230. }
  231. append_img()
  232. }