Thomasims

New GMod Wiki edits

Jan 29th, 2020
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         New GMod Wiki edits
  3. // @namespace    https://pastebin.com/raw/WxTS7j2F
  4. // @version      0.2
  5. // @description  Personalize the new GMod wiki
  6. // @author       Thomasims
  7. // @match        https://wiki.facepunch.com/gmod/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     let options = {
  15.         push_references_up: true, // Reorders categories to prioritize Developer References
  16.         reorder_references: true, // Reorders Developer References sub categories
  17.         custom_search: true, // Adds realm:server|client|menu to the search
  18.         fix_auto_scroll: true, // Fix the auto scroll on page load (broken from reordering panels)
  19.         show_fullnames: true, // Replaces all names by the full library + function name
  20.         sticky_headers: true, // Make headers in the left panel stick to the top of the list
  21.  
  22.         //extract_global: false, // removed: Officially added
  23.         //custom_search_max_results: 250, // removed: Officially added
  24.         //keep_search: false, // removed: Officially added
  25.     };
  26.  
  27.     let contents = document.getElementById("contents");
  28.     let [play_title, play_section, dev_title, dev_section, ref_title, ref_section] = contents.children;
  29.  
  30.     if ( options.push_references_up ) { // References > Dev Tutorials > Game Tutorials
  31.         contents.prepend(dev_section); contents.prepend(dev_title);
  32.         contents.prepend(ref_section); contents.prepend(ref_title);
  33.     }
  34.  
  35.     let [ref_s_global, ref_s_classes, ref_s_libraries, ref_s_hooks, ref_s_panels, ref_s_enums, ref_s_structs, ref_s_shaders] = ref_section.children;
  36.  
  37.     if ( options.reorder_references ) { // Hooks > Libraries > Global > ...
  38.         ref_section.prepend(ref_s_global);
  39.         ref_section.prepend(ref_s_libraries);
  40.         ref_section.prepend(ref_s_hooks);
  41.     }
  42.  
  43.     if ( options.fix_auto_scroll ) {
  44.         let active = document.getElementById("sidebar").getElementsByClassName("active")[0];
  45.         if ( active ) {
  46.             contents.scrollTop = contents.scrollTop + active.getBoundingClientRect().y - document.body.clientHeight / 2;
  47.         }
  48.     }
  49.  
  50.     let style = document.createElement("style");
  51.     style.innerText = `
  52. #sidebar details.level2 > ul > li > a {
  53.     white-space: nowrap;
  54. }
  55. `;
  56.  
  57.     if ( options.sticky_headers ) {
  58.         style.innerText += `
  59. #sidebar details.level1[open] > summary > div {
  60.     z-index: 10;
  61.     background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
  62.     background-attachment: fixed;
  63.     background-size: cover;
  64.     background-position: center top;
  65.     background-blend-mode: multiply;
  66.     background-color: rgba(0,0,0,.2) !important;
  67.     border-top-left-radius: 4px;
  68.     border-top-right-radius: 4px;
  69. }
  70. .level1[open] > summary {
  71.     position: sticky;
  72.     top: 0;
  73.     z-index: 9;
  74.     background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
  75.     background-attachment: fixed;
  76.     background-size: cover;
  77.     background-position: center top;
  78.     background-blend-mode: multiply;
  79. }
  80. .level2[open] > summary {
  81.     position: sticky;
  82.     top: 38px;
  83.     z-index: 8;
  84.     background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
  85.     background-attachment: fixed;
  86.     background-size: cover;
  87.     background-position: center top;
  88.     background-blend-mode: multiply;
  89.     background-color: rgba(0,0,0,.3);
  90. }
  91. .level3[open] > summary {
  92.     position: sticky;
  93.     top: 61px;
  94.     z-index: 7;
  95.     background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
  96.     background-attachment: fixed;
  97.     background-size: cover;
  98.     background-position: center top;
  99.     background-blend-mode: multiply;
  100.     background-color: rgba(0,0,0,.2);
  101. }
  102. #topbar > .search {
  103.     padding-bottom: 8px;
  104. }
  105. #sidebar .sectionheader:first-child {
  106.     margin-top: 0;
  107. }
  108. `;
  109.     }
  110.  
  111.     document.head.append(style);
  112.  
  113.     if ( options.show_fullnames ) {
  114.         let delay = (ms) => new Promise((s) => setTimeout(s,ms));
  115.         (async ()=>{
  116.             let i = 0;
  117.             for (let func of ref_section.getElementsByTagName("a")) {
  118.                 let s = func.getAttribute("search");
  119.                 if ( s && s != "" ) {
  120.                     func.title = s;
  121.                     func.innerText = s;
  122.                 }
  123.                 if (++i%100 == 0) await delay(50);
  124.             }
  125.         })();
  126.     }
  127.  
  128.     if ( options.custom_search ) {
  129.         window.UpdateSearch = function(limitResults = true) {
  130.             if (limitResults)
  131.                 MaxResultCount = 100;
  132.             else
  133.                 MaxResultCount = 2000;
  134.             var child = SearchResults.lastElementChild;
  135.             while (child) {
  136.                 SearchResults.removeChild(child);
  137.                 child = SearchResults.lastElementChild;
  138.             }
  139.             var string = SearchInput.value;
  140.             if (string.length < 1) {
  141.                 SidebarContents.classList.remove("searching");
  142.                 SearchResults.classList.remove("searching");
  143.                 var sidebar = document.getElementById("sidebar");
  144.                 var active = sidebar.getElementsByClassName("active");
  145.                 if (active.length == 1) {
  146.                     active[0].scrollIntoView({ block: "center" });
  147.                 }
  148.                 return;
  149.             }
  150.             SidebarContents.classList.add("searching");
  151.             SearchResults.classList.add("searching");
  152.             ResultCount = 0;
  153.             Titles = [];
  154.             TitleCount = 0;
  155.             SectionHeader = null;
  156.             let userealms = false; // Extract 'realm:...' out of the search
  157.             let realms = { server: false, client: false, menu: false };
  158.             var parts = string.split(' ');
  159.             var q = "";
  160.             for (var i in parts) {
  161.                 if (parts[i].length < 1)
  162.                     continue;
  163.                 if ( parts[i].startsWith("realm:") ) {
  164.                     userealms = true;
  165.                     for ( let realm of parts[i].substring(6).split(/[\/|:;,-_]/) )
  166.                         realms[realm] = true;
  167.                 } else {
  168.                     var t = parts[i].replace(/([^a-zA-Z0-9_-])/g, "\\$1");
  169.                     q += ".*(" + t + ")";
  170.                 }
  171.             }
  172.             q += ".*";
  173.             var regex = new RegExp(q, 'gi');
  174.             SearchRecursive(regex, SidebarContents, userealms ? realms : null);
  175.             if (limitResults && ResultCount > MaxResultCount) {
  176.                 var moreresults = document.createElement('a');
  177.                 moreresults.href = "#";
  178.                 moreresults.classList.add('noresults');
  179.                 moreresults.innerHTML = (ResultCount - 100) + ' more results - show more?';
  180.                 moreresults.onclick = (e) => { UpdateSearch(false); return false; };
  181.                 SearchResults.append(moreresults);
  182.             }
  183.             if (SearchResults.children.length == 0) {
  184.                 var noresults = document.createElement('span');
  185.                 noresults.classList.add('noresults');
  186.                 noresults.innerText = 'no results';
  187.                 SearchResults.appendChild(noresults);
  188.             }
  189.         }
  190.         function validate(element, realms) {
  191.             if (!realms) return true;
  192.             if ( (realms.server || realms.sv || realms.s) && !element.classList.contains('rs') ) return false;
  193.             if ( (realms.client || realms.cl || realms.c) && !element.classList.contains('rc') ) return false;
  194.             if ( (realms.menu   || realms.mn || realms.m) && !element.classList.contains('rm') ) return false;
  195.             return true;
  196.         }
  197.         window.SearchRecursive = function(str, el, realms) {
  198.             var title = null;
  199.             if (el.children.length > 0 && el.children[0].tagName == "SUMMARY") {
  200.                 title = el.children[0].children[0];
  201.                 Titles.push(title);
  202.                 TitleCount++;
  203.             }
  204.             var children = el.children;
  205.             for (var i = 0; i < children.length; i++) {
  206.                 var child = children[i];
  207.                 if (child.className == "sectionheader")
  208.                     SectionHeader = child;
  209.                 if (child.tagName == "A") {
  210.                     if (child.parentElement.tagName == "SUMMARY")
  211.                         continue;
  212.                     var txt = child.getAttribute("search");
  213.                     if (txt != null && validate(child,realms)) {
  214.                         var found = txt.match(str);
  215.                         if (found) {
  216.                             if (ResultCount < MaxResultCount) {
  217.                                 AddSearchTitle();
  218.                                 var copy = child.cloneNode(true);
  219.                                 copy.onclick = e => Navigate.ToPage(copy.href, true);
  220.                                 copy.classList.add("node" + TitleCount);
  221.                                 SearchResults.appendChild(copy);
  222.                             }
  223.                             ResultCount++;
  224.                         }
  225.                     }
  226.                 }
  227.                 SearchRecursive(str, child, realms);
  228.             }
  229.             if (title != null) {
  230.                 TitleCount--;
  231.                 if (Titles[Titles.length - 1] == title) {
  232.                     Titles.pop();
  233.                 }
  234.             }
  235.         }
  236.     }
  237.  
  238. })();
Advertisement
Add Comment
Please, Sign In to add comment