User:Ilikecomputers/vector-2022.js

Everything About Fiction You Never Wanted to Know.
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Google Chrome / Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
//Test: turn pink text black on Heartwarming pages.
if (document.title.includes('Heartwarming')) {
    document.body.style = 'color:#202122 !important';
}
//some options on the left of the screen will be hidden in the vector 2022 skin. Expand and show them here.
//update: looks like this bug got resolved
//document.querySelectorAll('#mw-panel .vector-menu-content').forEach(function (e) {
//    e.style.display = "block";
//});
//Disallows the usage of the Visual Editor, because it gets in the way of more experienced editors
//if (document.getElementById('ca-ve-edit')) document.getElementById('ca-ve-edit').innerHTML = '';

//adds a special "useful pages" section to the sidebar, alongside other things like "Troping Tools" and "Troper Social Networks"
function usefulPagesSidebar() {
    var commonPages = [
        ['Style Guide', 'https://allthetropes.org/wiki/All_The_Tropes:Style_Guide'],
        ['Personal Sandbox', 'https://www.allthetropes.org/wiki/User:Ilikecomputers/sandbox'],
        ['New Files Gallery', 'https://allthetropes.org/wiki/Special:NewFiles'],
        ['Active users list', "https://allthetropes.org/wiki/Special:ActiveUsers"],
        ['Statistics', 'https://allthetropes.org/wiki/Special:Statistics'],
        ['Pages with Most Revisions', 'https://allthetropes.org/wiki/Special:MostRevisions']]; //common pages
    var usefulPagesDiv = document.createElement('div');

    var subDiv = document.createElement('div');

    var heading = document.createElement('span');
    subDiv.classList.add('vector-menu-heading');
    subDiv.appendChild(heading);
    heading.textContent = 'Useful Pages';
    usefulPagesDiv.appendChild(subDiv);

    var listingItemsDiv = document.createElement('div');
    listingItemsDiv.classList = 'vector-menu-content';
    usefulPagesDiv.appendChild(listingItemsDiv);

    var unorderedList = document.createElement('ul');
    unorderedList.classList = 'vector-menu-content-list';
    listingItemsDiv.appendChild(unorderedList);

    for (var e in commonPages) {
        e = commonPages[e];
        var listElement = document.createElement('li');
        listElement.classList = 'mw-list-item';
        var aElement = document.createElement('a');
        listElement.appendChild(aElement);

        aElement.innerText = e[0];
        aElement.href = e[1];
        aElement.title = e[0];

        unorderedList.appendChild(listElement);
    }

    usefulPagesDiv.classList = 'vector-menu mw-portvar vector-menu-portal portal expanded';
    document.querySelector('#p-navigation').after(usefulPagesDiv);
}
usefulPagesSidebar();

//keep track of user's most visited pages, accessed when they click an internal link. search function does NOT work with this.
function mostVisitedPages() {
    function findLargest(obj, times) {
        var largest = 0;
        var largestKey = '';
        var largestDisplay = '';
        var returnArr = [];
        var tempArr = [];
        for (var key in obj) {
            var tempObj = obj[key];
            tempObj.key = key;
            tempArr.push(tempObj);
        }
        tempArr.sort(function (a, b) {return b.visitedTimes - a.visitedTimes;});
        return tempArr.slice(0, times);
    }
    
    var bigContainerDiv = document.createElement('div');
    document.getElementById('p-navigation').after(bigContainerDiv);
    bigContainerDiv.classList = 'vector-menu mw-portvar vector-menu-portal portal collapsed';
    bigContainerDiv.id = "mostVisitedPagesContainer";
    
    var titleDiv = document.createElement('div');
    bigContainerDiv.appendChild(titleDiv);
    titleDiv.classList = 'vector-menu-heading';
    
    var sectionHeader = document.createElement('span');
    sectionHeader.attributes.href = '#';
    titleDiv.appendChild(sectionHeader);
    
    var span = document.createElement('span');
    span.classList = "vector-menu-heading-label";
    span.textContent = "Most visited pages";
    sectionHeader.appendChild(span);
    
    var mainContent = document.createElement('div');
    mainContent.classList.add('vector-menu-content');
    bigContainerDiv.appendChild(mainContent);
    
    var mostVisitedUl = document.createElement('ul');
    mostVisitedUl.classList = 'vector-menu-content-list';
    mainContent.appendChild(mostVisitedUl);
    
    var mostVisited = localStorage.getItem('visitedPages');
    
    function updateMostVisited(mostVisited) {
        var largest = findLargest(mostVisited, 10);
        mostVisitedUl.innerHTML = '';
        largest.forEach(function (e) {
            if (e.visitedTimes > 0) {
                var liContainer = document.createElement('li');
                liContainer.classList = "mw-list-item";
                var a = document.createElement('a');
                liContainer.appendChild(a);
    
                a.href = 'https://www.allthetropes.org/wiki/' + e.key;
                a.textContent = e.title + ' (' + e.visitedTimes + ')';
                a.title = e.title;
                a.addEventListener('click', handleVisitedLinkClick);
                function handleVisitedLinkClick(f) {
                    if (f.shiftKey || f.altKey) {
                        if (f.shiftKey) {
                            f.preventDefault();
                            var newName = prompt("New name");
                            console.log(mostVisited, mostVisited[e.key]);
                            mostVisited[e.key].title = newName;
                        } else if (f.altKey) {
                            delete mostVisited[e.key];
                        }
                        localStorage.setItem('visitedPages', JSON.stringify(mostVisited));
                        liContainer.removeEventListener('click', handleVisitedLinkClick);
                        updateMostVisited(mostVisited);
                    }
    
                }
                mostVisitedUl.appendChild(liContainer);
            }
        });
    }
    
    if (mostVisited) {
        mostVisited = JSON.parse(mostVisited);
        updateMostVisited(mostVisited);
    }
    document.querySelectorAll('a').forEach(function (e) {
    	e.addEventListener('mousedown', function (f) {
    	var target = e;
		var dest;
		if (target.href) {
			dest = target.href.match(/(?:wiki|w)\/(?!index.php)(.+)/);	
		}
        if (dest && !dest[1].includes('Special:')) {
            dest = dest[1];
            var title = decodeURI(dest.replaceAll("_", " "));
            if (!f.shiftKey && !f.altKey) {
                var visitedPages;
                var parse = JSON.parse(localStorage.getItem('visitedPages'));
                if (parse) {
                    visitedPages = parse;
                } else {
                    visitedPages = {};
                }
                if (visitedPages[dest]) {
                    visitedPages[dest].visitedTimes++;
                } else {
                    visitedPages[dest] = {
                        visitedTimes: 1,
                        title: title
                    };
                }
                localStorage.setItem('visitedPages', JSON.stringify(visitedPages));
            }
        }
    	});
    });
}
mostVisitedPages();
//I'm not an admin, I don't care about admin only tools
document.querySelector('#p-Admin_Only_Tools').remove();