AutoUnlock

自动跳转并解锁百度网盘、Mega分享

目前為 2018-02-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AutoUnlock
// @namespace    https://greasyfork.org/scripts/31324-autounlock
// @version      0.4.0
// @description  自动跳转并解锁百度网盘、Mega分享
// @author       MaiJZ
// @homepageURL  https://github.com/maijz128/AutoUnlock
// @supportURL   https://github.com/maijz128/AutoUnlock
// @match        http://maijz128.github.io/AutoUnlock/AutoUnlock/?open=*
// @match        https://maijz128.github.io/AutoUnlock/AutoUnlock/?open=*
// @match        http://pan.baidu.com/share/init?*
// @match        https://pan.baidu.com/share/init?*
// @match        http://localhost:8094/AutoUnlock/?open=*
// @grant        none
// ==/UserScript==

const SITE_WAIT_TIME = 500;
const DATA_OVER_TIME = 10 * 1000;


const Util = {
    hrefContains: function (str) {
        return location.href.indexOf(str) > 1;
    },
    GetQueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r !== null)
            return (r[2]);
        else
            return null;
    }
};


(function () {
    'use strict';
    run();
})();

function run() {
    var isAutoUnlockSite = Util.hrefContains("AutoUnlock");
    var isBaiduShareInitSite = Util.hrefContains("pan.baidu.com/share/init");

    if (isAutoUnlockSite) {
        var inter = setInterval(function () {
            if (AutoUnlock.done) {
                clearInterval(inter);
                handle(AutoUnlock);
            }
        }, 50);
    } else if (isBaiduShareInitSite) {
        if (Util.hrefContains("password=")) {
            unlock_baidu2();
        }
    }
}

function handle(autoUnlock) {
    const url = autoUnlock.url;
    const password = autoUnlock.password;
    const isMega = url.indexOf("mega.nz") > -1;
    const isBaiduPan = url.indexOf("pan.baidu.com") > -1;


    if (isBaiduPan) {
        handleBaidu(url, password);
    } else if (isMega) {
        handleMega(url, password);
    }
}

function handleBaidu(url, password) {
    var autoUnlock = {
        updateTime: Date.now(),
        url: url,
        password: password
    };
    console.group("AutoUnlockSite >> pan.baiu.com:");
    console.info(autoUnlock);
    console.groupEnd();

    const baiduLink_format = formatBaiduLink(url);
    const targetURL = baiduLink_format + "&password=" + password;
    console.log(targetURL);
    window.location.href = targetURL;

}

function formatBaiduLink(link) {
    // link : https://pan.baidu.com/s/1kUM8Lt9
    // return : https://pan.baidu.com/share/init?surl=kUM8Lt9
    const tokenList = link.split("/");
    var key = tokenList[tokenList.length - 1];
    // 去掉前面一位字符
    key = key.substring(1);
    return "https://pan.baidu.com/share/init?surl=" + key;
}

function handleMega(url, password) {
    var targetURL = url + password;
    jumpSite(targetURL);
}

// 更新数据后跳转到网盘
function jumpSite(url) {
    var targetURL = url;
    if (targetURL.indexOf("http") !== 0) {
        targetURL = "http://" + targetURL;
    }
    setTimeout(function () {
        if (targetURL) {
            location.href = targetURL;
        }
    }, SITE_WAIT_TIME);
}

function unlock_baidu2() {
    const value = Util.GetQueryString("password");
    if (value !== null) {
        var password = decodeURIComponent(value);
        console.log("password=" + password);
        _unlock_baidu(password);
    }
}


function _unlock_baidu(password, count) {
    const MAX_TIME = 10 * 1000;
    const INTERVAL = 50;
    const MAX_COUNT = MAX_TIME / INTERVAL;
    count = count || 1;

    console.log("password: " + password + " count: " + count);
    if (count < MAX_COUNT && password) {

        var inputs = document.querySelectorAll("input");
        var input = inputs[0];
        var submitBtn = document.querySelector("a[title='提取文件']");
        if (input && submitBtn) {
            input.value = password;
            submitBtn.click();
        }

        setTimeout(function () {
            _unlock_baidu(password, count + 1);
        }, INTERVAL);
    }
}