Greasy Fork

Greasy Fork is available in English.

bcy(半次元)打包原图

先安装"Zip格式支持",原图包可从详细页或图片封面下载。

目前为 2017-03-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         bcy(半次元)打包原图
// @version       1.1.6
// @description   先安装"Zip格式支持",原图包可从详细页或图片封面下载。
// @author       opentdoor
// @match        http://bcy.net/*
// @grant        none
// @namespace http://greasyfork.icu/users/92069
// ==/UserScript==
var gl = {
};
(function () {
  'use strict';
  function DownLoad(url, progress, callback) {
    try {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.onreadystatechange = function (e) {
        if (this.readyState == 4) {
          callback(this.response, this.status);
        }
      };
      xhr.responseType = 'blob';
      xhr.onprogress = function (e) {
        progress(e);
      };
      xhr.send(null);
    } catch (ex) {
      callback(null, 500);
    }
  }
  function urlsToZip(urlFiles, zipName, av) {
    var zipWriter,
    index = 0;
    var writer = new zip.BlobWriter();
    function onError() {
      console.log(arguments);
    }
    function downloadProgress(e) {
      if (e.lengthComputable) {
        av.innerHTML = '正在打包第' + (index + 1) + '个:' + (e.loaded / e.total * 80).toFixed(0) + '%';
      }
    }
    function zipProgress(current, total) {
      av.innerHTML = '正在打包第' + (index) + '个:' + (80 + 20 * current / total).toFixed(0) + '%';
    }
    function zipEnd() {
      zipWriter.close(function (blob) {
        saveFile(blob, zipName);
      });
      av.isdown = false;
      av.innerHTML = '打包带走';
    }
    zip.createWriter(writer, function (wr) {
      zipWriter = wr;
    }, onError);
    function saveFile(blob, filename) {
      if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(blob, filename);
      } else {
        var a = document.createElement('a');
        var url = URL.createObjectURL(blob);
        a.href = url;
        a.download = filename;
        var evt = document.createEvent('MouseEvents');
        evt.initEvent('click', true, true);
        a.dispatchEvent(evt);
        URL.revokeObjectURL(url);
      }
    }
    function nextFile() {
      var file = urlFiles[index];
      DownLoad(file.url, downloadProgress, function (blob, status) {
        index++;
        if (status == 200) {
          zipWriter.add(file.name, new zip.BlobReader(blob), function () {
            if (index < urlFiles.length)
            nextFile();
             else
            zipEnd();
          }, zipProgress);
        } else {
          file.tryTime = (file.tryTime || 0) + 1;
          if (file.tryTime < 4) index--;
          if (!av.oldclick) {
            av.oldclick = av.onclick;
          }
          av.onclick = function () {
            av.onclick = av.oldclick;
            if (index < urlFiles.length)
            nextFile();
             else
            zipEnd();
          };
          setTimeout(function () {
            var ff = urlFiles[index];
            av.innerHTML = '第' + (index + 1) + '个下载失败,点击重试(' + (ff.tryTime || 0) + '次)!';
          }, 60);
        }
      });
    }
    setTimeout(nextFile, 250);
    ;
  }
  gl.urlsToZip = urlsToZip;
}) ();
(function () {
  'use strict';
  $(window).on('load', function () { //增加firefox支持
    var p = $('.js-content-img-wrap');
    var title = document.title.replace(/\|.*$/i, '').replace(/(^\s*|\s*$)/gi, '');
    var cn = $('.l-detailUser-name .blue1').html();
    if (cn) title += '-cn_' + cn;
    title = title.replace(/[\\\/\?\<\>\|\*"]/gi, '_');
    var urls = [
    ];
    var imgs = $('.content-img-wrap img');
    var i = 1;
    imgs.each(function () {
      var $this = $(this);
      var url = this.src.replace(/\/w650$/i, '');
      var ex = /\.\w+$/i.exec(url);
      var name = title + (i > 9 ? i : '0' + i) + (ex && ex.length ? ex[0] : '.jpg');
      urls.push({
        name: name,
        url: url
      });
      i++;
    });
    if (p.length && urls.length) {
      var av = document.createElement('a');
      p.css({
        'position': 'relative'
      });
      av.href = 'javascript:;';
      av.innerHTML = '打包带走';
      av.style.width = '250px';
      $('#phone').html('').append(av);
      av.onclick = function (e) {
        if (!this.isdown) {
          this.isdown = true;
          gl.urlsToZip(urls, title + '.zip', this);
        }
      };
    }
  });
}) ();
(function () {
  'use strict';
  function getInfo(url, an, callback) {
    try {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.onprogress = function (e) {
        if (e.lengthComputable) {
          an.innerHTML = '开始解析链接:' + (e.loaded / e.total * 100).toFixed(0) + '%';
        } else {
          an.innerHTML = '开始解析链接:' + (e.loaded / 1024).toFixed(0) + 'KB';
        }
      };
      var urls = [
      ];
      xhr.onreadystatechange = function (e) {
        if (this.readyState == 4) {
          if (this.status == 200) {
            var resText = this.responseText.replace(/[\r\n]*/g, '');
            var str = '';
            var reg = /\<img.*?src=['"]([^'"]*?)\/w650['"].*?\/\>/gi;
            var rs,
            i1 = 0,
            i2 = 0,
            i3;
            do {
              i1 = resText.indexOf('post__content', i2);
              if (i1 == - 1) break;
              i2 = resText.indexOf('</div', i1);
              i3 = resText.indexOf('<div', i1);
              while (i2 > i3 && i3 != - 1) {
                i2 = resText.indexOf('</div', i2 + 1);
                i3 = resText.indexOf('<div', i3 + 1);
              }
              str = resText.substring(i1, i2);
              rs = reg.exec(str);
            } while (!rs || !rs.length);
            i1 = resText.indexOf('<title>');
            i2 = resText.indexOf('</title>', i1);
            var title = resText.substring(i1 + 7, i2).replace(/\|.*$/i, '').replace(/(^\s*|\s*$)/gi, '');
            i1 = resText.indexOf('l-detailUser-name');
            if (i1 != - 1) {
              i2 = resText.indexOf('</div', i1);
              var pm = /\<a .*?blue1.*?\>(.*?)\<\/a/i.exec(resText.substring(i1, i2));
              var cn = pm ? pm[1] : '';
              if (cn) title += '-cn_' + cn;
            }
            title = title.replace(/[\\\/\?\<\>\|\*"]/gi, '_');
            var i = 1;
            while (rs && rs.length) {
              var ex = /\.\w+$/i.exec(rs[1]);
              var name = title + (i > 9 ? i : '0' + i) + (ex && ex.length ? ex[0] : '.jpg');
              urls.push({
                name: name,
                url: rs[1]
              });
              rs = reg.exec(str);
              i++;
            }
            callback(urls, title);
          } else {
            callback(urls, null);
          }
        }
      };
      xhr.send(null);
    } catch (ex) {
      callback([], null);
    }
  }
  function addAn(p, url) {
    p.css('position', 'relative');
    var a = document.createElement('a');
    a.innerHTML = '打包带走';
    a.href = 'javascript:;';
    a.style.cssText = 'position:absolute;top:0px;left:0px;right:0px;height:auto!important;line-height:150%;text-align:center;background:#1c84c6;color:white;border:none!important;box-shadow:none;';
    p.append(a);
    p.mouseenter(function () {
      $(a).show();
    }).mouseleave(function () {
      if (!a.isdown) $(a).hide();
    });
    a.onclick = function () {
      if (!this.isdown) {
        this.isdown = true;
        this.innerHTML = '开始解析链接...';
        getInfo(url, this, function (urls, title) {
          if (urls.length && title) {
            gl.urlsToZip(urls, title + '.zip', a);
          } else {
            a.innerHTML = '解析失败';
            a.isdown = false;
          }
        });
      }
      return false;
    };
    return a;
  }
  $(window).mouseenter(function (e) {
    if (e.target.tagName == 'IMG' && !e.target.iss01) {
      e.target.iss01 = true;
      var $this = $(e.target);
      var p = $this.closest('a');
      if (p && p.length && !p[0].iss01) {
        p[0].iss01 = true;
        if (p.is('[href*=\'/detail/\']:not([href^=\'/zhipin/\'])')) {
          var pp = p.parent();
          addAn(pp, p.attr('href'));
        }
      }
    }
  });
  $(window).on('load', function () {
    $('a.postWorkCard__link').each(function () {
      var a = addAn($(this).parent(), $(this).attr('href'));
      $(a).hide();
    });
  });
}) ();