User:Wugapodes/DYK promoter.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:Wugapodes/DYK promoter. |
console.log("A script to automatically promote DYK hooks to prep.");
var masterList = []
function getPrepId(pD) {
var prepTitle = $(pD).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[i]);
console.log("Loaded prep ".concat(j));
//prepDiv = prepDiv[0]
console.log(prepDiv[i]);
var hookObject = getPrepHooks(prepDiv[j-1]);
if (hookObject.slots > 0) {
prepDiv[j-1].appendChild(hookObject.hooks);
prepDiv[j-1].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_';
var prepDiv = []
for (i=0;i<6;i++) {
prepDiv[i] = document.createElement("div");
$( prepDiv[i] ).load( prepURL.concat(i+1), function(){});
}
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) {
loadPrepSet()
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(pD) {
console.log(pD)
var hookSection = $(pD).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;