Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name New GMod Wiki edits
- // @namespace https://pastebin.com/raw/WxTS7j2F
- // @version 0.2
- // @description Personalize the new GMod wiki
- // @author Thomasims
- // @match https://wiki.facepunch.com/gmod/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- let options = {
- push_references_up: true, // Reorders categories to prioritize Developer References
- reorder_references: true, // Reorders Developer References sub categories
- custom_search: true, // Adds realm:server|client|menu to the search
- fix_auto_scroll: true, // Fix the auto scroll on page load (broken from reordering panels)
- show_fullnames: true, // Replaces all names by the full library + function name
- sticky_headers: true, // Make headers in the left panel stick to the top of the list
- //extract_global: false, // removed: Officially added
- //custom_search_max_results: 250, // removed: Officially added
- //keep_search: false, // removed: Officially added
- };
- let contents = document.getElementById("contents");
- let [play_title, play_section, dev_title, dev_section, ref_title, ref_section] = contents.children;
- if ( options.push_references_up ) { // References > Dev Tutorials > Game Tutorials
- contents.prepend(dev_section); contents.prepend(dev_title);
- contents.prepend(ref_section); contents.prepend(ref_title);
- }
- 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;
- if ( options.reorder_references ) { // Hooks > Libraries > Global > ...
- ref_section.prepend(ref_s_global);
- ref_section.prepend(ref_s_libraries);
- ref_section.prepend(ref_s_hooks);
- }
- if ( options.fix_auto_scroll ) {
- let active = document.getElementById("sidebar").getElementsByClassName("active")[0];
- if ( active ) {
- contents.scrollTop = contents.scrollTop + active.getBoundingClientRect().y - document.body.clientHeight / 2;
- }
- }
- let style = document.createElement("style");
- style.innerText = `
- #sidebar details.level2 > ul > li > a {
- white-space: nowrap;
- }
- `;
- if ( options.sticky_headers ) {
- style.innerText += `
- #sidebar details.level1[open] > summary > div {
- z-index: 10;
- background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
- background-attachment: fixed;
- background-size: cover;
- background-position: center top;
- background-blend-mode: multiply;
- background-color: rgba(0,0,0,.2) !important;
- border-top-left-radius: 4px;
- border-top-right-radius: 4px;
- }
- .level1[open] > summary {
- position: sticky;
- top: 0;
- z-index: 9;
- background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
- background-attachment: fixed;
- background-size: cover;
- background-position: center top;
- background-blend-mode: multiply;
- }
- .level2[open] > summary {
- position: sticky;
- top: 38px;
- z-index: 8;
- background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
- background-attachment: fixed;
- background-size: cover;
- background-position: center top;
- background-blend-mode: multiply;
- background-color: rgba(0,0,0,.3);
- }
- .level3[open] > summary {
- position: sticky;
- top: 61px;
- z-index: 7;
- background-image: url(https://files.facepunch.com/garry/64338533-c1c7-47ad-bf1e-73cefab12684.png);
- background-attachment: fixed;
- background-size: cover;
- background-position: center top;
- background-blend-mode: multiply;
- background-color: rgba(0,0,0,.2);
- }
- #topbar > .search {
- padding-bottom: 8px;
- }
- #sidebar .sectionheader:first-child {
- margin-top: 0;
- }
- `;
- }
- document.head.append(style);
- if ( options.show_fullnames ) {
- let delay = (ms) => new Promise((s) => setTimeout(s,ms));
- (async ()=>{
- let i = 0;
- for (let func of ref_section.getElementsByTagName("a")) {
- let s = func.getAttribute("search");
- if ( s && s != "" ) {
- func.title = s;
- func.innerText = s;
- }
- if (++i%100 == 0) await delay(50);
- }
- })();
- }
- if ( options.custom_search ) {
- window.UpdateSearch = function(limitResults = true) {
- if (limitResults)
- MaxResultCount = 100;
- else
- MaxResultCount = 2000;
- var child = SearchResults.lastElementChild;
- while (child) {
- SearchResults.removeChild(child);
- child = SearchResults.lastElementChild;
- }
- var string = SearchInput.value;
- if (string.length < 1) {
- SidebarContents.classList.remove("searching");
- SearchResults.classList.remove("searching");
- var sidebar = document.getElementById("sidebar");
- var active = sidebar.getElementsByClassName("active");
- if (active.length == 1) {
- active[0].scrollIntoView({ block: "center" });
- }
- return;
- }
- SidebarContents.classList.add("searching");
- SearchResults.classList.add("searching");
- ResultCount = 0;
- Titles = [];
- TitleCount = 0;
- SectionHeader = null;
- let userealms = false; // Extract 'realm:...' out of the search
- let realms = { server: false, client: false, menu: false };
- var parts = string.split(' ');
- var q = "";
- for (var i in parts) {
- if (parts[i].length < 1)
- continue;
- if ( parts[i].startsWith("realm:") ) {
- userealms = true;
- for ( let realm of parts[i].substring(6).split(/[\/|:;,-_]/) )
- realms[realm] = true;
- } else {
- var t = parts[i].replace(/([^a-zA-Z0-9_-])/g, "\\$1");
- q += ".*(" + t + ")";
- }
- }
- q += ".*";
- var regex = new RegExp(q, 'gi');
- SearchRecursive(regex, SidebarContents, userealms ? realms : null);
- if (limitResults && ResultCount > MaxResultCount) {
- var moreresults = document.createElement('a');
- moreresults.href = "#";
- moreresults.classList.add('noresults');
- moreresults.innerHTML = (ResultCount - 100) + ' more results - show more?';
- moreresults.onclick = (e) => { UpdateSearch(false); return false; };
- SearchResults.append(moreresults);
- }
- if (SearchResults.children.length == 0) {
- var noresults = document.createElement('span');
- noresults.classList.add('noresults');
- noresults.innerText = 'no results';
- SearchResults.appendChild(noresults);
- }
- }
- function validate(element, realms) {
- if (!realms) return true;
- if ( (realms.server || realms.sv || realms.s) && !element.classList.contains('rs') ) return false;
- if ( (realms.client || realms.cl || realms.c) && !element.classList.contains('rc') ) return false;
- if ( (realms.menu || realms.mn || realms.m) && !element.classList.contains('rm') ) return false;
- return true;
- }
- window.SearchRecursive = function(str, el, realms) {
- var title = null;
- if (el.children.length > 0 && el.children[0].tagName == "SUMMARY") {
- title = el.children[0].children[0];
- Titles.push(title);
- TitleCount++;
- }
- var children = el.children;
- for (var i = 0; i < children.length; i++) {
- var child = children[i];
- if (child.className == "sectionheader")
- SectionHeader = child;
- if (child.tagName == "A") {
- if (child.parentElement.tagName == "SUMMARY")
- continue;
- var txt = child.getAttribute("search");
- if (txt != null && validate(child,realms)) {
- var found = txt.match(str);
- if (found) {
- if (ResultCount < MaxResultCount) {
- AddSearchTitle();
- var copy = child.cloneNode(true);
- copy.onclick = e => Navigate.ToPage(copy.href, true);
- copy.classList.add("node" + TitleCount);
- SearchResults.appendChild(copy);
- }
- ResultCount++;
- }
- }
- }
- SearchRecursive(str, child, realms);
- }
- if (title != null) {
- TitleCount--;
- if (Titles[Titles.length - 1] == title) {
- Titles.pop();
- }
- }
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment