Manga Loader

Load all manga in current page, only available on 178.com and dm5.com

当前为 2016-01-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Manga Loader
  3. // @description Load all manga in current page, only available on 178.com and dm5.com
  4. // @author Chris (http://chrisyip.im)
  5. // @include http://*.178.com/*
  6. // @include http://*.dm5.com/*
  7. // @include http://*.dmzj.com/*
  8. // @include http://www.manhua8.com/manhua/*
  9. // @include http://www.manhua8.net/manhua/*
  10. // @include http://www.manhua1.com/manhua/*
  11. // @include http://www.manhua2.com/manhua/*
  12. // @include http://www.073.cc/mmm/*
  13. // @include http://www.476.cc/anime/*
  14. // @include http://www.tuku.cc/comic/*
  15. // @version 1.3.21
  16. // @namespace https://greasyfork.org/users/8856
  17. // ==/UserScript==
  18.  
  19. /* global $ */
  20.  
  21. (function (window) {
  22. 'use strict'
  23.  
  24. var style, script, init, resize
  25.  
  26. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver
  27.  
  28. resize = function () {
  29. document.body.style.width = getComputedStyle(document.querySelector('html')).width
  30. }
  31.  
  32. init = function () {
  33. 'use strict'
  34.  
  35. let load_dm5, load_178, load_manhua8, load_073, selector, callback
  36. let SetBookmarker = window.SetBookmarker
  37. let ajaxloadimage = window.ajaxloadimage
  38. let nextChapterMsgBox = window.nextChapterMsgBox
  39.  
  40. load_dm5 = function () {
  41. if (!/^\/m\d+/.test(location.pathname)) {
  42. return
  43. }
  44.  
  45. var reNotAllowedImage = /\d+_\d{5,}/
  46.  
  47. var next_chapter_box = document.createElement('div')
  48. next_chapter_box.id = 'next_chapter_box'
  49. next_chapter_box.style.display = 'none'
  50. next_chapter_box.innerHTML = '<a class="close">X</a><p>已经是最后一页。<a class="add-bookmark">加入书签</a>。</p>'
  51. next_chapter_box.addEventListener('click', function (e) {
  52. if (e.target.className.includes('add-bookmark')) {
  53. this.style.display = 'none'
  54. SetBookmarker(window.DM5_CID, window.DM5_MID, window.DM5_IMAGE_COUNT, window.DM5_USERID)
  55. }
  56. if (e.target.className.includes('close')) {
  57. this.style.display = 'none'
  58. }
  59. }, false)
  60.  
  61. function showEnd () {
  62. if (!next_chapter_box.hasAttribute('data-show')) {
  63. $.ajax({
  64. url: window.DM5_CURL_END,
  65. dataType: 'html',
  66. success: function (data) {
  67. var link = $(data).find('.finalPage a[href*="addbookmarker"]')
  68. .siblings().find('[alt="继续阅读下一章"]').parent().get(0)
  69. var s = link ? '继续观看:' + link.outerHTML : '这已经是最新章节。'
  70. next_chapter_box.innerHTML += s
  71. document.body.appendChild(next_chapter_box)
  72. next_chapter_box.style.display = 'block'
  73. var rect = next_chapter_box.getBoundingClientRect()
  74. next_chapter_box.style.marginTop = '-' + rect.height / 2 + 'px'
  75. next_chapter_box.style.marginLeft = '-' + rect.width / 2 + 'px'
  76. next_chapter_box.setAttribute('data-show', true)
  77. },
  78. error: function () {
  79. location.href = window.DM5_CURL_END
  80. }
  81. })
  82. } else {
  83. return next_chapter_box.style.display === 'none' && (next_chapter_box.style.display = 'block')
  84. }
  85. }
  86.  
  87. function initDM5 () {
  88. var container = document.createElement('div')
  89. container.classList.add('ml-image-container')
  90.  
  91. container.addEventListener('click', function (e) {
  92. var node = e.target
  93.  
  94. if (node.tagName !== 'IMG') {
  95. return
  96. }
  97.  
  98. if (node.nextElementSibling) {
  99. var path = location.pathname.match(/^(\/m\d+)/)[1] + '-p' + (Math.floor(node.getAttribute('data-index')) + 1)
  100. location.href = path
  101. } else {
  102. showEnd()
  103. }
  104. }, false)
  105.  
  106. var originalContainer = document.querySelector(selector)
  107. originalContainer.style.display = 'none'
  108.  
  109. var cpImg = originalContainer.querySelectorAll('[id*=cp_image]')
  110.  
  111. originalContainer.parentElement.insertBefore(container, originalContainer)
  112.  
  113. let pager = document.getElementById('search_fy')
  114. let pages = pager.children
  115.  
  116. let currentPage = pager.querySelector('.current')
  117.  
  118. currentPage = currentPage ? Math.floor(currentPage.textContent) : 1
  119.  
  120. let lastPage = -1
  121.  
  122. if (pages.length) {
  123. lastPage = Math.floor(pages[pages.length - 1].textContent)
  124. }
  125.  
  126. var observer = new MutationObserver(function (mutationRecord) {
  127. mutationRecord.forEach(function (mutation) {
  128. if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
  129. var addedNodes = [].filter.call(mutation.addedNodes, function (node) {
  130. return /cp_image/.test(node.id)
  131. })
  132.  
  133. if (addedNodes.length > 0) {
  134. loadImage(addedNodes)
  135. }
  136. }
  137. })
  138. })
  139.  
  140. function loadImage (nodes) {
  141. setTimeout(function () {
  142. if (currentPage > lastPage) {
  143. container.appendChild(nodes[0].cloneNode())
  144. return observer.disconnect()
  145. }
  146.  
  147. var node
  148.  
  149. if (nodes.length > 1) {
  150. node = [].filter.call(nodes, function (node) {
  151. return !reNotAllowedImage.test(node.src) && node.style.display !== 'none'
  152. })
  153. } else {
  154. node = nodes[0]
  155. }
  156.  
  157. var img = document.createElement('img')
  158. img.setAttribute('data-index', currentPage)
  159. img.src = node.src
  160. container.appendChild(img)
  161.  
  162. if (currentPage < lastPage) {
  163. window.DM5_PAGE = ++currentPage
  164. ajaxloadimage(originalContainer, false)
  165. }
  166. }, 200)
  167. }
  168.  
  169. if (cpImg.length > 0) {
  170. loadImage(cpImg)
  171. }
  172.  
  173. observer.observe(originalContainer, {
  174. attributes: true,
  175. childList: true
  176. })
  177. }
  178.  
  179. function waiting () {
  180. setTimeout(function () {
  181. return document.querySelector(selector) ? initDM5() : waiting()
  182. }, 200)
  183. }
  184.  
  185. waiting()
  186. }
  187. // end load_dm5
  188.  
  189. load_178 = function () {
  190. var img = document.querySelector(selector)
  191.  
  192. if (!img) {
  193. return
  194. }
  195.  
  196. var parent = img.parentElement,
  197. currentPage = parseInt(document.querySelector('#jump_select2').value, 10),
  198. url_taste = /(.*\/\d+)(?:-\d+)?(\..*)/i.exec(location.href),
  199. url = {
  200. prefix: url_taste[1] + '-',
  201. format: url_taste[2]
  202. },
  203. callback = function () {
  204. var page = parseInt(this.getAttribute('data-index'), 10)
  205. if (page < window.COMIC_PAGE.page_count) {
  206. window.location.href = url.prefix + (page + 1) + url.format
  207. return
  208. }
  209.  
  210. if ($('#next_chapter').size() > 0) {
  211. nextChapterMsgBox()
  212. } else {
  213. if (window.final_page_url) {
  214. window.location.href = window.final_page_url
  215. return
  216. }
  217. window.alert('你已经浏览完所有内容。')
  218. window.location.href = 'http://manhua.178.com'
  219. }
  220. }
  221.  
  222. for (var i = currentPage, len = window.arr_pages.length, el; i < len; i++) {
  223. el = document.createElement('img')
  224. el.src = window.img_prefix + window.arr_pages[i]
  225. el.setAttribute('data-index', i + 1)
  226. el.addEventListener('mouseup', callback, false)
  227. parent.appendChild(el)
  228. }
  229. }
  230.  
  231. load_manhua8 = function () {
  232. var img = document.querySelector(selector)
  233.  
  234. if (!img) {
  235. return
  236. }
  237.  
  238. var parent = img.parentElement,
  239. pager = document.querySelector('#topSelect'),
  240. total_page = pager.querySelector('option:last-child').value,
  241. current_page = parseInt(pager.value, 10),
  242. url_taste = /(.+?)(\d+)(\.[a-z]{3,4})$/i.exec(img.src),
  243. url = {
  244. prefix: url_taste[1],
  245. page_num_length: url_taste[2].length,
  246. format: url_taste[3]
  247. },
  248. formatURL = function (index) {
  249. index = String(index)
  250. var len = url.page_num_length - index.length
  251. while (len) {
  252. index = '0' + index
  253. len--
  254. }
  255. return url.prefix + index + url.format
  256. }
  257.  
  258. while (++current_page <= total_page) {
  259. let el = document.createElement('img')
  260. el.classList.add('ImgComic')
  261. el.src = formatURL(current_page)
  262. el.setAttribute('data-index', current_page)
  263. parent.appendChild(el)
  264. }
  265. }
  266.  
  267. load_073 = function () {
  268. $(function () {
  269. window.loadAllimg()
  270. })
  271. }
  272.  
  273. function load_tuku () {
  274. function format_number (n, digit) {
  275. var s = String(n)
  276.  
  277. for (var i = 0; i < digit; i++) {
  278. if (s.length < digit) {
  279. s = '0' + s
  280. }
  281. }
  282.  
  283. return s
  284. }
  285.  
  286. var imageContainer = document.querySelector('#imageShow')
  287.  
  288. var container = document.createElement('div')
  289. container.classList.add('ml-image-container')
  290. imageContainer.parentElement.insertBefore(container, imageContainer)
  291.  
  292. var pager = document.querySelector('select[name=select_page1]')
  293. var currentPage = pager.selectedIndex + 1
  294. var lastPage = pager.querySelectorAll('option').length
  295.  
  296. let observer
  297.  
  298. function run (node) {
  299. imageContainer.style.display = 'none'
  300.  
  301. var img = node.querySelector('img')
  302. var clone = img.cloneNode()
  303. clone.removeAttribute('id')
  304. clone.removeAttribute('border')
  305. container.appendChild(clone)
  306.  
  307. var url = clone.src
  308. var digit = url.slice(url.lastIndexOf('/') + 1)
  309.  
  310. url = url.slice(0, url.lastIndexOf('/') + 1)
  311. digit = digit.slice(0, digit.lastIndexOf('.')).length
  312.  
  313. while (currentPage++ < lastPage) {
  314. (function (index) {
  315. var clone = document.createElement('img')
  316. clone.src = url + format_number(index, digit) + '.jpg'
  317. clone.setAttribute('data-prefix', url + format_number(index, digit))
  318. clone.onerror = function () {
  319. this.src = clone.getAttribute('data-prefix') + '.png'
  320. }
  321. container.appendChild(clone)
  322. })(currentPage)
  323. }
  324. observer.disconnect()
  325. }
  326.  
  327. observer = new MutationObserver(function (records) {
  328. records.forEach(function (mutation) {
  329. var node = mutation.addedNodes[0]
  330.  
  331. if (!node || node.id !== 'comicNextA') {
  332. return
  333. }
  334.  
  335. run(node)
  336. })
  337. })
  338. observer.observe(imageContainer, {
  339. attributes: true,
  340. childList: true
  341. })
  342.  
  343. var targetButton = document.querySelector('.readPageBox .pages a')
  344. var button = document.createElement('button')
  345. button.classList.add('btn')
  346. button.type = 'button'
  347. button.textContent = 'Load Manga'
  348. button.addEventListener('click', function () {
  349. var elem = document.querySelector('#comicNextA')
  350. if (elem) {
  351. run(elem)
  352. }
  353. }, false)
  354. targetButton.parentElement.insertBefore(button, targetButton)
  355. }
  356.  
  357. if (/dmzj\.com|178\.com/.test(location.host)) {
  358. selector = 'img[id*=bigimg]'
  359. callback = load_178
  360. } else if (location.host.includes('dm5.com')) {
  361. selector = '#showimage'
  362. callback = load_dm5
  363. } else if (/manhua\d+.(com|net)/.test(location.host)) {
  364. selector = '#comicImg'
  365. callback = load_manhua8
  366. } else if (/(073|476).cc/.test(location.host)) {
  367. selector = 'body'
  368. callback = load_073
  369. } else if (/(tuku).cc/.test(location.host)) {
  370. selector = 'body'
  371. callback = load_tuku
  372. }
  373.  
  374. var ready = function () {
  375. if (document.querySelector(selector)) {
  376. callback.call()
  377. return
  378. }
  379.  
  380. if (document.readyState !== 'complete') {
  381. setTimeout(ready, 1000)
  382. }
  383. }
  384.  
  385. ready()
  386. } // end init()
  387.  
  388. document.body.style.minHeight = screen.availHeight + 100 + 'px'
  389. resize()
  390. window.addEventListener('resize', resize, false)
  391. document.body.style.minWidth = '980px'
  392.  
  393. function styles () {
  394. /*
  395. .ml-image-container img,
  396. .content .ImgComic, .inner_img img, .manga_image {
  397. box-sizing: border-box;
  398. padding: 1px!important;
  399. border: 2px solid gray!important;
  400. margin: 0 auto 10px!important;
  401. display: block!important;
  402. max-width: 99%!important;
  403. width: auto!important;
  404. height: auto!important;
  405. cursor: pointer;
  406. }
  407. .ml-image-container {
  408. text-align: center;
  409. }
  410. #next_chapter_box {
  411. background: #333;
  412. position: fixed;
  413. top: 50%;
  414. left: 50%;
  415. margin: 0;
  416. z-index: 999999;
  417. padding: 20px;
  418. color: #fff;
  419. font-size: 16px;
  420. box-shadow: 0 0 15px #000;
  421. border-radius: 5px;
  422. }
  423. #next_chapter_box a {
  424. color: #FF4E00;
  425. cursor: pointer;
  426. }
  427. .close {
  428. position: absolute;
  429. top: 5px;
  430. right: 5px;
  431. font-size: 12px;
  432. }
  433. */
  434. }
  435.  
  436. style = document.createElement('style')
  437. style.innerHTML = styles.toString().match(/\/\*([\S\s]+)\*\//)[1]
  438. document.head.appendChild(style)
  439.  
  440. script = document.createElement('script')
  441. script.textContent = '(' + init.toString() + ')( window )'
  442. document.head.appendChild(script)
  443. })(window)