Jump to content

User:Wugapodes/DYK promoter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wugapodes (talk | contribs) at 06:22, 24 July 2019 (bug fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
console.log("A script to automatically promote DYK hooks to prep.");
var masterList = []

function getPrepId(prepDiv) {
	var prepTitle = $(prepDiv).find('#firstHeading')[0].innerText
	console.log(prepTitle)
	if (prepTitle.includes("Preparation area 1")) {
		return 1;
	} else if (prepTitle.includes("Preparation area 2")) {
		return 2;
	} else if (prepTitle.includes("Preparation area 3")) {
		return 3;
	} else if (prepTitle.includes("Preparation area 4")) {
		return 4;
	} else if (prepTitle.includes("Preparation area 5")) {
		return 5;
	} else if (prepTitle.includes("Preparation area 6")){
		return 6;
	} else {
		return -1;
	}
}

function loadPrepSet() {
	var j = getPrepId(prepDiv);
	console.log("Loaded prep ".concat(j));
	//prepDiv = prepDiv[0]
	console.log(prepDiv);
	var hookObject = getPrepHooks(prepDiv);
	if (hookObject.slots > 0) {
		prepDiv.appendChild(hookObject.hooks);
		prepDiv.setAttribute("class","prep");
		prepDiv.setAttribute("id","prep-".concat(j));
		hookObject.div = prepDiv;
		hookObject.name = "Prep ".concat(j);
		masterList.push(hookObject);
	}
}

var i
var prepURL = 'https://en.wikipedia.org/wiki/Template:Did_you_know/Preparation_area_';
for (i=1;i<7;i++) {
	var prepDiv = document.createElement("div");
	console.log('DYKPromoter: Loading Prep Area '.concat(i));
	$( prepDiv ).load( prepURL.concat(i), loadPrepSet );
	setTimeout(console.log("Next"),4000)
}

var wgAction = mw.config.get('wgAction'); // These need updated
var wgTitle = mw.config.get('wgTitle'); // These need updated
if (wgTitle.includes("Did you know nominations")) {
	main();
}

var api = new mw.Api();


function addPortletLink(f, text) {
	var portletLink = mw.util.addPortletLink('p-cactions', '#', text);
    $(portletLink).click(function (e) {
        e.preventDefault();
        f();
    });
}

function closeNom() {
	var wikiText = document.getElementById('wpTextbox1').innerHTML;
	var hooks = getHooks(wikiText);
	api.get({
		"action": "parse",
		"format": "json",
		"text": "*".concat(hooks.join("\n*")),
		"contentmodel": "wikitext"
	}).done(function(data) {
		formatEditNotice(data);
	});
	// doStuffToNom();
}

function formatEditNotice(data) {
	hookHTML = $.parseHTML(data["parse"]["text"]['*']);
	console.log(hookHTML[0]);
	var listItems = hookHTML[0].getElementsByTagName("li");
	var radioDiv = document.createElement("div"); 
	var i;
	for (i = 0; i < listItems.length; i++) { 
		makeRadioButton(i,listItems,radioDiv);
	}
	var editNotice = document.getElementById('editnotice-area');
	editNotice.innerHTML = "";
	editNotice.appendChild(radioDiv);
	// Add buttons to do stuff
	console.log(masterList)
	var prepSelect = document.createElement("select");
	for (i=0;i < masterList.length;i++) {
		var option = document.createElement("option");
		option.setAttribute("value",masterList[i].name);
		var slotText = ", ".concat(masterList[i].slots).concat(" open slots");
		option.innerText = masterList[i].name.concat(slotText);
		prepSelect.appendChild(option);
	}
	editNotice.appendChild(prepSelect);
}

function makeRadioButton(i,listItems,radioDiv) {
	var radioPara = document.createElement("p");
	var radioButton = document.createElement("input");
	radioButton.setAttribute("type","radio");
	radioButton.setAttribute("id",i);
	radioButton.setAttribute("name","hooks");
	radioButton.setAttribute("value",i);
	radioPara.appendChild(radioButton);
	var radioLabel = document.createElement("label");
	radioLabel.setAttribute("for",i);
	radioLabel.innerHTML = listItems[i].innerHTML; 
	radioPara.appendChild(radioLabel);
	radioDiv.appendChild(radioPara);
	console.log(listItems[i].innerText);
}

function getHooks(wikiText) {
	var re = /('''ALT.+?)?(\.\.\..*)\?/g;
	var hooks = wikiText.match(re);
	return hooks;
}

function main() {
	// Only add for pages with the right string somewhere in the title
    if (document.title.includes("Editing Template:Did you know nominations/")) {
        addPortletLink(closeNom, "Close DYK Nom");
    }
}

function getPrepHooks(prepDiv) {
	console.log(prepDiv)
	var hookSection = $(prepDiv).find('ul')[2]; // If order of stuff on prep changes, this may no longer work
	console.log(hookSection)
	var hooks = $(hookSection).find('li');
	console.log(hooks)
	var i;
	var hookNum = 0;
	for (i = 0; i < hooks.length; i++) {
		var hookText = hooks[i].innerText;
		console.log(hookText)
		if (hookText.includes("... that ...")) {
			hookNum = hookNum+1;
		}
	}
	var emptySlots = hooks.length - hookNum;
	var hookObject = {
		hooks: hookSection,
		slots: emptySlots
	};
	console.log(hookObject);
	return hookObject;
}

//var contentText = document.getElementById('mw-content-text');
//var firstHeading = document.getElementById('firstHeading');
//var redirMsg = contentText.getElementsByClassName('redirectMsg')[0];
//var uiWrapper = el('div');
//var edittoken = null;