User:Anerisys/contribution shortcuts.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Anerisys/contribution shortcuts. |
/* 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" );
}
}
}