User:Anerisys/contribution shortcuts.js

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

	// functions
c_shortcuts.addHistoryShortcut = function(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 {

c_shortcuts.contribution_count = 0;
c_shortcuts.day_count  = 0;

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

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

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

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

}