Jump to content

User:BEANS X2/vector.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() {
    pageTitle = mw.config.values.wgCanonicalNamespace+":"+mw.config.values.wgTitle;

    // [APPEARANCE] Custom styles for my userpage
    if (pageTitle.split("/")[0] === "User:BEANS X2") {
        var stylesheets = ["User:BEANS X2/Userpage/index.css","User:BEANS_X2/Userpage/mdl.css"];

        $("#beans").css("display","block");
        stylesheets.forEach(function(style) {
            $.ajax({
                url: "https://en.wikipedia.org/w/index.php?title="+style.replace("_"," ")+"&action=raw"
            })
            .done(function( data ) {
                mw.util.addCSS(data);
            });
        });
    }

    // [UTILITY] Alt+click page title to fill it into Search
    $("#firstHeading").on("click", function(ctx){
        if(ctx.altKey) {
            $("#searchInput")
                .val(pageTitle)
                .focus();
        }
    });

    // [BETA] Enable bracket matching
    mw.config.values.wgCodeMirrorEnableBracketMatching = true;

    // [APPEARANCE] Make the "edit saved" popup a snackbar
    if (mw.config.values.wgPostEdit == "saved") {
        $(".postedit").hide(); // Hide the classic popup

        var undoLastEdit = function() {
            // Adapted from [[User:Enterprisey/undo-last-edit.js]]
            var api = new mw.Api();
            api.get( {
                prop: 'revisions',
                rvprop: 'content',
                rvlimit: 2,
                titles: mw.config.get( "wgPageName" )
            } ).done( function ( data ) {
                if ( !data.query || !data.query.pages ) return;
                var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
                    text = data.query.pages[pageid].revisions[1]["*"];
                api.postWithToken( "csrf", {
                    action: "edit",
                    title: mw.config.get( "wgPageName" ),
                    summary: "Undoing my last edit [semi-automated]",
                    text: text
                } ).done ( function ( data ) {
                    if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                        console.log("Edit undone")
                        document.location.reload( true );
                    }
                } );
            } );
        }

        waitForRW = setInterval( function() {
            try{
                var snackbarContainer = document.querySelector('#rw-toast');
                var data = {
                    message: 'Edit saved',
                    timeout: 5000,
                    actionHandler: undoLastEdit,
                    actionText: 'Undo'
                };
                snackbarContainer.MaterialSnackbar.showSnackbar(data);
                clearInterval(waitForRW)
            }
            catch(err){null}
        },500)
    }
    
    // [UTILITY] Get the RPM easily
    mw.util.addPortletLink( 'p-tb', '#0', 'View RPM').innerHTML = "<a href='#0' id='rpm-link'>&#x21A9;&#xFE0F; View RPM</a>"
    $("#rpm-link").on("click", function(ctx) {
        $.getJSON("https://redwarn.toolforge.org/tools/rpmInfo.php").done( function(data) {
            mw.notify("Reverts per minute: "+data[0].totalRPM)		
        })
    })

    // [UTILITY] "Silent pages" - Add a blacklist of pages to not be added to your watchlist.
    var silentPages = ["Project:Sandbox"]
    var allowedActions = ["edit","submit"]
    if (silentPages.includes(pageTitle) && allowedActions.includes(wgAction)) {
        $("#wpWatchthis").prop('checked', false);
    }

    console.log("Hello from user JS!");
});