Greasy Fork

Greasy Fork is available in English.

隐心百度云盘钥匙

百度云盘钥匙,在浏览器中打开云盘分享链接后,会自动填写云盘提取码。

当前为 2018-10-21 提交的版本,查看 最新版本

// ==UserScript==
// @name         隐心百度云盘钥匙
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  百度云盘钥匙,在浏览器中打开云盘分享链接后,会自动填写云盘提取码。
// @author       Yisin
// @match        *://pan.baidu.com/*
// @match        *://*/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant        GM_xmlhttpRequest
// @supportURL   meek.com.cn
// ==/UserScript==

(function() {
    'use strict';   
    var isBDY = false;
    var URL_STORE = {};
    
    function getLocationKey(){
        var key = "";
        var url = document.location.href;
        if(url.indexOf('//pan.baidu.com/s/') != -1){
           key = document.location.pathname.substring(4);
        } else if(url.indexOf('//pan.baidu.com/share/init') != -1){
           key = getUrlParam(null, "surl");
        }
        if(/^1[^\s]+/g.test(key)){
            key = key.substring(1);
        }
        return key;
    }
    
    function getUrlKey(url){
        var key = "";
        if(url.indexOf('//pan.baidu.com/s/') != -1){
           key = url.substring(url.indexOf('//pan.baidu.com/s/') + 18);
        } else if(url.indexOf('//pan.baidu.com/share/init') != -1){
           key = getUrlParam(url, "surl");
        }
        if(/^1[^\s]+/g.test(key)){
            key = key.substring(1);
        }
        return key;
    }

    function getUrlParam(u, param) {
        var url = u || location.href;
        var reg = new RegExp("(" + param + ")=([^&#]*)", "g"),
            matched = url.match(reg);

        return matched && matched[0] ? matched[0].replace(param + "=", "") : null;
    }
    
    function LoadPass(bdyKey, callback){
        var bdyUrl = 'https://pan.baidu.com/s/' + bdyKey;
        var bdyPass = "";
        if(window['localStorage']){
            bdyPass = localStorage.getItem(bdyUrl);                
        }
        if(bdyPass && bdyPass.length == 4){
            console.info(bdyUrl,"->",bdyPass);
            if(callback){
                callback(bdyPass);
            }
            if(isBDY){
                $('.verify-input input').val(bdyPass);
                $('.g-button.g-button-blue-large').click();
            }            
        } else {
            var weburl = 'http://ypsuperkey.meek.com.cn/api/items/BDY-' + bdyKey + '?access_key=4fxNbkKKJX2pAm3b8AEu2zT5d2MbqGbD&client_version=web-client&' + new Date().getTime();
            GM_xmlhttpRequest({
                method: 'GET',
                url: weburl,
                headers: {"Accept": "application/json"},
                contentType: "application/json",
                dataType: 'json',
                onload: function(response){
                    if(response.statusText == 'OK'){
                        try{
                            var res = JSON.parse(response.responseText);
                            bdyPass = res.access_code;
                            console.info(bdyUrl,"->",bdyPass);
                            if(bdyPass && bdyPass.length == 4){
                                if(window['localStorage']){
                                    localStorage.setItem(bdyUrl, bdyPass);
                                }
                                if(callback){
                                    callback(bdyPass);
                                }
                                if(isBDY){
                                    $('.verify-input input').val(bdyPass);                                    
                                    $('.g-button.g-button-blue-large').click();
                                }
                            }                                
                        }catch(e){}
                    }
                }
            });
        }
    }
    
    function ShowBDYPass(url, pass){
        if(URL_STORE[url]){
            return;
        } 
        URL_STORE[url] = pass;
        var $div = $('#BDY-DIV-BOX');
        if(!$div.length){
            $div = $('<div id="BDY-DIV-BOX"></div>');
            $div.css({
                "position": "fixed",
                "width": "430px",
                "height": "100px",
                "overflow": "auto",
                "font-size": "13px",
                "top": 0,
                "left": '-420px',
                "border": "2px solid #5555ff",
                "background": "#e5e5e5",
                "color": "#ff0000",
                "z-index": 9999999
            });
            $div.hover(function(){
                $div.css('left', 0);
            }, function(){
                $div.css('left', '-420px');
            });
            $('html').append($div);
        }
        $div.append('<div>' + url + ',提取码:' + pass + '</div>');
    }
    
    function BaiduPan(url){
        this.url = url;
        this.code = '';
    }
    
    BaiduPan.prototype = {
        load: function(){
            var that = this;
            var bdyKey = getUrlKey(that.url);        
            if(bdyKey){
                LoadPass(bdyKey, function(pass){
                     ShowBDYPass(that.url, pass);
                });         
            }
        }
    };    

    if(/^(http|https):\/\/pan.baidu.com\//g.test(document.location.href)){
        isBDY = true;
        var bdyKey = getLocationKey();        
        if(bdyKey){
             LoadPass(bdyKey);         
        }
    } else {       
        var html = $('body').html();
        if(html){
            var arrs = html.match(/(http|https):\/\/pan.baidu.com\/s\/[a-zA-Z0-9_-]+/g);
            if(arrs && arrs.length){
                var index = 0;
                for(var i = 0; i < arrs.length; i++){
                    console.info(arrs[i]);
                    new BaiduPan(arrs[i]).load();          
                }
            }
        }
    }

})();