Greasy Fork

Manga Loader

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

目前为 2015-03-25 提交的版本。查看 最新版本

// ==UserScript==
// @name          Manga Loader
// @description   Load all manga in current page, only available on 178.com and dm5.com
// @author        Chris (http://chrisyip.im)
// @include       http://*.178.com/*
// @include       http://*.dm5.com/*
// @include       http://*.dmzj.com/*
// @include       http://www.manhua8.com/manhua/*
// @include       http://www.manhua8.net/manhua/*
// @include       http://www.manhua1.com/manhua/*
// @include       http://www.manhua2.com/manhua/*
// @include       http://www.073.cc/mmm/*
// @include       http://www.476.cc/anime/*
// @include       http://www.tuku.cc/comic/*
// @version       1.3.18
// @namespace https://greasyfork.org/users/8856
// ==/UserScript==

;(function(window){
  var style, script, init, resize

  var MutationObserver = window.MutationObserver || window.WebKitMutationObserver

  resize = function(){
    document.body.style.width = getComputedStyle(document.querySelector('html')).width
  }

  init = function(){
    var load_dm5, load_178, load_manhua8, load_073, selector, callback

    load_dm5 = function(){
      if (!/^\/m\d+/.test(location.pathname)) {
        return
      }

      var reNotAllowedImage = /\d+_\d{5,}/

      var next_chapter_box = document.createElement('div')
      next_chapter_box.id = 'next_chapter_box'
      next_chapter_box.style.display = 'none'
      next_chapter_box.innerHTML = '<a class="close">X</a><p>已经是最后一页。<a class="add-bookmark">加入书签</a>。</p>'
      next_chapter_box.addEventListener('click', function (e) {
        if (-1 < e.target.className.indexOf('add-bookmark')) {
          this.style.display = 'none'
          SetBookmarker(DM5_CID, DM5_MID, DM5_IMAGE_COUNT, DM5_USERID)
        }
        if (-1 < e.target.className.indexOf('close')) {
          this.style.display = 'none'
        }
      }, false)

      function showEnd (argument) {
        if (!next_chapter_box.hasAttribute('data-show')) {
          $.ajax({
            url: DM5_CURL_END,
            dataType: 'html',
            success: function (data) {
              var div = $(data).find('#index_mian a[href="javascript:addfavorite();"]').get(0).parentElement
              var link = div.querySelector('a[href*=m]')
              var s = link ? '继续观看:' + link.outerHTML : '这已经是最新章节。'
              next_chapter_box.innerHTML += s;
              document.body.appendChild(next_chapter_box)
              next_chapter_box.style.display = 'block'
              var rect = next_chapter_box.getBoundingClientRect()
              next_chapter_box.style.marginTop = '-' + (rect.height / 2) + 'px'
              next_chapter_box.style.marginLeft = '-' + (rect.width / 2) + 'px'
              next_chapter_box.setAttribute('data-show', true)
            },
            error: function(){
              location.href = DM5_CURL_END
            }
          })
        } else {
          'none' === next_chapter_box.style.display && (next_chapter_box.style.display = 'block')
        }
      }

      function init () {
        var container = document.createElement('div')
        container.classList.add('ml-image-container')

        container.addEventListener('click', function (e) {
          var node = e.target

          if (node.tagName !== 'IMG') {
            return
          }

          if (node.nextElementSibling) {
            var path = location.pathname.match(/^(\/m\d+)/)[1] + '-p' + (Math.floor(node.getAttribute('data-index')) + 1)
            location.href = path
          } else {
            showEnd()
          }
        }, false)

        var originalContainer = document.querySelector(selector)
        originalContainer.style.display = 'none'

        var cpImg = originalContainer.querySelectorAll('[id*=cp_image]')

        originalContainer.parentElement.insertBefore(container, originalContainer)

        var pager = document.getElementById('search_fy')
        var pages = pager.children
        var lastPage = Math.floor(pages[pages.length - 1].textContent)
        var currentPage = Math.floor(pager.querySelector('.current').textContent)

        var observer = new MutationObserver(function (mutationRecord, observer) {
          mutationRecord.forEach(function( mutation ) {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
              var addedNodes = [].filter.call(mutation.addedNodes, function (node) {
                return /cp_image/.test(node.id)
              })

              if (addedNodes.length > 0) {
                loadImage(addedNodes)
              }
            }
          })
        })

        function loadImage (nodes) {
          setTimeout(function () {
            if (currentPage > lastPage) {
              return observer.disconnect()
            }

            var node = [].filter.call(nodes, function (node) {
              return !reNotAllowedImage.test(node.src) && node.style.display !== 'none'
            })[0]

            var img = document.createElement('img')
            img.setAttribute('data-index', currentPage)
            img.src = node.src
            container.appendChild(img)

            if (currentPage < lastPage) {
              DM5_PAGE = ++currentPage
              ajaxloadimage(originalContainer, false)
            }
          }, 200)
        }

        if (cpImg.length > 0) {
          loadImage(cpImg)
        }

        observer.observe(originalContainer, { attributes: true, childList: true })
      }

      function waiting () {
        setTimeout(function () {
          document.querySelector(selector) ? init() : waiting()
        }, 200)
      }

      waiting()
    }
    // end load_dm5

    load_178 = function(){
      var img = document.querySelector( selector )
      if (!img) return;
      var parent = img.parentElement
        , currentPage = parseInt(document.querySelector('#jump_select2').value, 10)
        , url_taste = /(.*\/\d+)(?:-\d+)?(\..*)/i.exec(location.href)
        , url = {
          prefix: url_taste[1] + '-',
          format: url_taste[2]
        }
        , callback = function(){
          var page = parseInt(this.getAttribute('data-index'), 10)
          if (page < COMIC_PAGE.page_count) {
            window.location.href = url.prefix + (page + 1) + url.format
            return;
          } else {
            if ($('#next_chapter').size() > 0) {
              nextChapterMsgBox()
            } else {
              if (final_page_url) {
                window.location.href=final_page_url
                return;
              }
              alert('你已经浏览完所有内容。')
              window.location.href = 'http://manhua.178.com'
            }
          }
        }

      for (var i = currentPage, len = arr_pages.length, el; i < len; i++) {
        el = document.createElement('img')
        el.src = img_prefix + arr_pages[i]
        el.setAttribute('data-index', i + 1)
        el.addEventListener('mouseup', callback, false)
        parent.appendChild(el)
      }
    }

    load_manhua8 = function(){
      var img = document.querySelector( selector )
      if (!img) return;
      var parent = img.parentElement
        , pager = document.querySelector( '#topSelect' )
        , total_page = pager.querySelector( 'option:last-child' ).value
        , current_page = parseInt( pager.value, 10 )
        , url_taste = /(.+?)(\d+)(\.[a-z]{3,4})$/i.exec( img.src )
        , url = {
          prefix: url_taste[1],
          page_num_length: url_taste[2].length,
          format: url_taste[3]
        }
        , formatURL = function ( index ) {
          index = String(index)
          var len = url.page_num_length - index.length
          while ( len ) {
            index = '0' + index
            len--
          }
          return url.prefix + index + url.format
        }

      while ( ++current_page <= total_page ) {
        el = document.createElement( 'img' )
        el.classList.add( 'ImgComic' )
        el.src = formatURL( current_page )
        el.setAttribute( 'data-index', current_page )
        parent.appendChild( el )
      }
    }

    load_073 = function () {
      $(function () {
        window.loadAllimg()
      })
    }

    function load_tuku () {
      function format_number (n, digit) {
        var s = String(n)

        for (var i = 0; i < digit; i++) {
          if (s.length < digit) {
            s = '0' + s
          }
        }

        return s
      }

      var imageContainer = document.querySelector('#imageShow')

      var container = document.createElement('div')
      container.classList.add('ml-image-container')
      imageContainer.parentElement.insertBefore(container, imageContainer)

      var pager = document.querySelector('select[name=select_page1]')
      var currentPage = pager.selectedIndex + 1
      var lastPage = pager.querySelectorAll('option').length

      var observer = new MutationObserver(function (records, observer) {
        records.forEach(function (mutation) {
          var node = mutation.addedNodes[0]

          if (!node || node.id !== 'comicNextA') {
            return
          }

          imageContainer.style.display = 'none'

          var img = node.querySelector('img')
          var clone = img.cloneNode()
          clone.removeAttribute('id')
          clone.removeAttribute('border')
          container.appendChild(clone)

          var url = clone.src
          var digit = url.slice(url.lastIndexOf('/') + 1)

          url = url.slice(0, url.lastIndexOf('/') + 1)
          digit = digit.slice(0, digit.lastIndexOf('.')).length

          while (currentPage++ < lastPage) {
            (function (index) {
              var clone = document.createElement('img')
              clone.src = url + format_number(index, digit) + '.jpg'
              clone.setAttribute('data-prefix', url + format_number(index, digit))
              clone.onerror = function () {
                this.src = clone.getAttribute('data-prefix') + '.png'
              }
              container.appendChild(clone)
            })(currentPage)
          }
          observer.disconnect()
        })
      })
      observer.observe(imageContainer, { attributes: true, childList: true })
    }

    if ( /dmzj\.com|178\.com/.test( location.host ) ) {
      selector = 'img[id*=bigimg]'
      callback = load_178
    } else if ( ~location.host.indexOf( 'dm5.com' ) ) {
      selector = '#showimage'
      callback = load_dm5
    } else if ( /manhua\d+.(com|net)/.test( location.host ) ) {
      selector = '#comicImg'
      callback = load_manhua8
    } else if ( /(073|476).cc/.test(location.host) ) {
      selector = 'body'
      callback = load_073
    } else if ( /(tuku).cc/.test(location.host) ) {
      selector = 'body'
      callback = load_tuku
    }

    (function(){
      if ( document.querySelector( selector ) ) {
        callback.call()
        return;
      }

      if ( document.readyState !== 'complete' ) {
        setTimeout( arguments.callee, 1000 )
      }
    })()
  } // end init()

  document.body.style.minHeight = (screen.availHeight + 100) + 'px'
  resize()
  window.addEventListener('resize', resize, false)
  document.body.style.minWidth = '980px'

  function styles () {/*
    .ml-image-container img,
    .content .ImgComic, .inner_img img, .manga_image {
      box-sizing: border-box;
      padding: 1px!important;
      border: 2px solid gray!important;
      margin: 0 auto 10px!important;
      display: block!important;
      max-width: 99%!important;
      width: auto!important;
      height: auto!important;
      cursor: pointer;
    }

    .ml-image-container {
      text-align: center;
    }

    #next_chapter_box {
      background: #333;
      position: fixed;
      top: 50%;
      left: 50%;
      margin: 0;
      z-index: 999999;
      padding: 20px;
      color: #fff;
      font-size: 16px;
      box-shadow: 0 0 15px #000;
      border-radius: 5px;
    }

    #next_chapter_box a {
      color: #FF4E00;
      cursor: pointer;
    }

    .close {
      position: absolute;
      top: 5px;
      right: 5px;
      font-size: 12px;
    }
  */}

  style = document.createElement('style')
  style.innerHTML = styles.toString().match(/\/\*([\S\s]+)\*\//)[1]
  document.head.appendChild(style)

  script = document.createElement( 'script' )
  script.textContent = '(' + init.toString() + ')( window )'
  document.head.appendChild( script )
})(window);