// ==UserScript==
// @name 			Զд롾ǿ桿
// @description		Զдȡ롾ǿ桿+̳ȡںϡ
// @author			ƷСè
// @namespace   	www.52pojie.cn
// @homepage		https://greasyfork.org/scripts/13463
// @supportURL		https://greasyfork.org/scripts/13463/feedback
// @version			2.4.2
// @date			2015.10.30
// @modified		2016.10.05
// 
// ֵ֧
// @include     	http://*
// @include			https://*
// 
// 
// @exclude			/^https?://(localhost|10\.|192\.|127\.)/
// @exclude			/https?://www.baidu.com/(?:s|baidu)\?/
// @exclude			/https?://tieba.baidu.com/
// @exclude			https://*.evernote.com/*
// @exclude			https://*.yinxiang.com/*
// @exclude			http*://s.360.cn/*
// @exclude			http://*.ctrip.com/*
// @exclude			http*://*.yunpan.cn/resource/html/proxy.html
// require			http://code.jquery.com/jquery-2.1.4.min.js
// @require			http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
// @grant			unsafeWindow
// @encoding		utf-8
// @run-at			document-idle
// ==/UserScript==

var urls=location.href;
var hash=location.hash;
var host=location.hostname.replace(/^www\./i,'').toLowerCase();
var paths=location.pathname.toLowerCase();
unsafeWindow.eve = Event;

/*
 * ־
 * 2.4.2 2016.10.05
 * 1Ưңshaoit.comںϴ
 * 2reimu.net ںϴ
 * 3޸תӰٶҵ޷
 * 
 * 2.4.1.2 2016.09.28
 * 1 sijihuisuo.club ת
 * 2ӱIP
 * 
 * 2.4.1.1 2016.09.21
 * 1Я
 * 
 * 2.4.1 2016.09.21
 * 1֧һЩվںԤ
 * 2ְ֧ٶҵƵԶύҵ̵Ϊ4~14λ
 * 3֧ www.0dayku.com ȡؼ֡Extracted-code
 * 
 * 2.3.3 2016.08.08
 * 1֪תԤ
 * 
 * 2.3.2 2016.08.04
 * 1ָȡеġ롱ؼ֣ڣĺeվ
 * 
 * 2.3.1 2016.08.03
 * 1΢ȡ֧֣ƥԭ Jixun.Moe
 * 2ȡ
 * 3ظȡ
 * 
 * 2.3.0 2016.08.01
 * 1ƳԽɽ̵̡֧
 * 2ٶҵ̲׷֤
 * 3ҳתӰ죬ʱ֧ɵȡѽɼ
 * 4&ںϵĳɹ	 A ǩ󶨺Ϊ body ¼ԭ Jixun.Moe Ľ飩
 * 5Ҳʱıʽģл 10139 - mudoo Ľ飩
 * 6֧ڻбеȡ
*/

var site = {
  'yunpan.cn': {
    chk:	/^[a-z0-9]{4}$/,
    code:	'.pwd-input',
    btn:	'.submit-btn'
  },
  'baidu.com':{
    chk:	/^[a-z0-9]{4}$/,
    code:	'#accessCode',
    btn:	'#submitBtn',
    PreProcess: function() {	//Ѵ
      if(host=='eyun.baidu.com'){	//Ϊٶҵ̣ҵ̵ķʽ
        conf=site['baidu.com']=site['eyun.baidu.com'];
        site['baidu.com'].PreProcess();
      }
    }
  },
  'eyun.baidu.com': {
    chk:	/^[a-z0-9]{4}$/,
    code:	'.share-access-code',
    btn:	'.g-button-right',
    PreProcess: function() {
      if((hash&&!/sharelink|path/i.test(hash))&&!/enterprise/.test(paths)) {
        console.log('test');
        location.href=location.href.replace(location.hash,'');
      }
    }
  },
  'weiyun.com': {
    chk: /^[a-z0-9]{4}$/i,
    code: '#outlink_pwd',
    btn:  '#outlink_pwd_ok'
  },
  'pwdRule' : /(?:ȡ|)[a]?\s*[: ]?\s*([a-z\d]{4})/,			//
  'codeRule' : /(?:(?:ȡ||[a]||Extracted-code)[a]?)\s*[: ]?\s*([a-z\d]{4})/i,	//͵
  //תԤ
  'JumpUrl' : {
    'zhihu.com' :  {
      href: $('A[href*="//link.zhihu.com/?target="]'),
      url:'//link.zhihu.com/?target='
    },
    'sijihuisuo.club': {
      href: $('.down-tip A[href^="https://www.sijihuisuo.club/go/?url="]'),
      url: 'https://www.sijihuisuo.club/go/?url='
    }
  },
  //ںҪֵ֧վ
  'Support' : {
    'idanmu.co': {
      path : /storage\-download/i,
      callback : function(){
        $('.input-group').each(function(){
          $(this).text($(this).text()+$(this).find('input').val());
        });
      }
    },
    'shaoit.com': {
      path : /.*/i,
      callback : function(){
        $('A[href*=".baidu.com"],A[href*="yunpan.cn"]').each(function(){
          var tmp=$('A[href*="pan.baidu"],A[href*="yunpan"]').parent()
          tmp.html(tmp.html().replace(/(\w{4}$)/ig,'룺$1'))
        });
      }
    },
    'reimu.net': {
      path: /archives/i,
      callback: function(){
        site['codeRule']=/(?:(?:ȡ||[a])[a]?)\s*[: ]?\s*([a-z\d]{4}|8)/i
      }
    }
  }
};

var hostName = location.host.match(/\w+\.\w+$/)[0].toLowerCase();	//ȡǰվã
var conf = site[hostName];											//

var HostArr = [];									//
for(var i in site) HostArr.push(i);					//
var HostExp = new RegExp(HostArr.join("|"),'i');	//У鳬ӵ

//console.log(site.JumpUrl[host]);

/* -----===== ǷҪת Start =====----- */

if(site['JumpUrl'][host]){
  console.log(site['JumpUrl'][host]['href'])
  site['JumpUrl'][host]['href'].each(function(){
    console.log(site['JumpUrl'][host]['rep'])
    $(this).attr({'href':decodeURIComponent($(this).attr('href').replace(site['JumpUrl'][host]['url'],'')),'target':'blank'});
  });
}
/* -----===== ǷҪת End =====----- */


if(conf&&!/zhidao.baidu.com/i.test(host)){	//ҳ¼
  // ץȡȡ
  if(conf.PreProcess) conf.PreProcess();		//Ԥٶҵƣ
  var sCode = hash.slice(1).trim();

  // ãǷΪϷʽ
  if (!conf.chk.test(sCode)) {
    console.log('û Key ʽ');
  } else {
    console.log ('ץȡȡ: %s', sCode);
  }

  // ӸСʱ
  setTimeout (function () {
    // ȡ벢ύť
    var codeBox = $(conf.code),
        btnOk = $(conf.btn);
    if(codeBox.length>0) {		//ʱŽύ
      codeBox.val(sCode);		//д֤
      if (conf.preSubmit)
        if (conf.preSubmit (codeBox, btnOk))
          return ;
      btnOk.click();
    }
  }, 10);
} else {
  //ں رֵ֧վ
  if(site['Support'][hostName]&&site['Support'][hostName]['path'].test(paths)) {
    site['Support'][hostName].callback();
  }
  // A ǩ¼
  $('body').on('click', 'a', function () {
    var target=this;
    // hash 
    if(this.hash) return;
    //ĿΪٶҵ̣ƥ䷶ΧԼݰٶҵ
    if(/eyun.baidu.com/i.test(this.href)) {
  		site['pwdRule']=/(?:ȡ|)[a]?\s*[: ]?\s*([a-z\d]{4,14})/;
  		site['codeRule']=/(?:(?:ȡ||[a]|Extracted-code)[a]?)\s*[: ]?\s*([a-z\d]{4,14})/i;
    }
    //var target=event.target.tagName==='A'?event.target:event.target.parentNode;
   	
    //У鳬ƥ
    if(HostExp.test(this.href)&&!/(?:tieba)\.baidu\.com/i.test(this.href)){
      
      
      if(site['codeRule'].test(target.textContent)){
        console.log('ڵǰӵĶв');
        target.href+='#'+extCode(target);
      } else if(target.nextSibling&&site['codeRule'].test(target.nextSibling.textContent)){
        console.log('ڳӺֵԪ',target.nextSibling.textContent);
        if(!/#/i.test(target.href)) target.href+='#'+extCode(target.nextSibling);
      } else if(site['pwdRule'].test(target.parentNode.textContent)){
        console.log('Ӹв');
        if(!/#/i.test(target.href)) target.href+='#'+extCode(target.parentNode);
      } else {
        var i = 0,
            maxParent = 5,	//ϱĲ㼶
            parent = target;
        while(i<maxParent) {
          i++;									//
          parent = parent.parentNode;			//ȡø
          console.log('ϼĿ¼룺'+ i,parent);
          if(parent.tagName=="TR") {				//Ǳӱȡ
            if(site['codeRule'].test(parent.nextElementSibling.textContent)) {
              parent=parent.nextElementSibling;
              //console.log('вɹ',parent);
              target.href+='#'+extCode(parent);
              break;
            }
          } else if(site['codeRule'].test(parent.textContent)) {		//ճ淽ʽȡ
            console.log('ϱɹ');
            target.href+='#'+extCode(parent);
            break;
          }
          if(parent==document.body) break;								//Ѿ
        }
      }
      //console.log(site['codeRule']);
            //ɵĴӸбʽ
            //console.log(' document.body в');
            //if(!/#/i.test(target.href)) target.href+='#'+extCode(document.body);
    }
  });
}

function extCode(obj){
  text=obj.textContent.trim();
  return site['pwdRule'].test(text)?text.match(site['pwdRule'])[1]:text.match(site['codeRule'])[1];	//ȳʹ ȡ| ΪƥĹؼ֣Чʱʹøƥ
}