User:Bawolff/test-mode.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:Bawolff/test-mode. |
/*globals api, escapeQuotesHTML, addPortletLink, importScriptURI, wgPageName, addOnloadHook, wgNamespaceNumber, sortables_init, createCollapseButtons, createNavigationBarToggleButton, window, wgAction, ajaxPreviewExec */
//Script to re-render page with context title 'test<some number>-'. See {{tl|test-mode}} for why this is useful.
//Script by Bawolff, feel free to ask if you have any questions.
//To install add importScript('User:Bawolff/test-mode.js') to [[special:mypage/monobook.js]] (replace monobook with skin if you use different skin).
//Known issue: <source> tags don't work (since they add css to head, which doesn't get picked up). Perhaps fixed on next wmf update.
// Only works on vector and monobook. YMMV on other skins.
//could have better support for edit mode.
//-------------------
mw.loader.load('http://en.wikinews.org/w/index.php?title=User:Bawolff/mwapilib2.js&action=raw&ctype=text/javascript&smaxage=259200');
var testMode = {
init: function () {
//if (mw.config.get('wgAction') === 'edit') return; //doesn't work in non-show preview edit.
if (mw.config.get('wgNamespaceNumber') === -1) return; //no special
var editing = false;
if ((mw.config.get('wgAction') === 'submit' || mw.config.get('wgAction') === 'edit')) {
editing = true;
//add show preview with test mode.
var button = document.createElement('button');
button.type = 'button';
button.id = 'testModePreviewButton';
button.onclick = function () { testMode.exec(true); };
button.appendChild(document.createTextNode('Preview in testing mode'));
var prev = document.getElementById('wpDiff');
prev.parentNode.insertBefore(button, prev);
}
mw.util.addPortletLink('p-tb', 'javascript:testMode.exec(' + editing + ')%3Bvoid%200', 'Display in test mode', 'testModePreviewSidebarLink', 'View page using alternative templates that are being tested. See [[template:Test-mode]] for details.');
},
exec: function (editing) {
var testNumb = prompt('Which test mode do you wish to use? (1, 2 or 3)', '1');
var contextTitle = 'Test' + (testNumb? testNumb: '1') + '-';
var page;
if (editing) page = document.getElementById('wpTextbox1').value; //use edit box
else page = '{{:' + mw.config.get('wgPageName') + '}}';
api(page).parse(contextTitle).lift(this.display, testNumb, editing).exec();
return;
},
display: function (text, mode, editing) {
var body = document.getElementById('wikiPreview');
body = body ? body : document.getElementById('bodyContent');
body.style.display = 'block';
var titleMsg = '<br/><small>(Testing mode ' + escapeQuotesHTML(mode) + '. <source> tags and anything else that adds dynamic css/js may not display properly.';
if (!editing) titleMsg += ' <a onclick="location.reload()" href="#">Reload</a> for normal view.';
titleMsg += ')</small>';
var warn = document.getElementById('testModeWarning');
if (warn) {
warn.innerHTML = titleMsg;
} else {
var title = document.getElementById('firstHeading');
title.innerHTML += '<span id="testModeWarning">' + titleMsg + '</span>';
}
body.innerHTML = text;
//call various js hooks (stolen from [[user:js/ajaxPreview]])
sortables_init();
if (window.createCollapseButtons){//en.wp
createCollapseButtons();
createNavigationBarToggleButton();
}
if (window.ajaxPreviewExec) ajaxPreviewExec(body);
}
}
addOnloadHook(testMode.init);