Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Nested ZSR Navi Menu
- // @namespace http://cloudmodding.com/
- // @version 0.5
- // @description A ZSR Navi menu modification that adds nesting for the various header levels. The script also contain a Plus version with additional features that're listed bellow. Disable it by removing the function call in the init function.
- // @author CloudMax
- // @match http://zeldaspeedruns.com/*
- // @match www.zeldaspeedruns.com/*
- // @copyright 2012+, CloudMax
- // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js
- // ==/UserScript==
- //Changelong////////////////////////////////////////////////////////////////////////////////////////////////////////
- //0.5
- //The script now run when visiting the website using www
- //Added a validation so that the script only run if you're in the Games section of ZSR
- //The Navi Menu can no longer appear in a game sections front page
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Screenshot Album//////////////////////////////////////////////////////////////////////////////////////////////////
- // http://imgur.com/a/odWG8
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- *This script was created for Tampermonkey(Chrome)/Greasemonkey(Firefox)
- *The nested unordered lists that are generated gets a class applied to them by the name of naviSub<headerNumber> so that each level can be styled. (it is also used for the generating process to find the last suitable list)
- *By clicking on the yellow triforce symbols you can show/hide nested lists.
- *A magin/padding trick has been applied to all page content headers (this is normally only applied to h2 headers) so that when you go to a specific anchor, the header will also be displayed.
- */
- //Plus Features/////////////////////////////////////////////////////////////////////////////////////////////////////
- //Enables the Navi Menu on all pages. You can set the required amount of headers for the Navi Menu to appear.
- //Changes the Navi Menu title to "Navigation+" to indicate that the plus features are enabled.
- //The Navi Menu width is dynamic so that it can become slightly wider if the list items doesn't fit.
- //If a page displays 2 h1 headers at the top of the page, the header with the page name will be removed.
- //Wraps a link around the game logo in the upper left so that it takes you to the specified games main page.
- //The Triforce font doesn't have a % symbol, so it is replaced with "Percent" in headers. Ex: "Any%" to "AnyPercent" (DISABLED)
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Settings//////////////////////////////////////////////////////////////////////////////////////////////////////////
- var defLvl = 2, //All headers with a higher value will be hidden in the Navi Menu by default.
- nestLimit = 3, //How many header levels the menu will nest.
- minHeaders = 3, //The amount of headers that must be available for the Navi Menu to appear. (Plus feature)
- pathSections = location.pathname.replace(/\/+$/, "").split('/'), //Used for page check and when generating links
- headers = ''; //This variable will store the header selector using for the loop.
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //creates the header selector
- for (var i = 1; i <= nestLimit; i++) {
- headers += 'h' + i + ',';
- }
- headers = headers.slice(0, headers.length - 1); //Removes the last comma
- var NaviMenu = {
- init: function () {
- if ($("li.active > a:contains('Games')").length > 0) { //By checking if the Games dropdown in the navbar is active, we can validate whether or not you're visiting a game page.
- NaviMenu.activateMenuPlus(); //Enables all the plus features.
- if ($('#navi').length > 0) { //Make sure that there is a Navi Menu before generating the content.
- NaviMenu.createLinks();
- }
- }
- },
- createLinks: function () {
- $('#navi ul').remove(); //Clear the Navi Menu in case one has already been generated.
- var ul = $('#navi').append('<ul></ul>').find('ul');
- $('#pagecontent').children(headers).each(function () {
- var self = $(this),
- txt = self.text(),
- tag = self.prop("tagName").toLowerCase();
- if (tag == "h1") {
- var parent = ul; //The header is a h1, so it will be placed in the root ul
- } else {
- /*Old code. This one breaks if the page headers are set up in an odd manner (if the author didn't properly nest the headers, skipping a level.)
- var parent = ul.find('.naviSub' + parseInt(tag.substring(1)) + ':last'); //It is a sub-header, so it will be placed in the first list with the proper nesting level
- */
- var subLists = '';
- for (var i = 1; i <= parseInt(tag.substring(1)); i++) { //Loop through and create a .naviSub selector list for all the values that're lower than the current one
- subLists += '.naviSub' + i + ',';
- }
- subLists = subLists.slice(0, subLists.length - 1); //Removes the last comma
- var parent = ul.find(subLists).last(); //Use the generated selector to find the last list that has a lower nesting level.
- }
- //Create the List Item
- var li = parent.append('<li><span class="bullet"></span><a href="#' + NaviMenu.slug(txt) + '">' + txt + '</a></li>').find('li:last');
- li.append('<ul class="naviSub' + (parseInt(tag.substring(1)) + 1) + '" style="margin-left:10px;list-style-type: none;"></ul>');
- //Hides the nested list if the nesting level is higher than the specified default level
- if (parseInt(tag.substring(1)) + 1 > defLvl)
- li.find('ul:last').hide()
- //Generates the anchor ID for the header element
- $(this).attr("id", NaviMenu.slug(txt));
- });
- //Remove the main page header in the Navi Menu if it isn't used.
- if ($('#pagecontent > h1:first').next().prop("tagName").toLowerCase() == "h1") {
- ul.find('li:first').remove();
- }
- //Remove all the empty lists.
- ul.find('ul:empty').remove();
- //Adds a nested class to all li elements with a list in them so that CSS can be applied to them easily
- ul.find('ul').parent().addClass('nested');
- //Set the Triforce color of list items with a nested menu to yellow, and those without one to SandyBrown. This gives a visual indication that you can toggle an item (the yellow ones)
- $('li:not(.nested) > .bullet').css('color', 'SandyBrown');
- $('.nested > .bullet').css('color', 'Yellow');
- //Set the cursor to pointer for the items that can be toggled (the yellow ones)
- $('.nested > .bullet').css('cursor', 'pointer');
- //Applies the padding/margin-top trick to all page content headers so that when you use the navi menu, you'll scroll to the top of the header, instead of bellow it.
- $('#pagecontent').children(headers).css({
- "padding-top": "44px",
- "margin-top": "-44px"
- });
- //Allows you to toggle nested menues by clicking on the yellow triforce symbols that are displayed.
- $('#navi span').click(function (e) {
- $(this).siblings('ul:last').slideToggle();
- });
- },
- slug: function (txt) { //I took this from the original code. Generates a anchor friendly path out of the header text
- return txt.toLowerCase().replace(/[^\w|\s]/g, '').replace(/[ _]+/g, '-');
- },
- activateMenuPlus: function () {
- //Enables the Navi Menu on all pages which normally doesn't have one that has the required amount of headers, except for the game front page.
- if ($('#navi').length == 0 && $('#pagecontent').children(headers).length >= minHeaders && pathSections.length >= 4) {
- $('#pagecontent').prepend('<div id="navi" class="navi"/>');
- $('#navi').append('<h1> </h1>')
- .css({
- "min-height": "57px",
- "width": "62px"
- });
- }
- //Changes the Navi Menu title to "Navigation+". (Indicates that the plus features are enabled)
- $('#navi h1').text('Navigation+');
- //The Navi Menu width is modified so that it can become slightly wider if the list items doesn't fit.
- $('#navi').css({
- "min-width": "230px",
- "max-width": "300px",
- "width": "auto"
- });
- //If a page displays 2 h1 headers at the top of the page, the header with the page name will be removed.
- if ($('#pagecontent > h1:first').next().prop("tagName").toLowerCase() == "h1") {
- $('#pagecontent > h1:first').remove();
- }
- //Wraps a link around the game logo in the upper left that takes you to the specified games main game.
- $(".game-logo > img").wrap('<a href="/' + pathSections[1] + '" />');
- //Replaces the "%" symbol in all h1 headers with "Percent" as the Triforce font doesn't have a % symbol.
- /*
- $("h1").each(function () {
- $(this).text($(this).text().replace('%', 'Percent'));
- });
- */
- }
- };
- NaviMenu.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement