AutoPager for E-Hentai

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

As of 24.03.2015. See ბოლო ვერსია.

  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.20150324132514
  8. // @namespace https://greasyfork.org/users/2233
  9. // ==/UserScript==
  10.  
  11. var wnd = window
  12. var doc = wnd.document
  13. var loc = location
  14. var href = loc.href
  15.  
  16. var $ = function(e, css) { if(!css) { css=e; e=doc }; return e.querySelector(css) }
  17. var $$ = function(e, css) { if(!css) { css=e; e=doc }; return e.querySelectorAll(css) }
  18.  
  19. if(/\/[gs]\//.test(href)) {
  20. if(!/\??\bp=/.test(href)) { loc.href = href + '?p=0' }
  21. var api_url = ''
  22. if(/^http:\/\/g\.e-hentai\.org\//.test(href)) { api_url = 'http://g.e-hentai.org/api.php' }
  23. else if(/^http:\/\/exhentai\.org\//.test(href)) { api_url = 'http://exhentai.org/api.php' }
  24. if(!api_url) { throw 'exit' }
  25.  
  26. if((/^http:\/\/g\.e-hentai\.org\//.test(href)) || (/^http:\/\/exhentai\.org\//.test(href)))
  27. {
  28. if(!/\/[gs]\//.test(href)) { throw 'exit' }
  29.  
  30. var gid = gtoken = ''
  31. if(/\/g\//.test(href))
  32. {
  33. var m = /\/g\/([^\/]+)\/([^\/]+)/.exec(href)
  34. if(m == null) { console.log('Invalid gallery.'); throw 'exit' }
  35. gid = m[1], gtoken = m[2]
  36. }
  37. else if(/\/s\//.test(href))
  38. {
  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. {
  45. var gtoken = GM_xmlhttpRequest({
  46. method: 'POST',
  47. url: api_url,
  48. data: JSON.stringify({'method':'gtoken', 'pagelist':[[gid, imgkey, page]]}),
  49. synchronous: true
  50. })
  51. }
  52. else
  53. {
  54. var gtoken = new XMLHttpRequest()
  55. gtoken.open('POST', api_url, false)
  56. gtoken.send(JSON.stringify({'method':'gtoken', 'pagelist':[[gid, imgkey, page]]}))
  57. }
  58. gtoken = JSON.parse(gtoken.responseText)['tokenlist'][0]['token']
  59. }
  60. if(gid == '' || gtoken == '') { console.log('Cannot find gid or gtoken.'); throw 'exit' }
  61.  
  62. var filecount = ''
  63. if(typeof GM_xmlhttpRequest != 'undefined')
  64. {
  65. filecount = GM_xmlhttpRequest({
  66. method: 'POST',
  67. url: api_url,
  68. data: JSON.stringify({'method':'gdata', 'gidlist':[[gid, gtoken]]}),
  69. synchronous: true
  70. })
  71. }
  72. else
  73. {
  74. filecount = new XMLHttpRequest()
  75. filecount.open('POST', api_url, false)
  76. filecount.send(JSON.stringify({'method':'gdata', 'gidlist':[[gid, gtoken]]}))
  77. }
  78. filecount = parseInt(JSON.parse(filecount.responseText)['gmetadata'][0]['filecount'])
  79. if(isNaN(filecount)) { console.log('Cannot find file count.'); throw 'exit' }
  80.  
  81. if(/\/g\//.test(href))
  82. {
  83. var page = imgkey = ''
  84. //page = $$('.ip')[0].textContent.match(/\d+/)[0], page_url = ''
  85. //page = parseInt($('#gdt a').href.replace(/^.*-([0-9]+)/, '$1')), page_url = ''
  86. page = parseInt($('.gpc').textContent.match(/\d+/)[0]), page_url = ''
  87. lnks = $$('A')
  88. for(var i=lnks.length-1; i>=0; i--)
  89. {
  90. if(new RegExp('/s/[^/]+/'+gid+'-'+page+'$').test(lnks[i].href))
  91. {
  92. try {
  93. page_url = lnks[i].href
  94. imgkey = /\/s\/([^\/]+)/.exec(lnks[i].href)[1]
  95. if(imgkey.length > 10) { imgkey = imgkey.substring(0, 10) }
  96. } catch(e) {}
  97. break
  98. }
  99. }
  100. }
  101. else if(/\/s\//.test(href)) { page_url = href.replace(/\?.*/, '') }
  102. if(imgkey == '') { console.log('Cannot find the first imgkey.'); throw 'exit' }
  103.  
  104. var showkey = ''
  105. if(typeof GM_xmlhttpRequest != 'undefined')
  106. {
  107. showkey = GM_xmlhttpRequest({
  108. method: 'GET',
  109. url: page_url,
  110. synchronous: true
  111. })
  112. }
  113. else
  114. {
  115. showkey = new XMLHttpRequest()
  116. showkey.open('GET', page_url, false)
  117. showkey.send(null)
  118. }
  119. try{ showkey = /\bshowkey=['"]?([-0-9a-z]+)/.exec(showkey.responseText)[1] } catch(e) { showkey = '' }
  120. if(showkey == '') { console.log('Cannot find the first showkey.'); throw 'exit' }
  121.  
  122. var b = doc.body
  123. var append_img = function()
  124. {
  125. if(typeof GM_xmlhttpRequest != 'undefined')
  126. {
  127. GM_xmlhttpRequest({
  128. method: 'POST',
  129. url: api_url,
  130. data: JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}),
  131. onload: function(response)
  132. {
  133. var i3 = JSON.parse(response.responseText)['i3']
  134. var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')
  135.  
  136. var img = new Image()
  137. img.id = page
  138. img.onerror = function() { this.onerror = null; this.style.display = 'none' }
  139. img.src = img_src
  140. img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
  141. img.width = wnd.innerWidth
  142. b.appendChild(doc.createTextNode('p. '+page))
  143. b.appendChild(doc.createElement('BR'))
  144. b.appendChild(img)
  145. b.appendChild(doc.createElement('BR'))
  146.  
  147. if(page >= filecount) { return }
  148. var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
  149. if(m == null) { console.log('An error happened when parsing p. ' + page); return }
  150. imgkey = m[1]
  151. page = parseInt(m[2])
  152. setTimeout(append_img, 2000)
  153. }
  154. })
  155. }
  156. else
  157. {
  158. var xhr = new XMLHttpRequest()
  159. xhr.onreadystatechange = function() {
  160. if(xhr.readyState == 4 && xhr.status == 200) {
  161. var response = xhr
  162.  
  163. var i3 = JSON.parse(response.responseText)['i3']
  164. var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')
  165.  
  166. var img = new Image()
  167. img.id = page
  168. img.onerror = function() { this.onerror = null; this.style.display = 'none' }
  169. img.src = img_src
  170. img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
  171. img.width = wnd.innerWidth
  172. b.appendChild(doc.createTextNode('p. '+page))
  173. b.appendChild(doc.createElement('BR'))
  174. b.appendChild(img)
  175. b.appendChild(doc.createElement('BR'))
  176.  
  177. if(page >= filecount) { return }
  178. var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
  179. if(m == null) { console.log('An error happened when parsing p. ' + page); return }
  180. imgkey = m[1]
  181. page = parseInt(m[2])
  182. setTimeout(append_img, 2000)
  183. }
  184. }
  185. xhr.open('POST', api_url, true)
  186. xhr.send(JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}))
  187. }
  188. }
  189. append_img()
  190. }
  191. }