Auto ezproxy redirection

Automatically gain access for resources subscripted by your organisation via ezproxy.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Auto ezproxy redirection
// @namespace    org.jixun
// @version      0.1
// @description  Automatically gain access for resources subscripted by your organisation via ezproxy.
// @author       Jixun
// @include      *
// @grant        GM_registerMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // In case of emergency:
    // debugger;

    // Default configs.
    // e.g. eebo.chadwyck.com, dl.acm.org
    var redirHosts = [];
    var yourDomain = GM_getValue('proxy.host', '');
    reloadHosts();

    // Register command
    if (redirHosts.indexOf(location.hostname) == -1) {
        if (location.hostname.indexOf('.exproxy.') == -1) {
            GM_registerMenuCommand('[ezproxy] Add ' + location.hostname + ' to ezproxy list.', addCurrentHost, 'z');
        }

        GM_registerMenuCommand('[ezproxy] Change domain', changeDomain, 'd');
    } else if (yourDomain) {
        // Redirect
        doRedirect();
    } else {
        GM_registerMenuCommand('[ezproxy] Setup domain', changeDomain, 'd');
    }
    GM_registerMenuCommand('[ezproxy] Reset Everything', resetScript, 'r');

    function reloadHosts () {
        try {
            redirHosts = JSON.parse(GM_getValue('site.hosts', '[]'));
        } catch (e) {
            GM_setValue('site.hosts', '[]');
            alert('Host list corrupted, reset to default empty list.');
            redirHosts = [];
        }
    }

    function addCurrentHost () {
        if (!yourDomain) {
            if (!changeDomain())
                return ;
        }

        reloadHosts();
        if (redirHosts.indexOf(location.hostname) == -1) {
            redirHosts.push(location.hostname);
            GM_setValue('site.hosts', JSON.stringify(redirHosts));
        }

        doRedirect();
    }

    function doRedirect () {
        location.hostname += '.ezproxy.' + yourDomain;
    }

    function changeDomain () {
        yourDomain = prompt('Please enter your new ezproxy domain, format as example shown below:', yourDomain || 'university.ac.uk');
        GM_setValue('proxy.host', yourDomain);
        return !!yourDomain;
    }

    function resetScript () {
        GM_setValue('site.hosts', '[]');
        GM_setValue('proxy.host', '');
    }
})();