User:Xover/Headertools.js
Appearance
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:Xover/Headertools. |
/* global $, mw, OO */
"use strict";
var dependencies = [];
dependencies.push('mediawiki.util'); // Basic MW utility functions
dependencies.push('mediawiki.storage'); // Wrapper for Web Storage API
dependencies.push('oojs-ui-core'); // OOUI core module
dependencies.push('oojs-ui-windows'); // OOUI windowing functions for dialog
// Make sure the necessary modules are loaded
mw.loader.using(dependencies, () => {
// Wait for the page to be parsed (new-style $(document).ready())
$(() => {
/*
* First check that this is a context we should be active in.
*/
// Only active on Page:-namespace pages.
if (mw.config.get('wgCanonicalNamespace') !== 'Page') {
return;
}
// Only active on pages with content model 'proofread-page'.
if (mw.config.get('wgPageContentModel') !== 'proofread-page') {
return;
}
// Only active when in edit/preview/diff mode.
if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) < 0) {
return;
}
/*
* Extract name for the current work, and construct storage keys for it.
*/
var curWork = /^(.*)\/\d+$/.exec(mw.config.get('wgTitle'))[1];
var curWorkChapterKey = "headertools-" + curWork + "-chapter";
var curWorkAutoKey = "headertools-" + curWork + "-auto";
/*
* Add portlets for the various commands.
*/
var headerPortlet = mw.util.addPortletLink(
'p-tb', '#', 'Reset header and footer', 'ca-resetheader',
'Reset the header and footer to the values given in the Index.'
);
$(headerPortlet).click(event => {
event.preventDefault();
doResetHeader(curWorkChapterKey);
});
var chapterPortlet = mw.util.addPortletLink(
'p-tb', '#', 'Set chapter title', 'ca-setchapter',
'Save the selected text as the current chapter title.'
);
$(chapterPortlet).click(event => {
event.preventDefault();
doSetChapter(curWorkChapterKey);
});
var autoOnPortlet = mw.util.addPortletLink(
'p-tb', '#', 'Auto-reset header and footer', 'ca-autoadd',
'Automatically reset header and footer on edit.'
);
$(autoOnPortlet).click(event => {
event.preventDefault();
doSetAutoOn(curWorkAutoKey);
});
var autoOffPortlet = mw.util.addPortletLink(
'p-tb', '#', 'Stop auto-resetting header and footer', 'ca-noautoadd',
'Stop automatically resetting header and footer on edit.'
);
$(autoOffPortlet).click(event => {
event.preventDefault();
doSetAutoOff(curWorkAutoKey);
});
/*
* Automatically reset header and footer if the user requested it.
*/
if (mw.storage.get(curWorkAutoKey) === "yes") {
if (mw.config.get('wgAction') === "edit") { // Not on preview or diff
// mw.Title.exists() is broken, cf. T184953
// Workaround is to check revision ID which is 0 for non-existent pages.
// Also auto-reset for "Not Proofread" pages.
if (mw.config.get("wgArticleId") === 0 || mw.config.get("prpPageQuality") === 1) {
doResetHeader(curWorkChapterKey);
}
}
}
}); // END: $(document).ready()
}); // END: mw.loader.using()
function doResetHeader(curWorkChapterKey) {
var curChapter = mw.storage.get(curWorkChapterKey);
const ppIndexPageLabel = mw.config.get("prpFormattedPageNumber");
var h = mw.config.get("prpIndexFields").Header;
h = h.replaceAll('{{{pagenum}}}', ppIndexPageLabel);
h = h.replaceAll('{{{chapter}}}', curChapter);
$('#wpHeaderTextbox').val(h);
var f = mw.config.get("prpIndexFields").Footer;
f = f.replaceAll('{{{pagenum}}}', ppIndexPageLabel);
f = f.replaceAll('{{{chapter}}}', curChapter);
$('#wpFooterTextbox').val(f);
} // END: doResetHeader()
function doSetChapter(curWorkChapterKey) {
var oldChapter = mw.storage.get(curWorkChapterKey);
OO.ui.prompt(
'Set chapter title',
{
textInput: {
placeholder: 'Chapter title',
value: oldChapter
}
}
).done((chapter) => {
if (chapter !== null) {
mw.storage.set(curWorkChapterKey, chapter);
}
});
} // END: doSetChapter()
function doSetAutoOn(curWorkAutoKey) {
mw.storage.set(curWorkAutoKey, "yes");
}
function doSetAutoOff(curWorkAutoKey) {
mw.storage.set(curWorkAutoKey, "no");
}