Saren / nasece cloud permalink

// ==UserScript==
// @name        nasece cloud permalink
// @namespace   Saren
// @description #upload-images, #album, #url-shorten, #about, #apps, #profile
// @include     http*://na.cx*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @version     0.0.1
// ==/UserScript==
var mainFunctions = {
    "upload-images": "圖片上載",
    "album": "雲端相簿",
    "url-shorten": "網址縮短"
};

var secFunctions = {
    "about": "xhr/about",
    "apps": "xhr/apps"
};

function bindToClicks() {
    for (var i in mainFunctions) {
        $("body").on("click", "li[data-hint='" + mainFunctions[i] + "']", function (e) {
            for (var j in mainFunctions) {
                if ($(this).attr("data-hint") == mainFunctions[j]) {
                    window.location.hash = j;
                }
            }
        });
    }
    for (var i in secFunctions) {
        $("body").on("click", "a[data-twig='" + secFunctions[i] + "']", function (e) {
            for (var j in secFunctions) {
                if ($(this).attr("data-twig") == secFunctions[j]) {
                    window.location.hash = j;
                }
            }
        });
    }
    $("body").on("click", "div[data-twig='user/profile']", function (e) {
        window.location.hash = "profile";
    });
    $("body").on("click", "div[data-twig='auth/login']", function (e) {
        window.location.hash = "profile";
    });
}

function handleHash(hash) {
    if (hash in mainFunctions) {
        var trigger = setInterval(function () {
            var elem = $("li[data-hint='" + mainFunctions[hash] + "']")[0];
            if (typeof elem === 'undefined') {
                return;
            }
            $(elem).click();
            clearInterval(trigger);
        }, 10);
        return;
    }
    if (hash in secFunctions) {
        var trigger = setInterval(function () {
            var elem = $("a[data-twig='" + secFunctions[hash] + "']")[0];
            if (typeof elem === 'undefined') {
                return;
            }
            $(elem)[0].click();
            clearInterval(trigger);
        }, 10);
    }
    if (hash === "profile") {
        var trigger = setInterval(function () {
            var profile = $("div[data-twig='user/profile']")[0];
            var auth = $("div[data-twig='auth/login']")[0];
            if (typeof profile !== 'undefined') {
                $(profile)[0].click();
                clearInterval(trigger);
                return;
            }
            if (typeof auth !== 'undefined') {
                $(auth)[0].click();
                clearInterval(trigger);
                return;
            }
        }, 10);
        return;
    }
}

$(function () {
    var thisFunction = window.location.hash.substr(1);
    handleHash(thisFunction);
    bindToClicks();
});