Jump to content

User:Macaw*/UserScript1.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(function(){
    'use strict';
    function processSearchPage(url){
        fetch(url)
        .then(res => res.text())
        .then(html => {
            var parser = new DOMParser();
            var doc = parser.parseFromString(html, "text/html");
            var links = Array.from(doc.querySelectorAll(".mw-search-result-heading a")).map(a => a.getAttribute("href"));
            function processLink(i){
                if(i >= links.length){
                    var nextLink = doc.querySelector(".mw-search-pager-next a");
                    if(nextLink){
                        processSearchPage(location.protocol + "//" + location.host + nextLink.getAttribute("href"));
                    }
                    return;
                }
                var articleUrl = location.protocol + "//" + location.host + links[i];
                var rawUrl = articleUrl + "?action=raw";
                fetch(rawUrl)
                .then(r => r.text())
                .then(rawText => {
                    if(/{{\s*Short\s+description\s*\|/.test(rawText)){
                        processLink(i+1);
                    } else {
                        window.location.href = articleUrl;
                    }
                })
                .catch(() => processLink(i+1));
            }
            processLink(0);
        })
        .catch(()=>{});
    }
    window.addEventListener("keydown", function(e){
        if(e.ctrlKey && e.shiftKey && e.key.toLowerCase() === 'r'){
            e.preventDefault();
            var searchUrl = location.protocol + "//" + location.host + "/w/index.php?title=Special:Search&search=-hastemplate:%22Short%20description%22";
            processSearchPage(searchUrl);
        }
    }, false);
})();