Greasy Fork

Greasy Fork is available in English.

bcy(半次元)打包原图

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

目前为 2017-01-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         bcy(半次元)打包原图
// @version       1.1
// @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;
        a.click();
        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);
        }
      });
    }
    nextFile();
  }
  gl.urlsToZip = urlsToZip;
})();
(function () {
  'use strict';
  var p = $(".js-content-img-wrap");
  var title = document.title.replace(/\|.*$/i, '').replace(/(^\s*|\s*$)/ig, "");
  var cn = $(".l-detailUser-name .blue1").html();
  if (cn) title += "-cn_" + cn;
  title = title.replace(/[\\\/\?\<\>\|\*"]/ig, '_');
  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) + "%";
        }
      };
      var urls = [];
      xhr.onreadystatechange = function (e) {
        if (this.readyState == 4) {
          if (this.status == 200) {
            var i1 = this.responseText.indexOf('js-content-img-wrap');
            var i2 = this.responseText.indexOf('</div', i1);
            var reg = /\<img .*? src='(.*?)\/w650'.*?\/\>/ig;
            var str = this.responseText.substring(i1, i2);
            i1 = this.responseText.indexOf('<title>');
            i2 = this.responseText.indexOf('</title>', i1);
            var title = this.responseText.substring(i1 + 7, i2).replace(/\|.*$/i, "").replace(/(^\s*|\s*$)/ig, "");
            i1 = this.responseText.indexOf('l-detailUser-name');
            if (i1 != -1) {
              i2 = this.responseText.indexOf('</div', i1);
              var cn = /\<a .*?blue1.*?\>(.*?)\<\/a/i.exec(this.responseText.substring(i1, i2))[1];
              title += "-cn_" + cn;
            }
            title = title.replace(/[\\\/\?\<\>\|\*"]/ig, '_');
            var rs = reg.exec(str);
            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"));
        }
      }
    }
  });
  $("a.postWorkCard__link").each(function () {
    var a = addAn($(this).parent(), $(this).attr("href"));
    $(a).hide();
  });
})();