Greasy Fork

Greasy Fork is available in English.

bcy(半次元)查看原图&下载原图

在每张图片下面增加查看原图&下载原图

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

// ==UserScript==
// @name         bcy(半次元)查看原图&下载原图
// @version      1.0.1
// @description    在每张图片下面增加查看原图&下载原图
// @author       opentdoor
// @match        http://bcy.net/*/detail/*
// @grant        none
// @namespace http://greasyfork.icu/users/92069
// ==/UserScript==
(function () {
  'use strict';
  var imgs = $(".content-img-wrap img");
  var i = 1;
  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, '_');
  imgs.each(function () {
    var $this = $(this);
    var url = this.src.replace(/\/w650$/i, '');
    var av = document.createElement('a');
    var p = $this.closest("div.content-img-wrap");
    p.css({ "position": "relative" });
    av.href = url;
    av.target = "_blank";
    av.title = title + (i > 9 ? i : '0' + i);
    av.innerHTML = "查看原图";
    var complete = this.complete;
    if (!complete) {
      $this.on('load', function () {
        setTimeout(function () {
          var right = Number(((p.width() - $this.width()) / 2).toFixed(0));
          ad.style.right = right + "px";
          av.style.right = (right + 66) + "px";
        }, 80);
      });
    }
    var right = Number(((p.width() - Math.max($this.width(), 132)) / 2).toFixed(0));
    av.style.cssText = "z-index:9999;position:absolute;width:66px;text-align:center;bottom:-21px;right:" + (right + 66) + "px;background:rgba(255,255,255,0.6);";
    p.append(av);
    var ad = av.cloneNode(true);
    ad.style.right = right + "px";
    ad.innerHTML = "下载原图";
    var ex = /\.\w+$/i.exec(url);
    ad.download = ad.title + (ex && ex.length ? ex[0] : ".jpg");
    p.append(ad);
    i++;
  });
})();
(function () {
  'use strict';
  function Download(a) {
    var xhr = new XMLHttpRequest();
    var oldhtml = a.innerHTML;
    xhr.open("GET", a.href, true);
    xhr.responseType = 'blob';
    xhr.onprogress = function (e) {
      if (e.lengthComputable) {
        a.innerHTML = (e.loaded / e.total * 100).toFixed(2) + "%";
      }
    };
    xhr.onload = function (e) {
      if (this.status == 200) {
        var blob = this.response;
        saveFile(blob, a.download);
        a.innerHTML = oldhtml;
        a.isDownload = false;
      }
    };
    xhr.send(null);
  }
  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);
    }
  }
  var ars = document.querySelectorAll("a[href][download]");
  for (var i = 0; i < ars.length; i++) {
    var a = ars[i];
    a.addEventListener("click", function (e) {
      e.preventDefault();
      if (!this.isDownload) Download(this);
      this.isDownload = true;
      return false;
    }, true);
  }
})();