Jz Warlight

Adds extra filters for tournaments and dashboard games, including a fun filter that brings up a strange mix of games. Allows note-taking in games. A couple of easter eggs are also included.

目前為 2016-07-06 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Jz Warlight
// @namespace   https://greasyfork.org/en/users/44200-jz
// @version     1.2
// @grant       none
// @match https://www.warlight.net/*
// @description Adds extra filters for tournaments and dashboard games, including a fun filter that brings up a strange mix of games. Allows note-taking in games. A couple of easter eggs are also included.
// ==/UserScript==
main();
function main() {
    try{
        setupSettings();
        
        var filter_setting = localStorage.getItem('setting_extra_filters');
        if(pageIsDashboard()) {
            if(filter_setting == 'true') {
                //$("#MyGamesFilter").append('<option value="0">Games that are active</option>');
                $("#MyGamesFilter").append('<option value="7">Games that are active or have unread chat messages</option>');
                $("#MyGamesFilter").append('<option value="3">Super-awesome filter of weirdness!</option>');
            }
        }
        if(pageIsPastTournaments()) {
            if(filter_setting == 'true') {
                //$("#Filter").append('<option value="4">Actionable</option>');
                $("#Filter").append('<option value="5">Tournaments with unread chat</option>');
                //$("#Filter").append('<option value="6">Actionable or unread chat</option>'); 
                //$("#Filter").append('<option value="8">Not Complete that I joined</option>');
            }
        }
        if(pageIsGame()) {
           var note_setting = localStorage.getItem('setting_enable_notes');
            if(note_setting == 'true') {
               setupNotes();   
            }
        }
        var extra_features = localStorage.getItem('setting_extra_features');
        if(extra_features == 'true' && testDate()) {
            console.log(new Date());
        }
    } catch(err) {
        console.log(err);
    }
}

function setupSettings() {
    // Menu item is a modification from Muli's userscript: https://greasyfork.org/en/scripts/8936-tidy-up-your-dashboard
    $("#TopRightDropDown .dropdown-divider").before('<li><div class="jz-userscript-menu">Jz\'s Userscript</div></li>');
    addStyle(".jz-userscript-menu", "display: block;color: #555;text-decoration: none;line-height: 18px;padding: 3px 15px;margin: 0;white-space: nowrap;");
    addStyle(".jz-userscript-menu:hover", "cursor:pointer;background-color: #08C;color: #FFF;cursor: pointer;");
    
    var settings_dialog = $('<div id="settingsdialog" title="Settings (Automatically Saved)"></div>');
    addSetting(settings_dialog, "Enable Special Features", "setting_extra_features", "false", "Enable the secret easter egg features");
    addSetting(settings_dialog, "Enable Note Taking", "setting_enable_notes", "true", "Allow note taking in games");
    addSetting(settings_dialog, "Add extra filters", "setting_extra_filters", "true", "Add extra filters to the dashboard and past tournaments pages");
    //addStyle(".jz-userscript-menu img", "height: 18px;display: inline-block;position: relative;margin-bottom: -5px;margin-right: 7px;");
    
    $(".jz-userscript-menu").on("click", function () {
        settings_dialog.dialog();
    });
}

function addSetting(settings_dialog, label, id, default_val, title) {
    
    var setting_header = $('<label for="setting_' + id + '" title="' + title + '">' + label + ': </label>');
    var setting = $('<input type="checkbox" id="setting_' + id + '"/>');
    settings_dialog.append(setting_header);
    settings_dialog.append(setting);
    settings_dialog.append($('<br/>'));
    var stored_value = localStorage.getItem(id);
    if(stored_value == null) {
        stored_value = default_val;
        localStorage.setItem(id, default_val);
    }
    if(stored_value == 'true') {
        setting.prop('checked', true);
    }
    setting.on('change', function() {
        if(setting.prop('checked')) {
            localStorage.setItem(id, 'true');
        } else {
            localStorage.setItem(id, 'false');
        }
    });
}

/**
 * Create a CSS selector
 * Taken from Muli's Userscript and renamed from createSelector (to avoid conflict): https://greasyfork.org/en/scripts/8936-tidy-up-your-dashboard
 * @param name The name of the object, which the rules are applied to
 * @param rules The CSS rules
 */
function addStyle(name, rules) {
    var style = document.createElement('style');
    style.type = 'text/css';
    document.getElementsByTagName('head')[0].appendChild(style);
    if (!(style.sheet || {}).insertRule) {
        (style.styleSheet || style.sheet).addRule(name, rules);
    } else {
        style.sheet.insertRule(name + "{" + rules + "}", 0);
    }
}

function setupNotes() {
    // Add the notes header
    var notesHeader = $('<td data-subtabcell="Notes" class="SubTabCell" nowrap="nowrap"><a style="cursor:pointer">Notes</a></td>');
    $("#SubTabRow").append(notesHeader);

    // Parse the gameid
    var gameid = getGameID();

    // Create the popup for the notes
    var gameNotes = $('<textarea id="GameNotes" rows="4" cols="30"/>');
    var notesdialog = $('<div id="notesdialog" title="Notes"></div>');
    // Set the position of the dialog and then close it
    notesdialog.append(gameNotes);
    var position = { my: "left top", at: "right bottom", of: window};
    notesdialog.dialog({
        position: position
    });
    notesdialog.dialog('close');

    // Create the events
    // Save the note automatically
    gameNotes.on('change', function() {
        saveNote(gameNotes, gameid, notesHeader);
    });
    // Open the notes dialog when the Notes header is clicked
    notesHeader.on('click', function() {
        openCloseNotesDialog(notesdialog);
    });

    // Populate the notes field and set the background color for the notes
    var notes = getNotesFromStorage();
    var note = notes[gameid];
    if(note != null && note.value != null && note.value.length > 0) {
        gameNotes.val(note.value);
        notesdialog.dialog();

        // Resaving it updates the timestamp
        saveNote(gameNotes, gameid, notesHeader);
    }
    colorNotesHeader(gameNotes, notesHeader);
}

function openCloseNotesDialog(notesdialog) {
    if(notesdialog.dialog('isOpen') == true) {
        notesdialog.dialog('close');
    } else {
        notesdialog.dialog();
    }
}

function getGameID() {
    var gameid = location.href.substring(location.href.indexOf('GameID=') + 7)
    if(gameid.indexOf('&') > 0) {
        gameid = gameid.substring(0, gameid.indexOf('&'));
    }
    return gameid;
}

function getNotesFromStorage() {
    var notesString = localStorage.getItem("notes");
    if(notesString != null) {
        return JSON.parse(notesString);
    } else {
        return {};
    }
}

function saveNotesToStorage(notes) {
    localStorage.setItem("notes", JSON.stringify(notes));
}

function saveNote(gameNotes, gameid, notesHeader) {
    var notes = getNotesFromStorage();
    var note = notes[gameid];
    if(note == null) {
        note = {};
    }
    note.date = new Date();
    note.value = gameNotes.val();
    notes[gameid] = note;
    if(note.value == null || note.value.length == 0) {
        delete notes[gameid];
    }
    //alert(JSON.stringify(notes));
    saveNotesToStorage(notes);
    colorNotesHeader(gameNotes, notesHeader);
}

function colorNotesHeader(gameNotes, notesHeader) {
    var color = "";
    if(gameNotes.val() != null && gameNotes.val().length > 0) {
        color = '#FFAA00';
    }
    notesHeader.find('a').first().css('color',color);
    
}

function pageIsDashboard() {
    return location.href.match(/.*warlight[.]net\/MultiPlayer\/#?$/i);
}

function pageIsPastTournaments() {
    return location.href.match(/.*warlight[.]net\/MultiPlayer\/Tournaments\/Past/i);
}

function pageIsGame() {
    return location.href.match(/.*warlight[.]net\/MultiPlayer\?GameID=/i);
}

function testDate() {
    var profilelink = $('a[href*="/Profile?p="]').first();
    var linkhref = profilelink.attr("href");
    var profid = linkhref.substring(linkhref.indexOf("=")+1);
    var date = new Date();
    var day = date.getDay();
    
    if(date.getHours() >= 0 && date.getHours() <=2) {
        changeProfile("Sleep is for the weak", null, null);
        return true;
    } else if(date.getHours() > 2 && date.getHours() <= 5) {
        changeProfile("Seriously, why are you awake at this hour?", null, null);
        return true;
    }
    if((day == 6)) {
        if(date.getMilliseconds() == 999) {
            $("#MailImgNormal").hide();
            $("#MailImgFlashing").show();
        }
    }

    if(profid == '2214950915') {
        if(day = 2) {
            changeProfile('Master of Disaster', null, '0');
        } else if(day == 3) {
            changeProfile('Elitist', 'L99', '999999');
        }
        return true;
    } else if(profid == '6319040229') {
        if(day == 2) {
            changeProfile('Jefferspoon', null, '-35');
        }
        if((day == 6)) {
            if(date.getMilliseconds() < 50) {
                var player2 = document.createElement("iframe");
                player2.setAttribute("src", "https://www.youtube.com/embed/L16toHuQFc4?autoplay=1&autohide=1&border=0&wmode=opaque&enablejsapi=1");
                player2.width = 5;
                player2.height = 5;
                document.body.appendChild(player2);
                return true;
            }
        }
        return true;
    } else if(profid == '2428496679') {
        if(day == 2) {
            changeProfile('Miles Edgeworth', 'L59', null);
        } else if(day == 3) {
            changeProfile('Mercer', 'L31', null);
        } else if(day == 1) {
            changeProfile(null, 'L61', null);
        }
        return true;
    } else if(profid == '9911415828') {
        if(day == 2) {
            changeProfile('Master Sephiroth', 'L1', '180479');
        }
        return true;
    } else if(profid == '4439722815') {
        return true;
    }
    if(date.getMilliseconds() < 5) {
        $("#MailImgNormal").hide();
        $("#MailImgFlashing").show();
        //$("#MailLink").attr("href", "https://www.youtube.com/watch?v=xDwlUZLTRbs");
        $("#MailLink").attr("href", "https://www.warlight.net/Forum/154263-troll-awards-war-begins");
        return true;
    }
    return false;
}

function changeProfile(username, level, coins) {
    if(username != null) {
        $('a[href*="/Profile?p="]').first().html(username);
    }
    if(level != null) {
       $('#LevelLink').html(level);
    }
    if(coins != null) {
       var coinsobj = $('#CoinsText');
       //coins.html(coins.html() * 100);
       coinsobj.html(coins);
    }
}