Jump to content

User:Xaradnam/monobook.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Xaradnam (talk | contribs) at 10:12, 20 October 2021 (+). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.
importScript('User:Shubinator/DYKcheck.js');

//==================================================

CurPg=mw.config.get('wgPageName'); // Current page name

//==================================================

function printValue(txt, val) {
 $('body').append("<center><h4><tt>" + txt + ": " + val + " </tt></h4></center>");
}
if (mw.config.get('wgRevisionId')) {
	printValue("old time", document.getElementById('mw-diff-otitle1').firstChild.firstChild.firstChild.nodeValue);
	printValue("old user", document.getElementById('mw-diff-otitle2').firstChild.firstChild.firstChild.nodeValue);
	printValue("new time", document.getElementById('mw-diff-ntitle1').firstChild.firstChild.firstChild.nodeValue);
	printValue("new user", document.getElementById('mw-diff-ntitle2').firstChild.firstChild.firstChild.nodeValue);
}
	var time = new Date();
printValue("CurPg", CurPg);
printValue("30+4", 30+4);
//printValue("UTC", time.UTC());
printValue("TimeOffset", time.getTimezoneOffset()/60);
text1="Revision as of 11:19, 13 April 1998";
text2="Latest revision as of 19:29, 23 May 1998";
printValue('text1', text1);
printValue('text1 replaced', text1.replace(/(Latest )?Revision as of /i, ""));
printValue('text2', text2);
printValue('text2 replaced', text2.replace(/(Latest )?Revision as of /i, ""));

//==================================================

// Section subscribe
CurNSnum=mw.config.get('wgNamespaceNumber');
if (CurNSnum%2==1||CurNSnum==4)
	{mw.util.addPortletLink(
		'p-tb',
		mw.config.get('wgScript')+'?title='+mw.config.get('wgPageName')+'&dtenable=1',
		'Section subscribe'
		);}
		
//==================================================

// Add unredirected link next to redirects; from [[User:BrandonXLF/NoRedirect.js]]
$(function() {
	$('#mw-content-text .mw-redirect').after(function() {
		return this.href.indexOf('redirect=no') !== -1 || this.href.indexOf('action=') !== -1  || this.href.indexOf('diff=') !== -1 ? ''
			: '<a href="' +
			this.href +
			(this.href.includes('?') ? '&' : '?') +
			'redirect=no" title="' +
			(this.title || this.href) +
			' (redirect pg)"><span style="color:#534e8a"><sup> Я</sup></span></a>';
	});
});


// ======== ADD DIFF INFO TO BOTTOM OF PAGE WHEN APPLICABLE =========
$.when( $.ready, mw.loader.using(["mediawiki.util"]) ).then( function () {
    var suffix = mw.config.get("wgDiffNewId");
    if (suffix) {
        if (document.getElementsByClassName("diff-multi").length ||
            CurPg === "Special:ComparePages") {
            suffix = mw.config.get("wgDiffOldId") + "/" + suffix;
        }
        $('body').append("<center><h4><tt>[[Special:Diff/" + suffix + "&#124;diff]]</tt></h4></center>");
    } else {
        var oldidMatch = mw.util.getParamValue("oldid");
        if(oldidMatch) {
            $('body').append("<center><h4><tt>[[Special:Permalink/" + oldidMatch + "&#124;permalink]]</tt></h4></center>");
        }
    }
} );

//==================================================
//Adds time between diffs and time since each revision
// User:Dudemanfellabra/diffs.js
function DateSub(Date1,Date2) {
    var timeDifference = Date2 - Date1;
    var minutesAgo = Math.round(timeDifference / 1000 / 60);
    var hoursAgo = 0
    var daysAgo = 0
    var hoursText = 'hour'
    var daysText = 'day'

    while (minutesAgo >= 60) {
        minutesAgo = minutesAgo-60;
        hoursAgo++;
    }

    while (hoursAgo >= 24) {
        hoursAgo = hoursAgo-24;
        daysAgo++;
    }

    if (hoursAgo!=1)
        hoursText = hoursText + 's';

    if (daysAgo!=1)
        daysText = daysText + 's';

    if (daysAgo > 0) {
        if (hoursAgo > 0) {
            if (minutesAgo <=1 ) {
                minutesAgo = daysAgo + ' ' + daysText + ', ' + hoursAgo + ' ' + hoursText;
            }
            else {
                minutesAgo = daysAgo + ' ' + daysText + ', ' + hoursAgo + ' ' + hoursText + ', ' + minutesAgo + ' minutes';
            }
        }
        else {
            if (minutesAgo <=1 ) {
                minutesAgo = daysAgo + ' ' + daysText;
            }
            else {
                minutesAgo = daysAgo + ' ' + daysText + ', ' + minutesAgo + ' minutes';
            }
        }
    }
    else {
        if (hoursAgo > 0) {
            if (minutesAgo <=1 ) {
                minutesAgo = hoursAgo + ' ' + hoursText;
            }
            else {
                minutesAgo = hoursAgo + ' ' + hoursText + ', ' + minutesAgo + ' minutes';
            }
        }
        else {
            if (minutesAgo <=1 ) {
                minutesAgo = 'Less than a minute';
            }
            else {
                minutesAgo = minutesAgo + ' minutes';
            }
        }
    }
    return minutesAgo;
}

if (document.getElementById('mw-diff-otitle1') || document.getElementById('mw-diff-ntitle1')) {

function convertTimestampStringToDate(id) {
    var timestamp = document.getElementById(id).firstChild.firstChild.firstChild.nodeValue;
    var str = /evision as of /g
    str.lastIndex = 0;
    str.test(timestamp)
    var date = timestamp.substring(str.lastIndex)
/*
Safari doesn't recognize Wikipedia's date format.
Every other browser can just use the date string
object above.
*/
    var str1 = /[A-z]+/
    var str2 = /\d+/g
    var monthtext = date.match(str1);
    var testdate = date.match(str2);
    var hours = testdate[0];
    var minutes = testdate[1];
    var day = testdate[2];
    var year = testdate[3];
    var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    for (i in months) {
        if (monthtext==months[i]) {
            var month = i;
            break;
        }
        i++
    }
/*

End Safari madness

*/
    date = new Date(year,month,day,hours,minutes);
    return date;
}

var leftNode = document.getElementById('mw-diff-otitle1');
var rightNode = document.getElementById('mw-diff-ntitle1');
var firstDate = convertTimestampStringToDate('mw-diff-otitle1');
var secondDate = convertTimestampStringToDate('mw-diff-ntitle1');

function TimeBetweenDiffs() {

    var minutesAgo = DateSub(firstDate,secondDate);

    var newNode = document.createElement('span');
    newNode.setAttribute('style', 'font-weight:bold; color:red;')
    newNode.appendChild(document.createTextNode('(' + minutesAgo + ' later)'));
    newNode.appendChild(document.createElement('br'));

    rightNode.insertBefore(newNode, rightNode.firstChild);
}

function TimeSinceDiff() {
    var DateNow = new Date();

    var minutesAgo1 = DateSub(firstDate,DateNow);
    var minutesAgo2 = DateSub(secondDate,DateNow);

    document.getElementById('mw-diff-otitle1').title=minutesAgo1 + ' ago';
    document.getElementById('mw-diff-ntitle1').title=minutesAgo2 + ' ago';

    var t = setTimeout("TimeSinceDiff()",60000);
}

$(window).load(TimeSinceDiff);                                  // Adds time since displayed diffs as tooltip
$(window).load(TimeBetweenDiffs);                               // Adds time between diffs

}