Jump to content

User:Jag123/StubScript.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <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>