User:Spangineer/Running header.js

/* * Does a lookup two previous Page:* from the current, extract the rh from * its content, insert it in the header after trying to increase the rh page * number. See the FIXME comment(s) for caveats. * * From User:Inductiveload/Running header.js; originally at User:Phe/Running header.js * * To use it, add this to your JavaScript: *  importScript('User:Spangineer/Running header.js'); */

/** * TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor. * @see https://meta.wikimedia.org/wiki/TemplateScript * @update-token */ $.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function {	pathoschild.TemplateScript.add({ name: 'Running header', script: set_running_header }); });

function set_running_header(editor) { editor = editor || pathoschild.TemplateScript.Context; // backward compatibility var wikiPageName = mw.config.get('wgPageName');

// get previous page var page = Number(wikiPageName.match(/(\d+)$/)[1]) - 2; var pagename = wikiPageName.replace(/\d+$/g, page); $.getJSON(mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=' + encodeURIComponent(pagename)).then(function(data) {		// extract running header		var runningHeader = null;		if(!data || data.query.pages['-1'])			return;		for (var ids in data.query.pages) {			var content = data.query.pages[ids].revisions[0]['*'];			var match = content.match(//);			if (match) {				// FIXME: needs to be tweaked, actually works only if the first				// sequence of digit in the rh is the one and only one we want				// to modify. Break also for non roman number.				match = match[0];				var page = Number(match.match(/(\d+)/)[1]) + 2;				runningHeader = match.replace(/\d+/, page);			}			break;		}

// update page if(runningHeader) { editor.forField('#wpHeaderTextbox') .append('\n' + runningHeader);

//var h = document.getElementById("prp_header"); //if (h.style.cssText != '') //	pr_toggle_visibility;

editor.setEditSummary('running header'); }	}); }

//function addButton2(id,alt,comment,source,onclick) { //	var tb = document.getElementById("toolbar"); //	if(tb) { //		var image = document.createElement("img"); //		image.width = 46; //		image.height = 22; //		image.border = 0; //		image.className = "mw-toolbar-editbutton"; //		image.style.cursor = "pointer"; //		image.alt = alt; //		image.title = comment; //		image.src = source; //		image.onclick = onclick; //		tb.appendChild(image); //	} //}

//function addRhButton { //	addButton2( //		"wpRh", "Running header auto", "Running header", //		"http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Button_running_header.svg/23px-Button_running_header.svg.png", set_running_header //	); //}

//$(addRhButton);