AutoPager for E-Hentai

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

La data de 25-06-2015. Vezi ultima versiune.

  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.20150625174318
  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. /*** Settings ***/
  20.  
  21. var freq = [5, 7] // min seconds ~ max seconds
  22.  
  23. /*** End of Settings ***/
  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. console.log('page_url = ' + page_url + ', imgkey = ' + imgkey)
  105. if(page_url == '') { console.log('Cannot find page_url.') }
  106. if(imgkey == '') { console.log('Cannot find the first imgkey.') }
  107. if((page_url == '') || (imgkey == '')) { throw 'exit' }
  108.  
  109. // g: showkey; s: showkey
  110. var showkey = ''
  111. if(typeof GM_xmlhttpRequest != 'undefined') {
  112. var xhr = GM_xmlhttpRequest({
  113. method: 'GET',
  114. url: page_url,
  115. synchronous: true
  116. })
  117. }
  118. else {
  119. var xhr = new XMLHttpRequest()
  120. xhr.open('GET', page_url, false)
  121. xhr.send(null)
  122. }
  123. //console.log('xhr.responseText = ' + xhr.responseText)
  124. try{ showkey = /\bshowkey=['"]?([-0-9a-z]+)/.exec(xhr.responseText)[1] } catch(e) { showkey = '' }
  125. console.log('showkey = ' + showkey)
  126. if(showkey == '') { console.log('Cannot find the first showkey.'); throw 'exit' }
  127.  
  128. var get_rand = function(range) {
  129. var min = range[0]
  130. var max = range[1]
  131. return Math.floor(Math.random() * (max - min + 1)) + min
  132. }
  133.  
  134. var b = doc.body
  135. var append_img = function() {
  136. if(typeof GM_xmlhttpRequest != 'undefined') {
  137. GM_xmlhttpRequest({
  138. method: 'POST',
  139. url: api_url,
  140. data: JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}),
  141. onload: function(response) {
  142. var res = JSON.parse(response.responseText)
  143. var i3 = res['i3']
  144. var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')
  145.  
  146. var img = new Image()
  147. img.id = page
  148. img.onerror = function() { this.onerror = null; this.style.display = 'none' }
  149. img.src = img_src
  150. img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
  151. img.width = wnd.innerWidth
  152. b.appendChild(doc.createTextNode('p. '+page+' / '+filecount))
  153. b.appendChild(doc.createElement('BR'))
  154. b.appendChild(img)
  155. b.appendChild(doc.createElement('BR'))
  156.  
  157. if(page >= filecount) { return }
  158. var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
  159. if(m == null) { console.log('An error happened when parsing p. ' + page); return }
  160. imgkey = m[1]
  161. page = parseInt(m[2])
  162. setTimeout(append_img, get_rand(freq)*1000)
  163. }
  164. })
  165. }
  166. else {
  167. var xhr = new XMLHttpRequest()
  168. xhr.onreadystatechange = function() {
  169. if(xhr.readyState == 4 && xhr.status == 200) {
  170. var res = JSON.parse(xhr.responseText)
  171.  
  172. var i3 = res['i3']
  173. var img_src = /src=['"]([^'"]+)['"]/.exec(i3)[1].replace(/&/g, '&')
  174.  
  175. var img = new Image()
  176. img.id = page
  177. img.onerror = function() { this.onerror = null; this.style.display = 'none' }
  178. img.src = img_src
  179. img.style.cssText = 'display: block; margin-left: auto; margin-right: auto'
  180. img.width = wnd.innerWidth
  181. b.appendChild(doc.createTextNode('p. '+page+' / '+filecount))
  182. b.appendChild(doc.createElement('BR'))
  183. b.appendChild(img)
  184. b.appendChild(doc.createElement('BR'))
  185.  
  186. if(page >= filecount) { return }
  187. var m = /\/s\/([^'"]+?)\/[^'"]+-([0-9]+)['"]/.exec(i3)
  188. if(m == null) { console.log('An error happened when parsing p. ' + page); return }
  189. imgkey = m[1]
  190. page = parseInt(m[2])
  191. setTimeout(append_img, get_rand(freq)*1000)
  192. }
  193. }
  194. xhr.open('POST', api_url, true)
  195. xhr.send(JSON.stringify({'method':'showpage', 'gid':gid, 'page':page, 'imgkey':imgkey, 'showkey':showkey}))
  196. }
  197. }
  198. append_img()
  199. }