AutoPager for E-Hentai

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

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