Greasy Fork

磁力链接百度网盘补完

try to take over the world!

目前为 2019-05-14 提交的版本。查看 最新版本

// ==UserScript==
// @name         磁力链接百度网盘补完
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  try to take over the world!
// @author       backrock12
// @include      *
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';

    console.log("UrlLinePlugin");
var MagnetMk = true;
var BaiduMk = true;



var PixivMk = false;
var BilibiliMk = false;



//字符串格式化方法
String.prototype.UrlLineformat = function (args) {
  var result = this;
  if (arguments.length > 0) {
    if (arguments.length == 1 && typeof args == "object") {
      for (var key in args) {
        if (args[key] != undefined) {
          var reg = new RegExp("({" + key + "})", "g");
          result = result.replace(reg, args[key]);
        }
      }
    } else {
      for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] != undefined) {
          var reg = new RegExp("({)" + i + "(})", "g");
          result = result.replace(reg, arguments[i]);
        }
      }
    }
  }
  return result;
};


$.fn.replaceText = function (search, replace, action, text_only) {
  return this.each(function () {
    var node = this.firstChild,
      val,
      new_val,// Elements to be removed at the end.
      remove = [];

    // Only continue if firstChild exists.
    if (node) {
      // Loop over all childNodes.
      do {
        // Only process text nodes.
        if (node.nodeType === 3) {
          // The original node value.
          val = node.nodeValue;
          // The new value.
          if (!action && replace) {
            new_val = val.replace(search, replace);
          }
          else {
            var t = search.exec(val)
            if (action && t && t.length > 0 && search.test(val))
              new_val = action(node, val, t);
            else
              new_val = val;
          }
          // Only replace text if the new value is actually different!
          if (new_val !== val) {
            if (!text_only && /</.test(new_val)) {
              // The new value contains HTML, set it in a slower but far more
              // robust way.
              $(node).before(new_val);
              // Don't remove the node yet, or the loop will lose its place.
              remove.push(node);
            } else {
              // The new value contains no HTML, so it can be set in this
              // very fast, simple way.
              node.nodeValue = new_val;
            }
          }
        }
      } while (node = node.nextSibling);
    }

    // Time to remove those elements!
    remove.length && $(remove).remove();
  });
};


//默认配置参数  default settings
var defaults = {
  pixivPattern: /(?=.{6,20}\b)(?=(\b[Pp])|\b(id|Id|ID))[^0-9?]{0,12}(\d{6,10})(?![:.\-\!\?])/g,
  hcagPattern: /(\u672c\u7ad9\u6682?\u4e0d\u63d0\u4f9b(\u6587\u4ef6)?\u4e0b\u8f7d)/g,
  bilibiliPattern: /(av|Av|AV)(\u53f7)?([0-9]{5,10})(?![:.\-\!\?/\d])/g,
  magnetPattern2: /不提供(文件)?下载/g,
  magnetPattern: /(magnet:\?)?(xt=)?(urn:btih:)?(?=.{0,31}[0-9])(?=.{0,31}[a-z])([0-9A-Z]{32,40})(?![:.\-\!\?a-z0-9])/gi,
  pixivurl:
    "<a href='https://www.pixiv.net/member_illust.php?mode=medium&illust_id={0}' target='_blank'>{1}</a>",
  bilibiliurl:
    "<a href='https://www.bilibili.com/video/av{0}/' target='_blank'>{1}</a>",
  magneturl: "<a href='magnet:?xt=urn:btih:{0}' target='_blank'>magnet:?xt=urn:btih:{1}</a>",
  baiduPattern0: /((https?:\/\/)?pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})|(https?:\/\/)?pan\.baidu\.com\/share\/init\?surl=([a-zA-Z0-9_\-]{5,22})|(?!\w)\/?s\/1([a-zA-Z0-9_\-]{5,22}))/,
  baiduurl1:
    "<a href='https://pan.baidu.com/s/1{0}' target='_blank' urlline='true'>" +
    "https://pan.baidu.com/s/1{1}</a>",
  baiduurl2:
    "<a href='https://pan.baidu.com/s/1{0}#{1}' target='_blank' urlline='true' >{2}</a>",
  baiduurl01:
    "<a href='https://pan.baidu.com/s/1{0}' target='_blank' urlline='true'>{1}</a>",
  baiduurl02:
    "<a href='https://pan.baidu.com/s/1{0}#{1}' target='_blank' urlline='true' >{2}</a>",
  baiduurl3:
    "<a href='https://pan.baidu.com/s/1{0}' target='_blank' urlline='true' >{1}</a>",
  baiduPattern: /(https?:\/\/)?pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})/,
  baiduPattern2: /(https?:\/\/)?pan\.baidu\.com\/share\/init\?surl=([a-zA-Z0-9_\-]{5,22})/,
  baiduPattern3: /(?!\w)\/?s\/1([a-zA-Z0-9_\-]{5,22})/,
  common_reg: /\s*(提取密碼|提取密码|提取码|提取碼|提取|密碼|密码|百度|百度云|云盘|yun|通关口令|本帖隐藏的内容)[::]?\s*(<[^>]+>)?\s*([0-9a-zA-Z]{4,})\s*/,
  common_r: "\\s*({0})[::]?\\s*(<[^>]+>)?\\s*([0-9a-zA-Z]{4,})\\s*",
  Pwnum: 5
};
function UrlLinePlugin(options) {
  this.settings = $.extend({}, defaults, options);
  this._defaults = defaults;
  this.init();
}
UrlLinePlugin.prototype = {
  init: function () {
    //obj = this;
  },
  Star: function () {
    var e = this;
    console.log("star");

    e.StarReplace.call(e);
  },
  StarReplace: function () {
    var obj = this;
    if (PixivMk)
      obj.PixivReplace(obj);
    if (BilibiliMk)
      obj.BilibiliReplace(obj);
    if (MagnetMk)
      obj.MagnetReplace(obj);
    if (BaiduMk)
      obj.BaiduReplace(obj);
  },
  PixivReplace: function (obj) {
    try {
      obj._subPixivReplace(obj);
    } catch (err) {
      console.log("Pixiv replace Error:" + err.message);
    }
  },
  BilibiliReplace: function (obj) {
    try {
      obj._subBilibiliReplace(obj);
    } catch (err) {
      console.log("Bilibili replace Error:" + err.message);
    }
  },
  MagnetReplace: function (obj) {
    try {
      obj._subMagnetReplace(obj);
    } catch (err) {
      console.log("Magnet replace Error:" + err.message);
    }
  },

  BaiduReplace: function (obj) {

    try {
      if (/pan\.baidu\.com/.test(location.href)) {
        console.log("baiduPassword");
        //填寫密碼
        if (location.hash && location.hash.length == 5) {

          var pwinput = $(".QKKaIE");
          if (!pwinput)
            pwinput = $(":contains('请输入提取码'):last").nextAll("input");
          if (!pwinput)
            pwinput = $(":contains('请输入提取码'):last").next().find("input");
          pwinput.val(location.hash.slice(1, 5));

          setTimeout(function () {
            var pwbtn = $(".g-button-right");
            if(!pwbtn)
              $("a:contains('提取文件')");
              pwbtn.click();
          }, 50);
        }
      } else {
        obj._subBaiduReplace(obj);
      }
    } catch (err) {
      console.log("baidu replace Error:" + err.message);
    }
  },
  _subPixivReplace: function (obj) {
    function pixivReplacer(match, p1, p2, p3) {
      return obj.settings.pixivurl.UrlLineformat(p3, match);
    }
    $("*")
      .not("script")
      .replaceText(obj.settings.pixivPattern, pixivReplacer);
    console.log(location.href + "is pixiv replace");
  },
  _subBilibiliReplace: function (obj) {
    function bilibiliReplacer(match, p1, p2, p3) {
      return obj.settings.bilibiliurl.UrlLineformat(p3, match);
    }
    $("*")
      .not("script")
      .replaceText(obj.settings.bilibiliPattern, bilibiliReplacer);
    console.log(location.href + "is bilibili replace");
  },
  _subMagnetReplace: function (obj) {
    $("*").replaceText(obj.settings.hcagPattern, "");
    $("*").replaceText(obj.settings.magnetPattern2, "");
    function magnetReplacer(match, p1, p2, p3, p4) {
      var value = match
        .replace("magnet:?", "")
        .replace("xt=", "")
        .replace("urn:btih:", "");
      return obj.settings.magneturl.UrlLineformat(value, match);
    }
    $("*")
      .not("script")
      .replaceText(obj.settings.magnetPattern, magnetReplacer);
    console.log(location.href + "is magnet replace");
  },

      _subBaiduReplace: function (obj) {
      function fetchPid(e) {
        let t2;
        //var v = (t2 = /https?:\/\/pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})/.exec(e)) && 2 === t2.length ? t2[1] : (t2 = baiduPattern2.exec(e)) && 3 === t2.length ? t2[2] : null
        var v =
          (t2 = obj.settings.baiduPattern.exec(e)) && 3 === t2.length
            ? t2[2]
            : null;
        if (v == null)
          v =
            (t2 = obj.settings.baiduPattern2.exec(e)) && 3 === t2.length
              ? t2[2]
              : null;
        if (v == null)
          v =
            (t2 = obj.settings.baiduPattern3.exec(e)) && 2 === t2.length
              ? t2[1]
              : null;
        return v;
      }
      function GetDownUrl(value, Pid, pw) {
        if (!pw) {
          //return obj.settings.baiduurl01.UrlLineformat(Pid, value);
          return obj.settings.baiduurl1.UrlLineformat(Pid, Pid);
        } else {
          //return obj.settings.baiduurl02.UrlLineformat(Pid,pw, value);
          return obj.settings.baiduurl2.UrlLineformat(Pid, pw, pw);
        }
      }
      function Geturl(Pid, match) {
        return obj.settings.baiduurl3.UrlLineformat(Pid, match);
      }
      function baiducheck(text) {
          return obj.settings.baiduPattern0.test(text)
        return (
          obj.settings.baiduPattern.test(text) ||
          obj.settings.baiduPattern2.test(text) ||
          obj.settings.baiduPattern3.test(text)
        );
      }
      function GetPw(text) {
        let p;
        var pw =
          (p = obj.settings.common_reg.exec(text)) && 4 === p.length
            ? p[3]
            : null;
        return pw;
      }

	  function	Getnextnode(node)
	  {
		  var loopnum = obj.settings.Pwnum;
		  var bnode = node.nextSibling;
		  if(!bnode)bnode=node.parentNode.nextSibling;
			while(node&&!bnode&&loopnum >= 0){
				 bnode = node.nextSibling;
				 node = node.parentNode;
				 loopnum--;
			 }
			return	bnode;
	  }

      function FindNextPw(node, Pid) {
        var loopnum = obj.settings.Pwnum;
        var bnode = Getnextnode(node);
        if (bnode) {
          do {
            var bh = bnode.nodeValue ? bnode.nodeValue : bnode.outerHTML;
            var bt = bnode.nodeValue ? bnode.nodeValue : bnode.innerText;
            if (bnode && bt) {
              if (baiducheck(bt)) break;
              var pw = GetPw(bt);
              if (pw != null) {
                var retext = GetDownUrl(pw, Pid, pw);
                var h = bh.replace(pw, retext);
                $(bnode).replaceWith(h);
                //$(bnode).remove();
                break;
              }
            }
			if(!bnode.nextSibling)
			{
				bnode = Getnextnode(bnode);
				}
			else
			{
			bnode =bnode.nextSibling;
			}

            if (!bnode) break;
            loopnum--;
          } while (loopnum >= 0);
        }
      }

      function ReplaceorPw(node, e, n) {
        var Pid = fetchPid(n[0]);
        if (!Pid) return e;
        var retext = GetDownUrl(n[0], Pid);
        var v = e.replace(n[0], retext);
        var pw = GetPw(e);
        if (pw != null) {
          var retext2 = GetDownUrl(pw, Pid, pw);
          v = v.replace(pw, retext2);
        } else {
          FindNextPw(node, Pid);
        }
        return v;
      } //Replacer functions

      function baiduReplacer(match, p1, p2, p3) {
        var Pid = fetchPid(match);
        return Geturl(Pid, match);
      } 

       $("*")
        .not("script")
        .not("a")
        .replaceText(obj.settings.baiduPattern0, baiduReplacer, ReplaceorPw);

//       $("*")
//         .not("script")
//         .not("a")
//         .replaceText(obj.settings.baiduPattern, baiduReplacer, ReplaceorPw);

//           $("*")
//         .not("script")
//         .not("a")
//         .replaceText(obj.settings.baiduPattern2, baiduReplacer, ReplaceorPw);

//           $("*")
//         .not("script")
//         .not("a")
//         .replaceText(obj.settings.baiduPattern3, baiduReplacer, ReplaceorPw);

      $("a[urlline!='true']").each(function () {
        var h = $(this).attr("href");
        if (baiducheck(h)) {
          var Pid = fetchPid(h);
          FindNextPw(this, Pid);
        }
      });
      console.log(location.href + "is baidu replace");
    }

  };

var e = new UrlLinePlugin();
e.Star();

})();