自定義服務器(解除B站區域限制輔助腳本)

以彈窗設定解除B站區域限制腳本使用伺服器的小工具,必須配合解除B站區域限制腳本一同使用。

As of 2017-04-19. See the latest version.

// ==UserScript==
// @name         自定義服務器(解除B站區域限制輔助腳本)
// @namespace    http://tampermonkey.net/
// @version      0.5.0
// @description  以彈窗設定解除B站區域限制腳本使用伺服器的小工具,必須配合解除B站區域限制腳本一同使用。
// @author       i9602097
// @include      *://bangumi.bilibili.com/anime/*
// @include      *://bangumi.bilibili.com/anime/v/*
// @include      *://www.bilibili.com/html/html5player.html*
// @include      *://www.bilibili.com/blackboard/html5player.html*
// @run-at       document-end
// @grant      GM_registerMenuCommand
// @grant      unsafeWindow
// ==/UserScript==
//
if (unsafeWindow.self === unsafeWindow.top) {
    function setDefault(argument) {
        if (argument) {
            return argument;
        } else {
            return 'https://www.biliplus.com';
        }
    }

    function popupText(argument) {
        if (argument) {
            return "讀取的Cookie值為:" + argument + "\n將使用上述伺服器";
        } else {
            return "讀取的Cookie值為空白\n將使用預設伺服器";
        }
    }

    function setHost(argument) {
        if (argument === null) {
            alert("已取消操作\nCookie沒被修改\n" + popupText(bangumi_aera_limit_hack.getCookie('balh_server')));
        } else {
            if (bangumi_aera_limit_hack.setCookie('balh_server', argument)) {
                alert("操作成功\nCookie已經成功修改\n" + popupText(bangumi_aera_limit_hack.getCookie('balh_server')));
            } else {
                alert("操作失敗\n請檢查解除B站區域限制腳本是否正確");
            }
        }
    }

    function changeServer() {
        if (unsafeWindow.bangumi_aera_limit_hack) {
            console.log("讀取的Cookie值為:" + bangumi_aera_limit_hack.getCookie('balh_server'));
            setHost(prompt("請輸入自定義服務器地址\n清空則清除Cookie值\n按取消鍵取消操作", setDefault(bangumi_aera_limit_hack.getCookie('balh_server'))));
        } else {
            alert("執行失敗\n請確定是否已安裝並打開解除B站區域限制腳本");
        }
    }

    function GM_wait(coun) {
        if (coun < 100) {
            console.log("第" + (coun + 1) + "次測試是否加載解除B站區域限制腳本");
            if (typeof unsafeWindow.bangumi_aera_limit_hack == 'undefined') {
                console.log("還未加載解除B站區域限制腳本,延遲100毫秒");
                window.setTimeout(GM_wait(coun + 1), 100);
            } else {
                console.log("已加載解除B站區域限制腳本,設置腳本命令");
                GM_registerMenuCommand('自定義服務器地址', changeServer);
            }
        } else {
            console.log("已測試超過" + coun + "次還未加載,放棄設置腳本命令");
        }
    }
    // GM_registerMenuCommand('自定義服務器地址', changeServer);
    if (!unsafeWindow.bangumi_aera_limit_hack) {
        var bangumi_aera_limit_hack;
        console.log("還未加載解除B站區域限制腳本,設置監聽");
        Object.defineProperty(unsafeWindow, 'bangumi_aera_limit_hack', {
            configurable: true,
            enumerable: true,
            set: function(v) {
                bangumi_aera_limit_hack = v;
                console.log("正在加載解除B站區域限制腳本,設置腳本命令");
                GM_registerMenuCommand('自定義服務器地址', changeServer);
            },
            get: function() {
                return bangumi_aera_limit_hack;
            }
        });
    } else {
        console.log("已加載解除B站區域限制腳本,設置腳本命令");
        GM_registerMenuCommand('自定義服務器地址', changeServer);
    }
    // GM_wait(0);
}