FPL JS

//

```
/\*
 - Featured Page List (FPL)
 - Allows a user manage, add to, and remove from <choose> templates.
 - ! UNFINISHED ! This script does not include all planned features and may contain bugs.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 - Author: Blaster Niceshot (http://bniceshottest.wikia.com/wiki/User:Blaster\_Niceshot)
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 - CSS stylesheet: http://bniceshottest.wikia.com/wiki/FeaturedPageList.css
\*/

//TODO
//range remove alert
//file mode: add by cat
//multi recommend
//multi alerts
//update alerts to use $.showCustomModal

$(document).ready(function() {
 var fplConfig = $.extend({
 templates: ['Template:Featured Minigame','Template:Hi'],
 categories: ["Minigame"],
 maxPages: 10,
 linkMode: true,
 fileMode: true,
 imgSelector: '.pi-image-thumbnail',
 recommendMode: true
 }, window.fplConfig);

 if (fplConfig.templates.length === 0) {
 console.log("FPL: No templates found. Exiting script.");
 } else {
 var fplCheckExist = setInterval(function() {
 if ($('#fplButton').length) {
 var fplTime;
 $(".fplMenu").on('click',function() {
 $(this).toggleClass("active");
 });
 $(".fplMenu .WikiaMenuElement").mouseleave(function(){
 fplTime = setTimeout(function(){$(".fplMenu").removeClass("active")},300);
 });
 $(".fplMenu .WikiaMenuElement, .fplMenu").mouseenter(function(){
 clearTimeout(fplTime);
 });
 clearInterval(fplCheckExist);
 if (fplConfig.templates.length > 1) {
 $(".fplBtnAddRmv").click(function() { fplMultiTemp('addRmv'); return false;});
 $(".FPL-catAdd").click(function() { fplMultiTemp('catAdd'); return false;});
 $(".FPL-catRemove").click(function() { fplMultiTemp('catRmv'); return false;});
 $(".FPL-multiAdd").click(function() { fplMultiTemp("multiAdd"); return false;});
 $(".FPL-multiRmv").click(function() { fplMultiTemp("multiRmv"); return false;});
 $(".FPL-about").click(function() { fplAbout(); return false;});
 $(".FPL-clear").click(function() { fplMultiTemp('clear'); return false;});
 $(".FPL-range").click(function() { fplMultiTemp('rangeRmv'); return false;});
 } else {
 $(".fplBtnAdd").click(function(){ fplAddNewPage(); return false; });
 $(".fplBtnRmv").click(function(){ fplRemovePage(); return false; });
 $(".FPL-catAdd").click(function() { fplGetCat("Add"); return false;});
 $(".FPL-catRemove").click(function() { fplGetCat("Remove"); return false;});
 $(".FPL-multiAdd").click(function() { fplGetPgs("Add"); return false;});
 $(".FPL-multiRmv").click(function() { fplGetPgs("Remove"); return false;});
 $(".FPL-about").click(function() { fplAbout(); return false;});
 $(".FPL-clear").click(function() { fplClear(); return false;});
 $(".FPL-range").click(function() { fplRangeRmv(); return false;});
 }
 }
 }, 5);

 var token = mw.user.tokens.values.editToken,
 pageName = mw.config.get("wgPageName").replace(/\_/g," "),
 serverName = mw.config.get("wgServer"),
 skin = mw.config.get("skin"),
 userLang = mw.config.get("wgUserLanguage"),
 categories = mw.config.get("wgCategories"),
 catName,
 fplOption,
 fplPgList,
 executed = 0,
 w = 0,
 versionNum = 'DEV',
 lsb = '',
 rsb = '';

 if (fplConfig.linkMode === true && fplConfig.fileMode === false) {
 lsb = '[[';
 rsb = ']]';
 }

 var fpl = {
 en: {
 addLabel: "Add to FPL",
 removeLabel: "Remove from FPL",
 addRmvLabel: "FPL Add/Remove",
 addCatLabel: "Add by Category",
 removeCatLabel: "Remove by Category",
 addMultiLabel: "Add Multiple Pages",
 removeMultiLabel: "Remove Multiple Pages",
 clearLabel: "Clear FPL",
 rangeLabel: "Range Remove",
 aboutLabel: "About FPL",
 btnBug: "Report bug",
 btnCont: "Continue",
 bugText: '<br /><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertAddHead: '<span style="color:#74FF63;">SUCCESS: </span>Page Added',
 alertAdd1: 'FPL has successfully added the page <span style="font-style:italic;">' + pageName + '</span> to the designated FPL page <span style="font-style:italic;">',
 alertAdd2:'</span>.<br /><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertAddErrHead: '<span style="color:#FF6363;">ERROR: </span>Repeat Page',
 alertAddErr1: 'FPL has detected that the page <span style="font-style:italic;">' + pageName + '</span> is already listed on the designated FPL page <span style="font-style:italic;">',
 alertAddErr2:'</span>.<br /><span style="font-size:12px;">Something occured that prevented FPL from functioning properly. Please try reloading the page and/or clearing your cache. If that does not work, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertRemoveHead: '<span style="color:#74FF63;">SUCCESS: </span>Page Removed',
 alertRemove1: 'FPL has successfully removed the page <span style="font-style:italic;">' + pageName + '</span> from the designated FPL page <span style="font-style:italic;">',
 alertRemove2:'</span>.<br /><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertRemoveErrHead: '<span style="color:#FF6363;">ERROR: </span>Missing Page',
 alertRemoveErr1: 'FPL has detected that the page <span style="font-style:italic;">' + pageName + '</span> is not listed on the designated FPL page <span style="font-style:italic;">',
 alertRemoveErr2:'</span>.<br /><span style="font-size:12px;">Something occured that prevented FPL from functioning properly. Please try reloading the page and/or clearing your cache. If that does not work, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertRemoveByCatHead: '<span style="color:#74FF63;">SUCCESS: </span>Page(s) Removed',
 alertRemoveByCat1: 'FPL has successfully removed all pages in <span style="font-style:italic;">Category:',
 alertRemoveByCat2:'</span> which were listed on the FPL page <span style="font-style:italic;">',
 alertRemoveByCat3:'</span>.<br /><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertAddByCatHead: '<span style="color:#74FF63;">SUCCESS: </span>Page(s) Added',
 alertAddByCat1: 'FPL has successfully added all pages in <span style="font-style:italic;">Category:',
 alertAddByCat2:'</span> which were not listed on the FPL page <span style="font-style:italic;">',
 alertAddByCat3:'</span>.<br /><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertGetCatHead:' by Category',
 alertGetCat:'Please input the category name below. Do not include the <span style="font-style:italic;">Category:</span> namespace prefix, otherwise it will not work. <span style="font-style:italic;font-size: 12px;">Note: This may take a minute or two for large FPLs.</span><br><input id="FPL-input" type="text" style="color:inherit; margin: 10px 0;width: 100%;border: none;background: rgba(128,128,128,0.2);padding: 5px 10px;box-sizing: border-box;"><br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertPermHead:'<span style="color:#FF6363;">ERROR: </span>Inadequate Permissions',
 alertPerm:'You must be an Administator to use this feature in order to prevent vandalism.<br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertClearConfHead:'FPL: Clear Confirmation',
 alertClearConf:'You are about to clear FPL. This change could potentially break templates. To confirm, select the box below and press "Continue". <span style="font-size:12px;">(You can undo this change via the FPL\'s history.)</span><br><input id="FPL-check" type="checkbox" style="height: 12px; margin: 10px 0; margin-right: 5px;">I confirm that this is an intentional change. <span style="font-size:12px">Leave blank and press "Continue" if not.</span><br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertClearHead:'<span style="color:#74FF63;">SUCCESS: </span>FPL Cleared',
 alertClear:'All pages have been successfully cleared from FPL.<br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertAboutHead:'FPL: About',
 alertAbout:'<span style="font-weight:bold;">Featured Page List (FPL)</span> is a script that allows a user manage, add to, and remove from <code>&lt;choose&gt;</code> templates.<br><br><span style="font-weight:bold;">Version:</span> ' + versionNum + '<br><span style="font-weight:bold;">Current Developer(s):</span> <a href="http://bniceshottest.wikia.com/wiki/User:Blaster\_Niceshot" target="\_blank">Blaster Niceshot</a><br><span style="font-weight:bold;">All Developer(s):</span> <a href="http://bniceshottest.wikia.com/wiki/User:Blaster\_Niceshot" target="\_blank">Blaster Niceshot</a>',
 alertRecHead:'FPL: Recommended Page',
 alertRec:'This page has one or more of the categories specified in your FPL config, and is not currently on FPL. Would you like to add this page to FPL now? <span style="font-size:12px;">See your <a href="/wiki/Special:MyPage/wikia.js" target="\_blank">wikia.js</a> or your <a href="http://c.wikia.com/wiki/Special:MyPage/global.js" target="\_blank">global.js</a> for these specified categories, depending on where you have FPL installed.</span><br><input id="FPL-check" type="checkbox" style="height: 12px; margin: 10px 0; margin-right: 5px;">Add page to FPL when I press "Continue".<span style="font-size:12px"> Leave blank and press "Continue" if not.<br>You can turn off these messages in your FPL configuration. If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertMultiHead:'FPL: Select Template',
 alertMulti:'Please select a template to execute this FPL function on.<br><select id="FPL-temp" style="color:inherit; margin: 10px 0; background: rgba(128,128,128,0.2); padding: 5px 10px; box-sizing: border-box; width:100%; border:none; color:inherit;"></select><br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertAddRHead:'FPL: Add Page',
 alertAddR:'This page is currently not listed on FPL. To add it, simply leave the box below checked and press "Continue". Otherwise, uncheck the box and press "Continue".<br><input id="FPL-check" type="checkbox" style="height: 12px; margin: 10px 0; margin-right: 5px;" checked>Add this page to FPL.<br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>',
 alertRmvAHead:'FPL: Remove Page',
 alertRmvA:'This page is currently listed on FPL. To remove it, simply leave the box below checked and press "Continue". Otherwise, uncheck the box and press "Continue".<br><input id="FPL-check" type="checkbox" style="height: 12px; margin: 10px 0; margin-right: 5px;" checked>Remove this page from FPL.<br><span style="font-size:12px;">If you believe this is a bug, please click the Report bug button, describe what happened, and include this message. Thank you.</span>'
 }
 };

 if (fplConfig.lang && typeof fplConfig.lang === 'string') {
 fpl = $.extend(fpl.en, fpl[fplConfig.lang]);
 } else {
 fpl = $.extend(fpl.en, fpl[userLang]);
 }

 var fplLink = $('<link href="http://bniceshottest.wikia.com/index.php?title=FeaturedPageList.css&action=raw&ctype=text/css" type="text/css" rel="stylesheet" media="screen,print">');
 $("head").append(fplLink);

 var fplMenu;
 var fplMenuItems = '<li><a class="FPL-catAdd">' + fpl.addCatLabel + '</a></li><li><a class="FPL-catRemove">' + fpl.removeCatLabel + '</a></li><li><a class="FPL-multiAdd">' + fpl.addMultiLabel + '</a></li><li><a class="FPL-multiRmv">' + fpl.removeMultiLabel + '</a></li><li><a class="FPL-clear">' + fpl.clearLabel + '</a></li><li><a class="FPL-range">' + fpl.rangeLabel + '</a></li><li><a class="FPL-about">' + fpl.aboutLabel + '</a></li>';
 if (skin === "oasis" || skin === "wikia") {
 fplMenu = '<nav class="wikia-menu-button contribute secondary combined fplMenu" style="margin-right:10px; padding-left:10px;"> FPL Tools <span class="drop"><img src="data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D" class="chevron"></span><ul class="WikiaMenuElement" style="min-width: 103px;">' + fplMenuItems + '</ul></nav>';
 $(".WikiHeader .buttons").prepend(fplMenu);
 fplMenu = '<div class="portlet"> <h5>FPL Tools</h5> <div class="pBody"><ul class="fplBox">' + fplMenuItems + '</ul></div></div>';
 $("#p-wikicities-nav").before(fplMenu);
 }

 var fplCurText = [];
 var fplLi;
 var e = 0;
 $.each(fplConfig.templates, function(i,val) {
 $.get(serverName + "/wiki/" + val + "?action=raw").done(function(fplData) {
 e++;
 fplCurText[i] = fplData;
 //Add FPL button
 if (fplConfig.templates.length === 1) {
 if (fplCurText[i].indexOf("<option>[[" + pageName + "]]</option>") === -1 && fplCurText[i].indexOf("<option>" + pageName + "</option>") === -1) {
 fplLi = $('<li><a class="fplBtnAdd" id="fplButton">' + fpl.addLabel + '</a></li>');

 //Check for FPL categories
 if (fplConfig.recommendMode === true) {
 for (var a = 0; a < fplConfig.categories.length; a++) {
 var fplHasCat = categories.indexOf(fplConfig.categories[a]);
 if (fplHasCat !== -1 && $('.FPL-alert').length === 0) {
 fplAlert(fpl.alertRecHead, fpl.alertRec);
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function() {
 if ($("#FPL-check").is(":checked") === true) {
 fplAddNewPage(); return false;
 } else {
 fplRemoveAlert(false);
 }
 }
 );
 }
 }
 }
 } else {
 fplLi = $('<li><a class="fplBtnRmv" id="fplButton">' + fpl.removeLabel + '</a></li>');
 }
 $(".fplMenu ul").prepend(fplLi);
 } else {
 fplLi = $('<li><a class="fplBtnAddRmv" id="fplButton">' + fpl.addRmvLabel + '</a></li>');
 if (e === fplConfig.templates.length) {
 $(".fplMenu ul").prepend(fplLi);
 }
 }
 });
 });
 }

 /\* FUNCTIONS \*/
 function fplRangeRmv(template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)];
 var monthNum1 = '';
 var monthNum2 = '';
 fplAlert('FPL: Range Remove','Please select a start date and end date according to UTC time for pages added to FPL within that date range to be removed. Entering nonexistant dates may give unexpected results.<br><br>Start date:<br><input class="FPL-range-input" type="text" maxlength="2" id="FPL-min-1" placeholder="Minutes (0-59)"></input><input class="FPL-range-input" type="text" maxlength="2" id="FPL-hr-1" placeholder="Hour (0-23)"></input><input class="FPL-range-input" type="text" maxlength="2" id="FPL-day-1" placeholder="Day (1-31)"></input><select class="FPL-range-input"id="FPL-month-1"><option>January</option><option>February</option><option>March</option><option>April</option><option>May</option><option>June</option><option>July</option><option>August</option><option>September</option><option>October</option><option>November</option><option>December</option></select><input class="FPL-range-input" type="text" maxlength="4" id="FPL-year-1" placeholder="Year"></input><br><br>End date:<br><input class="FPL-range-input" type="text" maxlength="2" id="FPL-min-2" placeholder="Minutes (0-59)"></input><input class="FPL-range-input" type="text" maxlength="2" id="FPL-hr-2" placeholder="Hour (0-23)"></input><input class="FPL-range-input" type="text" maxlength="2" id="FPL-day-2" placeholder="Day (1-31)"></input><select class="FPL-range-input"id="FPL-month-2"><option>January</option><option>February</option><option>March</option><option>April</option><option>May</option><option>June</option><option>July</option><option>August</option><option>September</option><option>October</option><option>November</option><option>December</option></select><input class="FPL-range-input" type="text" maxlength="4" id="FPL-year-2" placeholder="Year"></input>');
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function() {
 fplRemoveAlert(false);
 switch ($('#FPL-month-1').val()) {
 case 'January':
 monthNum1 = '0';
 break;
 case 'February':
 monthNum1 = '1';
 break;
 case 'March':
 monthNum1 = '2';
 break;
 case 'April':
 monthNum1 = '3';
 break;
 case 'May':
 monthNum1 = '4';
 break;
 case 'June':
 monthNum1 = '5';
 break;
 case 'July':
 monthNum1 = '6';
 break;
 case 'August':
 monthNum1 = '7';
 break;
 case 'September':
 monthNum1 = '8';
 break;
 case 'October':
 monthNum1 = '9';
 break;
 case 'November':
 monthNum1 = '10';
 break;
 case 'December':
 monthNum1 = '11';
 break;
 }

 switch ($('#FPL-month-2').val()) {
 case 'January':
 monthNum2 = '0';
 break;
 case 'February':
 monthNum2 = '1';
 break;
 case 'March':
 monthNum2 = '2';
 break;
 case 'April':
 monthNum2 = '3';
 break;
 case 'May':
 monthNum2 = '4';
 break;
 case 'June':
 monthNum2 = '5';
 break;
 case 'July':
 monthNum2 = '6';
 break;
 case 'August':
 monthNum2 = '7';
 break;
 case 'September':
 monthNum2 = '8';
 break;
 case 'October':
 monthNum2 = '9';
 break;
 case 'November':
 monthNum2 = '10';
 break;
 case 'December':
 monthNum2 = '11';
 break;
 }
 var startDate = Date.UTC($('#FPL-year-1').val(),monthNum1,$('#FPL-day-1').val(),$('#FPL-hr-1').val(),$('#FPL-min-1').val());
 var endDate = Date.UTC($('#FPL-year-2').val(),monthNum2,$('#FPL-day-2').val(),$('#FPL-hr-2').val(),$('#FPL-min-2').val());
 var rmvPageName = '';
 var rmvPageDate = '';
 var rmvOption = '';
 fplCurText = fplCurText.replace('<choose>','');
 fplCurText = fplCurText.replace('</choose>','');
 var list = fplCurText.split('</option>');
 list.pop();
 $.each(list,function(i,val){
 rmvPageName = val.replace(/<option date=".\*">/,'');
 rmvPageDate = val.replace('<option date="','');
 rmvPageDate = rmvPageDate.replace(/">.\*/,'');
 if (startDate <= Date.UTC(rmvPageDate) <= endDate) {
 rmvOption = '<option date="' + rmvPageDate + '">' + rmvPageName + '</option>';
 rmvOption = rmvOption.replace(/\r?\n|\r/g,'');
 fplCurText = fplCurText.replace(rmvOption,'');
 }
 if (i === list.length -1) {
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=<choose>' + fplCurText + '</choose>&summary=FPL:Removing all pages within a date range from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertClearHead, fpl.alertClear,"success");
 });
 }
 });
 }
 );
 }

 function fplMultiAdd(array,template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)];
 fplCurText = fplCurText.replace('</choose>','');
 var i = 0;
 var o = 0;
 $.each(array,function(i,val){
 if (fplConfig.fileMode === true) {
 $.get(serverName + '/wiki/' + val)
 .done(function(data) {
 var imgName = $(fplConfig.imgSelector,data).attr('data-image-key');
 if (imgName === undefined) {
 imgName = lsb + val + rsb;
 if (fplCurText.indexOf('link=' + val + ']]') === -1 && fplCurText.indexOf('<option>' + val + '</option>') === -1 && fplCurText.indexOf('<option>[[' + val + ']]</option>') === -1) {
 fplCurText += '<option>' + imgName + '</option>';
 }
 o++;
 } else {
 imgName = '[[File:' + imgName + '|link=' + val + ']]';
 if (fplCurText.indexOf('link=' + val + ']]') === -1 && fplCurText.indexOf('<option>' + val + '</option>') === -1 && fplCurText.indexOf('<option>[[' + val + ']]</option>') === -1) {
 fplCurText += '<option>' + imgName + '</option>';
 }
 o++;
 }
 if (o === array.length) {
 fplCurText += '</choose>';
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding multiple pages to featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertAddHead, fpl.alertAdd1 + template + fpl.alertAdd2,"success");
 });
 }
 });
 } else if (fplConfig.fileMode === false) {
 fplCurText += '<option>' + lsb + val + rsb + '</option>';
 if (i === array.length - 1) {
 fplCurText += '</choose>';
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding multiple pages to featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertAddHead, fpl.alertAdd1 + template + fpl.alertAdd2,"success");
 });
 }
 }
 });
 }

 function fplMultiRemove(array,template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)];
 var i = 0;
 $.each(array,function(i,val){
 val = val.replace(/[-\/\\^$\*+?.()|[\]{}]/g, '\\$&');
 var regex = new RegExp('<[option.\*>]\*?' + val + '<\\/option>');
 fplCurText = fplCurText.replace(regex,'');
 var regex2 = new RegExp('<[option.\*>]\*?\\[\\[' + val + '\\]\\]<\\/option>');
 fplCurText = fplCurText.replace(regex2,'');
 var regex3 = new RegExp('<[option.\*>]\*?\\[\\[File:.\*\\|link=' + val + '\\]\\]<\\/option>');
 fplCurText = fplCurText.replace(regex3,'');
 if (i === array.length - 1) {
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Removing multiple pages from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertRemoveHead, fpl.alertRemove1 + template + fpl.alertRemove2,"success");
 });
 }
 });

 }

 function fplGetPgs(type,template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 var array = [];
 fplAlert("FPL: " + type + " Multiple Pages", 'Please input the page names below, and include namespace prefixes. <span style="font-size:12px;">You can change the number of inputs in your configuration.</span>');
 for (var i = 0; i < fplConfig.maxPages; i++) {
 $('.FPL-content').append('<br><div style="display:table; width:100%;"><div style="display:table-cell; width:10%; padding-right:10px;">#' + (i + 1) +': </div><input class="FPL-input" type="text" style="width:100%; color:inherit; margin: 10px 0; border: none; background: rgba(128,128,128,0.2); padding: 5px 10px; box-sizing: border-box; display:table-cell;"></div>');
 w++;
 if (w === fplConfig.maxPages) {
 $('.FPL-content').append(fpl.bugText);
 }
 }
 $(".FPL-cont").off('click');
 var c = 0;
 $(".FPL-cont").click(
 function(){
 $.each($(".FPL-input[value!='']"),function(i,value){
 array.push($(this).val());
 c++;
 if (c === $(".FPL-input[value!='']").length) {
 fplRemoveAlert(false);
 if (type === 'Add') {
 fplMultiAdd(array,template);
 } else if (type === 'Remove') {
 fplMultiRemove(array,template);
 }
 }
 });
 });
 }

 function fplAddRemove(template) {
 x = fplConfig.templates.indexOf(template);
 if (fplCurText[x].indexOf("<option>[[" + pageName + "]]</option>") === -1 && fplCurText[x].indexOf("<option>" + pageName + "</option>") === -1 && fplCurText[x].indexOf("link=" + pageName + ']]') === -1) {
 fplAlert(fpl.alertAddRHead, fpl.alertAddR);
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function(){
 if ($("#FPL-check").is(":checked") === true) {
 fplRemoveAlert(false);
 fplAddNewPage(template);
 } else {
 fplRemoveAlert(false);
 }
 });
 } else {
 fplAlert(fpl.alertRmvAHead, fpl.alertRmvA);
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function(){
 if ($("#FPL-check").is(":checked") === true) {
 fplRemoveAlert(false);
 fplRemovePage(template);
 } else {
 fplRemoveAlert(false);
 }
 });
 }
 }

 function fplMultiTemp(type) {
 fplAlert(fpl.alertMultiHead, fpl.alertMulti);
 for (var i = 0; i < fplConfig.templates.length; i++) {
 $('#FPL-temp').append('<option value="' + fplConfig.templates[i] + '">' + fplConfig.templates[i] + '</option>');
 }
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(function() {
 var temp = $('#FPL-temp').val();
 fplRemoveAlert(false);
 switch (type) {
 case 'add':
 fplAddNewPage(temp);
 return false;
 case 'remove':
 fplRemovePage(temp);
 return false;
 case 'addRmv':
 fplAddRemove(temp);
 return false;
 case 'catAdd':
 fplGetCat("Add",temp);
 return false;
 case 'catRmv':
 fplGetCat("Remove",temp);
 return false;
 case 'multiAdd':
 fplGetPgs("Add",temp);
 return false;
 case 'multiRmv':
 fplGetPgs("Remove",temp);
 return false;
 case 'clear':
 fplClear(temp);
 return false;
 case 'rangeRmv':
 fplRangeRmv(temp);
 return false;
 }}
 );
 }

 function fplClear(template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 if (mw.config.get("wgUserGroups").indexOf('sysop') === -1) {
 fplAlert(fpl.alertPermHead, fpl.alertPerm,'fail');
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function(){
 fplRemoveAlert(false);
 });
 } else {
 fplAlert(fpl.alertClearConfHead, fpl.alertClearConf);
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function() {
 if ($("#FPL-check").is(":checked") === true) {
 fplRemoveAlert(false);
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=<choose></choose>&summary=FPL:Removing all pages from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertClearHead, fpl.alertClear,"success");
 });
 } else {
 fplRemoveAlert(false);
 }
 });
 }
 }

 function fplAbout() {
 fplAlert(fpl.alertAboutHead, fpl.alertAbout,'logo');
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function(){
 fplRemoveAlert(false);
 });
 }

 //Show an alert that gets the category name from user input
 function fplGetCat(type,template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 fplAlert('FPL: ' + type + fpl.alertGetCatHead, fpl.alertGetCat);
 $(".FPL-cont").off('click');
 $(".FPL-cont").click(
 function(){
 fplRemoveAlert(false);
 if (type == "Add") {
 fplAddByCat($("#FPL-input").val(),template);
 }
 else if (type == "Remove") {
 fplRemoveByCat($("#FPL-input").val(),template);
 }
 return false;
 }
 );
 }

 function fplAddByCat(cat,template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 var o = 0;
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)].replace("</choose>","");
 $.get(serverName + '/api.php?cmtitle=Category:' + cat +'&action=query&list=categorymembers&cmlimit=500')
 .done(function(fplInCategory) {
 fplPgList = fplInCategory.split(/title=&quot;(.\*)&quot; \/&gt;/);
 $.each(fplPgList,function(i,val) {
 fplOption = fplPgList[i];
 if(i % 2 === 1 && fplCurText.indexOf("[[" + fplPgList[i] + "]]") === -1) {
 o++;
 if (fplConfig.fileMode === false) {
 fplCurText += "<option>" + lsb + fplOption + rsb + "</option>";
 w++;
 var fplLast = fplPgList.slice(-1)[0];
 fplLast = fplLast.split(/cmcontinue=&quot;(.\*)&quot; \/&gt/);
 if (fplLast[1] !== undefined) {
 fplAddByCatGet(fplLast[1],cat);
 } else {
 if (w === fplPgList.length) {
 fplCurText += "</choose>";
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding pages in Category:' + cat + ' from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () { //post updated page via ajax
 fplAlert(fpl.alertAddByCatHead, fpl.alertAddByCat1 + cat + fpl.alertAddByCat2 + template + fpl.alertAddByCat3,"success");
 });
 }
 }
 } else if (fplConfig.fileMode === true) {
 $.get(serverName + '/wiki/' + val)
 .done(function(data) {
 var imgName = $(fplConfig.imgSelector,data).attr('data-image-key');
 if (imgName === undefined) {
 imgName = lsb + val + rsb;
 if (fplCurText.indexOf('link=' + val + ']]') === -1 && fplCurText.indexOf('<option>' + val + '</option>') === -1 && fplCurText.indexOf('<option>[[' + val + ']]</option>') === -1) {
 fplCurText += '<option>' + imgName + '</option>';
 w++;
 }
 } else {
 imgName = '[[File:' + imgName + '|link=' + val + ']]';
 if (fplCurText.indexOf('link=' + val + ']]') === -1 && fplCurText.indexOf('<option>' + val + '</option>') === -1 && fplCurText.indexOf('<option>[[' + val + ']]</option>') === -1) {
 fplCurText += '<option>' + imgName + '</option>';
 w++;
 }
 }
 if (w === o) {
 var fplLast = fplPgList.slice(-1)[0];
 fplLast = fplLast.split(/cmcontinue=&quot;(.\*)&quot; \/&gt/);
 if (fplLast[1] !== undefined) {
 fplAddByCatGet(fplLast[1],cat,template,o);
 } else {
 fplCurText += "</choose>";
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding pages in Category:' + cat + ' from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () { //post updated page via ajax
 fplAlert(fpl.alertAddByCatHead, fpl.alertAddByCat1 + cat + fpl.alertAddByCat2 + template + fpl.alertAddByCat3,"success");
 });
 }
 }
 });
 }
 }
 });
 });

 }

 var fplAddByCatGet = function(cont,cat,template,o) {
 var w = 0;
 $.get(serverName + '/api.php?cmtitle=Category:' + cat +'&action=query&list=categorymembers&cmlimit=500&cmcontinue=' + cont).done(function(fplInCategory) {
 fplPgList = fplInCategory.split(/title=&quot;(.\*)&quot; \/&gt;/);
 $.each(fplPgList,function(i,val) {
 fplOption = fplPgList[i];
 if(i % 2 === 1 && fplCurText.indexOf("<option>[[" + fplPgList[i] + "]]</option>") === -1 && fplCurText.indexOf("<option>" + fplPgList[i] + "</option>") === -1) {
 if (fplConfig.fileMode === false) {
 fplCurText += "<option>" + lsb + fplOption + rsb + "</option>";
 w++;
 var fplLast = fplPgList.slice(-1)[0];
 fplLast = fplLast.split(/cmcontinue=&quot;(.\*)&quot; \/&gt/);
 if (fplLast[1] !== undefined) {
 fplAddByCatGet(fplLast[1],cat);
 } else {
 if (w === fplPgList.length) {
 fplCurText += "</choose>";
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding pages in Category:' + cat + ' from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () { //post updated page via ajax
 fplAlert(fpl.alertAddByCatHead, fpl.alertAddByCat1 + cat + fpl.alertAddByCat2 + template + fpl.alertAddByCat3,"success");
 });
 }
 }
 } else if (fplConfig.fileMode === true) {
 $.get(serverName + '/wiki/' + val)
 .done(function(data) {
 var imgName = $(fplConfig.imgSelector,data).attr('data-image-key');
 if (imgName === undefined) {
 imgName = lsb + val + rsb;
 if (fplCurText.indexOf('link=' + val + ']]') === -1 && fplCurText.indexOf('<option>' + val + '</option>') === -1 && fplCurText.indexOf('<option>[[' + val + ']]</option>') === -1) {
 fplCurText += '<option>' + imgName + '</option>';
 w++;
 }
 } else {
 imgName = '[[File:' + imgName + '|link=' + val + ']]';
 if (fplCurText.indexOf('link=' + val + ']]') === -1 && fplCurText.indexOf('<option>' + val + '</option>') === -1 && fplCurText.indexOf('<option>[[' + val + ']]</option>') === -1) {
 fplCurText += '<option>' + imgName + '</option>';
 w++;
 }
 }
 if (w === o) {
 var fplLast = fplPgList.slice(-1)[0];
 fplLast = fplLast.split(/cmcontinue=&quot;(.\*)&quot; \/&gt/);
 if (fplLast[1] !== undefined) {
 fplAddByCatGet(fplLast[1],cat);
 } else {
 fplCurText += "</choose>";
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding pages in Category:' + cat + ' from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () { //post updated page via ajax
 fplAlert(fpl.alertAddByCatHead, fpl.alertAddByCat1 + cat + fpl.alertAddByCat2 + template + fpl.alertAddByCat3,"success");
 });
 }
 }
 });
 }
 }
 });
 });

 };

 //Remove pages by category
 function fplRemoveByCat(cat,template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 var fplOption = [];
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)].replace("</choose>","");
 fplCurText = fplCurText.replace("<choose>","");
 var fplPgList = fplCurText.split(/<\/option>/g);
 $.each(fplPgList, function(i) {
 fplPgList[i] = fplPgList[i].replace(/<[option.\*>]\*?\[\[File:.\*\|link=/,"");
 fplPgList[i] = fplPgList[i].replace(/<option>/g,"");
 fplPgList[i] = fplPgList[i].replace('[[','');
 fplPgList[i] = fplPgList[i].replace(']]','');
 fplPgList[i] = $.trim(fplPgList[i]);
 fplOption.push(fplPgList[i]);
 $.get(serverName + "/wiki/" + fplOption[i])
 .done(function(fplPgCats) {
 fplPgCats = fplPgCats.split(/wgCategories=\[(.\*?)\]/)[1];
 if (fplPgCats.indexOf(cat) !== -1) {
 fplCurText = fplCurText.replace("<option>[[" + fplOption[i] + "]]</option>","");
 fplCurText = fplCurText.replace("<option>" + fplOption[i] + "</option>","");
 var regex = new RegExp('<[option.\*>]\*?\\[\\[File:.\*\\|link=' + fplOption[i] + '\\]\\]<\\/option>');
 fplCurText = fplCurText.replace(regex,'');
 console.log(regex);
 }
 })
 .always(function(fplPgCats) {
 w++;
 if (w === fplPgList.length) {
 fplCurText = "<choose>" + fplCurText + "</choose>";
 fplCurText = fplCurText.replace(/^\s\*[\r\n]/gm,'');
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Removing pages in Category:' + cat + ' from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () { //post updated page via ajax
 fplAlert(fpl.alertRemoveByCatHead, fpl.alertRemoveByCat1 + cat + fpl.alertRemoveByCat2 + template + fpl.alertRemoveByCat3,"success");
 });
 }
 });
 });
 }

 //Create alert
 function fplAlert(title,content,type) {
 var fplOverlay = $('<div class="FPL-overlay"></div>');
 fplOverlay.appendTo($(document.body)).hide().fadeIn(200);

 var fplAlertBox = $('<div class="FPL-alert"><span class="FPL-close">X</span><div class="FPL-header">' + title + '</div></div>');
 fplOverlay.append(fplAlertBox);
 $(".FPL-close").click(function(){ fplRemoveAlert(false); return false; });

 var fplEmote;
 if (type == "success") {
 fplEmote = '<img src="http://vignette3.wikia.nocookie.net/bniceshottest/images/6/6f/Sadface-green.png" style="float:left; width:150px; margin: 0 25px;">';
 } else if (type == "fail") {
 fplEmote = '<img src="http://vignette4.wikia.nocookie.net/bniceshottest/images/6/6a/Sadface-red.png/revision/latest" style="float: left; width: 150px; margin: 0 25px;">';
 } else if (type == "logo") {
 fplEmote = '<img src="http://vignette4.wikia.nocookie.net/bniceshottest/images/b/b7/Fpl-logo-merged.png/revision/latest?cb=20151226204039" style="float: left; width: 150px; margin: 0 25px;">';
 }

 var fplBody = $(fplEmote + '<div class="FPL-content">' + content + '</div>');
 $(".FPL-alert").append(fplBody);

 var fplButtons = $('<div class="FPL-button-container"><a href="http://bniceshottest.wikia.com/wiki/Talk:Featured\_Page\_List?action=edit&section=new" target="\_blank" class="FPL-button FPL-bug">' + fpl.btnBug + '</a><a class="FPL-button FPL-cont">' + fpl.btnCont + '</a></div>');
 $(".FPL-alert").append(fplButtons);
 $(".FPL-cont").click(function(){ fplRemoveAlert(); return false; });

 if (skin === "oasis" || skin === "wikia") {
 $(".FPL-alert").css("background-color", $(".WikiaPage .WikiaPageBackground").css("background-color"));
 $(".FPL-header").css("background-color", $(".WikiHeader .navbackground").css("background-color"));
 $(".FPL-header").css("color", $(".WikiHeader .accent a").css("color"));
 $(".FPL-alert").css("background-color", $("#content").css("background-color"));
 $(".FPL-header").css("background-color", "rgba(128,128,128,0.5)");
 }
 }

 //Remove alert & check if doc should reload
 function fplRemoveAlert(reload) {
 $(".FPL-overlay").fadeOut(200, function() { $(this).remove(); });
 if (reload === false) {
 return;
 } else {
 location.reload(true);
 }
 }

 //Add the current page to FPL
 function fplAddNewPage(template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)];
 console.log(fplCurText);
 fplCurText = fplCurText.replace('</choose>','');
 if (fplCurText.indexOf("<option>[[" + pageName + "]]</option>") === -1 && fplCurText.indexOf("<option>" + pageName + "</option>") === -1 && fplCurText.indexOf("link=" + pageName + ']]') === -1) {
 if (fplConfig.fileMode === true) {
 var imgName = $(fplConfig.imgSelector).attr('data-image-key');
 if (imgName === undefined) {
 fplCurText += '<option>' + lsb + pageName + rsb + '</option></choose>';
 } else {
 fplCurText += '<option>[[File:' + imgName + '|link=' + pageName + ']]</option></choose>';
 }
 } else {
 fplCurText += '<option>' + lsb + pageName + rsb + '</option></choose>';
 }
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Adding ' + pageName + ' to featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertAddHead, fpl.alertAdd1 + template + fpl.alertAdd2,"success");
 });
 } else {
 fplAlert(fpl.alertAddErrHead, fpl.alertAddErr1 + template + fpl.alertAddErr2,"fail");
 }
 }

 function fplRemovePage(template) {
 if (template === undefined) {
 template = fplConfig.templates[0];
 }
 fplCurText = fplCurText[fplConfig.templates.indexOf(template)];
 console.log(fplCurText);
 fplCurText = fplCurText.replace("<option>[[" + pageName + "]]</option>","");
 fplCurText = fplCurText.replace("<option>" + pageName + "</option>","");
 var regex = new RegExp('<[option.\*>]\*?\\[\\[File:.\*\\|link=' + pageName + '\\]\\]<\\/option>');
 fplCurText = fplCurText.replace(regex,'');
 var fplUrl = serverName + '/api.php?action=edit&title=' + template + '&text=' + encodeURIComponent(fplCurText) + '&summary=FPL:Removing ' + pageName + ' from featured pages list&format=json&token=' + encodeURIComponent(token);
 $.post(fplUrl, function () {
 fplAlert(fpl.alertRemoveHead, fpl.alertRemove1 + template + fpl.alertRemove2,"success");
 });
 } else {
 fplAlert(fpl.alertRemoveErrHead, fpl.alertRemoveErr1 + template + fpl.alertRemoveErr2,"fail");
 }
 }
});
//

```