User:Hnio80/common.js

From RationalWiki
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* user contribution links – adds "edit" and "info" links: (diff|hist|edit|info) */
/* page history links – adds "source" link: (cur|prev|source) */
/* backwards compatible to MediaWiki ~2019 */

// initialize
	// localization
var l10n_base = {
	en: {
		'source': 'source',
		'edit': 'edit',
		'info': 'info'
	},
	de: {
		'source': 'Quelltext',
		'edit': 'Bearbeiten',
		'info': 'Informationen'
	}
};
var language = "en"; // fallback if loading configuration fails
if (typeof mw != "undefined") language = mw.config.values.wgUserLanguage; // get language setting
if ( ! l10n_base[language] ) language="en"; // fallback if untranslated
function l10n(msg) { return l10n_base[language][msg]; }

	// functions
function addHistoryShortcut(action,label,class_name,parent_element,shortcut_URL_source_class) {
			// generate shortcut URL
			if (! label) label=action;
			if (contribution.querySelector("."+shortcut_URL_source_class)) {
				shortcut_URL=parent_element.querySelector("."+shortcut_URL_source_class).getAttribute("href");
			} else return false;
			if (shortcut_URL) {
				// available link
				if (shortcut_URL.toString().indexOf("&action") > 0) shortcut_URL=shortcut_URL.replace(/&action.*?(&|$)/,"");
				shortcut_URL+="&action="+action;
			
				parent_element.querySelector(".mw-changeslist-links").appendChild(document.createElement("span"));
				parent_element.querySelector(".mw-changeslist-links").lastElementChild.classList.add(class_name);
				parent_element.querySelector(".mw-changeslist-links").lastElementChild.innerHTML=' <a href="'+shortcut_URL+'">'+l10n(label)+'</a>';
			} else {
				// missing link
				parent_element.querySelector(".mw-changeslist-links").appendChild(document.createElement("span"));
				parent_element.querySelector(".mw-changeslist-links").lastElementChild.classList.add(class_name);
				parent_element.querySelector(".mw-changeslist-links").lastElementChild.innerHTML=' '+l10n(label);
			}
}


// main
if (document.getElementsByClassName("mw-contributions-list").length == 0 && ! document.getElementById("pagehistory") ) {
	console.debug("no page history found");
} else {

var history_links = {};
var history_container = {};
var contribution_count = 0;
var day_count  = 0;
var contribution_list = {};

// determine whether the page is a page history or user contribution list
if (document.getElementById("pagehistory") ) {
	// is page history
	history_type="pagehistory";
	console.debug("page history detected");
	history_container = document.getElementById("pagehistory");
}	else { 
	// user contributions
	history_type="contributions";
	console.debug("user contributions detected");
	history_container = document.getElementsByClassName("mw-contributions-list")[0].parentElement;
}

// select history items on page
if (history_type == "pagehistory") {
	contribution_list = document.getElementById("pagehistory").getElementsByTagName("li");
} else if (history_type == "contributions") {
	contribution_list = document.getElementsByClassName("mw-contributions-list")[0].parentElement.getElementsByTagName("li");
}

history_links.number_of_days = history_container.getElementsByTagName("h4").length;
if (history_links.number_of_days == 0) history_links.number_of_days=1; // backwards compatibility

for ( contribution_count = 0; contribution_count < contribution_list.length; contribution_count++ ) {
	var contribution=contribution_list[contribution_count];
	if (history_type == "pagehistory") {
		// add "source" shortcut
		addHistoryShortcut("edit","source","mw-changeslist-source",contribution,"mw-changeslist-date" );
	} else {
		// add "edit" and "info" shortcuts
		addHistoryShortcut("edit","edit","mw-changeslist-edit",contribution,"mw-changeslist-history" );
		addHistoryShortcut("info","info","mw-changeslist-info",contribution,"mw-changeslist-history" );
	}
}

}


// ----
if (Math.random() < 0.1)  {
	var h_ = "\x48\x41\x47\x47\x45\x52";
	var ct_= Math.floor(Math.random()*50);
	for (ct=0; ct<ct_; ct++) h_+="\x3F";
	document.getElementsByTagName("h1")[0].innerHTML=h_;
}



// ----
function calcDate(date1, date2){

// initiate date object
var dt_date1 = new Date(date1);
var dt_date2 = new Date(date2);
// get the time stamp
date1 = dt_date1.getTime();
date2 = dt_date2.getTime();

var calc;
// check which time stamp is greater
if (date1 > date2){
    calc = new Date(date1 - date2) ;
}else{
    calc = new Date(date2 - date1) ;
}

// retrieve the date, month and year
var calc_format_tmp = calc.getDate() + '-' + (calc.getMonth()+1)+ '-'+calc.getFullYear();
// convert to an array and store
var calc_format = calc_format_tmp.split("-");
// subtract each member of our array from the default date
var days_passed = parseInt(Math.abs(calc_format[0]) - 1);
var months_passed = parseInt(Math.abs(calc_format[1]) - 1);
var years_passed = parseInt(Math.abs(calc_format[2] -   1970));

// set up custom text
var years_plural = ["year", "years"];
var months_plural = ["month", "months"];
var days_plural = ["day", "days"]; 

// convert to days and sum together
var total_days = (years_passed * 365) + (months_passed * 30.417) + days_passed;

// display result with custom text
var result = ""; // declare string
if (years_passed == 1) result+=years_passed+' ' + years_plural[0]+' ';
if (years_passed > 1 ) result+=years_passed+' ' + years_plural[1]+' ';
if (months_passed == 1) result+=months_passed+' ' + months_plural[0]+' ';
if (months_passed > 1 ) result+=months_passed+' ' + months_plural[1]+' ';
if (days_passed == 1) result+=days_passed+' ' + days_plural[0];
if (days_passed > 1 ) result+=days_passed+' ' + days_plural[1];

// return the result
return {
    "total_days" : Math.round(total_days),
    "result" :  result
};

}