User:Jitse Niesen/Client-side preferences/Main.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. |
![]() | Documentation for this user script can be added at User:Jitse Niesen/Client-side preferences/Main. |
function clientSidePreferencesLoadFunction() {
var el = document.getElementById('bodyContent').childNodes[0];
while (el.nextSibling) {
el = el.nextSibling;
if (el.tagName && el.tagName.toLowerCase() == 'p')
break;
}
var cspForm = document.createElement("form");
cspForm.innerHTML = '' +
'<form><p>' +
'<input type="checkbox" name="tabPurge" /> ' +
'Add \'purge\' tab to clear Wikipedia cache of page<br />' +
'<input type="checkbox" name="tabSince" /> ' +
'Add \'since\' tab to show changes since user last edited given page<br />' +
'<input type="button" onclick="cspProcessForm();" value="Generate script" /><br />' +
'</p></form>';
cspForm.name = 'cspForm';
el.parentNode.replaceChild(cspForm, el);
}
function cspProcessForm() {
window.newscript = 'if (location.href == "http://en.wikipedia.org/wiki/' +
'User:Jitse_Niesen/Client-side_preferences")\n' +
' document.write(\'<script type="text/javascript" src="\' \n' +
' + \'http://en.wikipedia.org/w/index.php?title=User:Jitse_Niesen' +
'/Client-side_preferences/Main.js\' \n' +
' + \'&action=raw&ctype=text/javascript&dontcountme=s"></script>\'); \n';
window.fragments = new Array();
if (document.cspForm.tabPurge.checked)
window.fragments = window.fragments.concat('Add_purge_to_tabs', 'addLink');
if (document.cspForm.tabSince.checked)
window.fragments = window.fragments.concat('Changes_since_I_last_edited', 'Add_LI_link');
window.bottom = document.cspForm;
cspDownloadFragments();
}
function cspDownloadFragments() {
if (window.fragments.length == 0) {
cspLoadEditPage();
return;
}
var fragment = window.fragments[0];
var msg = document.createTextNode('Loading ' + fragment + '.js ...');
window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);
// Copied from http://developer.mozilla.org/en/docs/AJAX:Getting_Started
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
window.http_request = new XMLHttpRequest();
window.http_request.overrideMimeType('text/xml');
window.http_request.onreadystatechange = cspReceivedFragment;
var url = 'http://en.wikipedia.org/w/index.php?title=User:Jitse_Niesen/Client-side_preferences/' +
fragment + '.js&action=raw&ctype=text/javascript&dontcountme=s';
window.http_request.open('GET', url, true);
window.http_request.send(null);
}
function cspReceivedFragment() {
if (http_request.readyState != 4) {
// still not ready
return;
}
if (http_request.status != 200) {
alert('There was a problem with the request.');
return;
}
var msg = document.createTextNode(' done');
window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);
window.bottom = window.bottom.parentNode.insertBefore(document.createElement('BR'), bottom.nextSibling);
window.newscript += '\n// From [[User:Jitse Niesen/Client-side preferences/' + window.fragments[0] +
']]\n\n' + http_request.responseText + '\n';
window.fragments = window.fragments.slice(1);
cspDownloadFragments();
}
function cspLoadEditPage() {
var msg = document.createTextNode('Loading your user script ...');
window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);
var userpageURL = document.getElementById('pt-userpage').firstChild.href;
var username = userpageURL.slice('http://en.wikipedia.org/User:'.length)
var newURL = 'http://en.wikipedia.org/w/index.php?title=User:' + username + '/monobook.js&action=edit';
var newwin = window.open(newURL, '_blank');
newwin.onload = cspSaveEditPage;
window.newwin = newwin;
}
function cspSaveEditPage() {
var msg = document.createTextNode(' saving new script');
window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);
window.bottom = window.bottom.parentNode.insertBefore(document.createElement('BR'), bottom.nextSibling);
window.newwin.document.editform.wpTextbox1.value = window.newscript;
window.newwin.document.editform.wpSummary.value = 'Updated client-side preferences';
window.newwin.document.editform.wpMinoredit.checked = true;
window.newwin.document.editform.wpSave.click();
}
addOnloadHook(clientSidePreferencesLoadFunction);