User:Phe/Running header.js
< User:Phe
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
Documentation for this script can be added at User:Phe/Running header. |
/*
* 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.
*
* To use it, add this to your JavaScript:
* importScript('User:Phe/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 [[File:pathoschild/templatescript.js]]
*/
$.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,
forNamespaces: 'page'
});
});
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(/{{([Rr]h|[Rr]unning ?[Hh]eader) *\|.*}}/);
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);