User:Jag123/StubScript.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:Jag123/StubScript. |
// <nowiki>
var stubName = "";
var criteria = "";
var fnName = "";
var newDiv;
var stubList = "";
function reformatMyPage() {
addToolbox();
}
function createA(href,label)
{
var obj = document.createElement("A");
obj.href = href;
obj.innerText = label;
return obj;
}
function createLI(id)
{
var obj = document.createElement("LI");
obj.id = id;
return obj;
}
function changeStub(NewStub)
{
//--Variables--//
var szNewStub = "{{" + NewStub + "-stub}}"; // New stub to be added
var szEditSummary = "ch to " + szNewStub + " - [[Wikipedia:WikiProject Stub sorting]]"; // Edit Summary message
var bAutoSave = true; // Automatically 'Click' Save Page?
var bCheckMinor = true;
//-----------//
var ta = document.getElementsByTagName("textarea")(0);
if (ta == null) return;
var szArticle = ta.innerText;
var lPos = szArticle.indexOf('stub}}');
var lStartPos = 0;
var x = 0;
var bFound = false;
while (lStartPos == 0)
{
x++
if (szArticle.substr(lPos - x,2) == "{{")
lStartPos = lPos - x ;
if (x>20)
lStartPos = -1;
}
var szArticleEdit = '';
if (lStartPos >0)
{
szArticleEdit = szArticle.substr(0,lStartPos) + szNewStub + szArticle.substr(lPos+6,szArticle.length);
ta.innerText = szArticleEdit;
var txtSummary = findSummaryTag();
txtSummary.value = szEditSummary;
if (bCheckMinor) checkMinorEdit();
if (bAutoSave) document.all['wpSave'].click();
} else window.alert('Could not find stub message');
}
function findSummaryTag()
{
var col = document.getElementsByTagName('input');
for (var x =0; x < col.length; x++)
if (col(x).name == 'wpSummary') break;
return col(x)
}
function checkMinorEdit()
{
var col = document.getElementsByTagName('input');
for (var x =0; x < col.length; x++)
if (col(x).name == 'wpMinoredit')
{
col(x).checked = "true";
break;
}
}
function AddToolboxLink(link,title,id)
{
var objUL = document.all['p-tb'].children(1).children(0);
objLI = createLI('but' + id + 'Stub');
objLI.appendChild(createA(link,title));
objUL.appendChild(objLI);
}
function openStub()
{
var sz = stubList;
newDiv = document.createElement('div');
newDiv.innerHTML = sz;
newDiv.style.position = "absolute";
newDiv.style.backgroundColor = "#0099CC";
newDiv.style.visibility = "visible";
document.all['p-tb'].children(1).children(0).appendChild(newDiv);
criteria = "stubName == ''";
fnName = "changeStub(stubName);";
fnRun();
}
function addToolbox()
{
var objUL = document.all['p-tb'].children(1).children(0);
var objHR = document.createElement('hr');
objUL.appendChild(objHR);
AddToolboxLink("Javascript:openStub();","Open Stubs","StubWin");
}
function fnRun()
{
if (eval(criteria))
{
setTimeout("fnRun();",500);
} else
{
eval(fnName);
}
}
function setStub(data)
{
stubName = data;
}
///////////////////////////////////////////////
if (window.addEventListener) window.addEventListener("load",reformatMyPage,false);
else if (window.attachEvent) window.attachEvent("onload",reformatMyPage);
stubList = stubList + '<a href="javascript:setStub(\'anatomy\');">Anatomy</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'chem\');">Chem</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'cellbio\');">Cell Bio</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'biosci\');">Biology</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'biochem\');">Biochem</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'bacteria\');">Bacteria</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'animal\');">Animal</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'plant\');">Plant</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'med\');">Medecine</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'physics\');">Physics</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'psychology\');">Psychology</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'astronomy\');">Astronomy</a><br>';
stubList = stubList + '<a href="javascript:setStub(\'geology\');">Geology</a><br>';
// </nowiki>