Auto ezproxy redirection

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

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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', '');
    }
})();