Jump to content

User:Enterprisey/offset-history-link.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 () {
    var diffCell = document.querySelector( "td.diff-ntitle" );
    if( diffCell ) {
        mw.loader.using( [ "mediawiki.api" ], function () {
            new mw.Api().get( {
                action: "query",
                prop: "revisions",
                revids: mw.config.get( "wgDiffNewId" ),
                rvprop: "timestamp"
            } ).then( function ( data ) {
                var timestamp = data.query.pages[ mw.config.get( "wgArticleId" ) ].revisions[0].timestamp;
                var TIMESTAMP_RGX = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/;
                var timestampMatch = TIMESTAMP_RGX.exec( timestamp );
                var year = parseInt( timestampMatch[1] ),
                    month = parseInt( timestampMatch[2] ),
                    day = parseInt( timestampMatch[3] ),
                    hour = parseInt( timestampMatch[4] ),
                    minute = parseInt( timestampMatch[5] ),
                    second = parseInt( timestampMatch[6] );
                var newTimestamp;
                var paddedMonth = month < 10 ? '0' + month : '' + month;
                function pad( x ) { return ( x < 10 ) ? '0' + x : '' + x; }

                // perhaps the most ludicrous if statement I've ever written.
                // I look forward to seeing if it works if someone ever uses
                // this script on an edit made during the final second of the year.
                if( second !== 59 ) {
                    newTimestamp = year + pad( month ) + pad( day ) + pad( hour ) + pad( minute ) + pad( second + 1 );
                } else if( minute !== 59 ) {
                    newTimestamp = year + pad( month ) + pad( day ) + pad( hour ) + pad( minute + 1 ) + "00";
                } else if( hour !== 23 ) {
                    newTimestamp = year + pad( month ) + pad( day ) + pad( hour + 1 ) + "0000";
                } else {
                    var leapYear = ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 );
                    var febLen = leapYear ? 29 : 28;
                    var monthLength = [ 31, febLen, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ][ month ];
                    if( day !== monthLength ) {
                        newTimestamp = year + pad( month ) + pad( day + 1 ) + "000000";
                    } else if( month !== 12 ) {
                        newTimestamp = year + pad( month + 1 ) + "00000000";
                    } else {
                        newTimestamp = ( year + 1 ) + "0000000000";
                    }
                }

                var histUrl = mw.config.get( "wgScript" ) +
                    "?action=history" +
                    "&title=" + encodeURIComponent( mw.config.get( "wgPageName" ) ) +
                    "&offset=" + newTimestamp;
                var contribsUrl = mw.config.get( "wgScript" ) +
                    "?title=Special:Contributions" +
                    "&offset=" + newTimestamp +
                    "&target=" + encodeURIComponent( diffCell.querySelector( "a.mw-userlink" ).textContent );
                $( diffCell ).prepend(
                    $( "<a>", { "href": histUrl } ).text( "History" ),
                    " / ",
                    $( "<a>", { "href": contribsUrl } ).text( "contribs" ),
                    " starting here"
                );
            } );
        } );
    }
} )();