User:Cblair91/SoF.js

// /* * Coded by Cblair91 * Based off the Dropadd script by Joeytje50 * http://runescape.wikia.com/wiki/User:Joeytje50/Dropadd.js * * Currently under recoding for personal use */ function callAPI(data, method, callback) { data['format'] = 'json'; $.ajax({		data: data,		dataType: 'json',		url: wgScriptPath + '/api.php',		type: method,		cache: false,		success: function(response) {			if (response.error)				alert('API error: ' + response.error.info);			else 				callback(response);		},		error: function(xhr, error) { alert('AJAX error: ' + error); }	}); } //--Charm logs--// //To disable charm log submissions on the monster page itself, add   var onPage = false;   just before the import. function isInt(n) { return parseInt(n) % 1 == 0; } function diff(x,y,z) { z=z?z:1; x=parseFloat(x)/z, y=parseFloat(y)/z var diff = Math.abs(x-y); return Math.round(diff*100)/100; } var onPage = onPage?onPage:true; var waterfiend = wgPageName=='Waterfiend' var submitTable = ' ' if (onPage && !wgPageName.match('/Charm_log') && $('.charmtable').length) { if (waterfiend) { $(document).ready(function{			$('#charmLogSubmit').attr('id','charmLogSubmit'+$('a[href$="&action=edit&section=new&preload=Template:Charm_log_submission_preload&editintro=Template:CharmLogPreloadedUpdateGuide&useeditor=mediawiki"]').parents('.charmpage').attr('class').split(' ')[1])		}) }	$(function {	$('a[href$="&action=edit&section=new&preload=Template:Charm_log_submission_preload&editintro=Template:CharmLogPreloadedUpdateGuide&useeditor=mediawiki"]').attr('href', function(i, val) { return val.replace(/.*\?title=([^&]*).*/, '/wiki/$1') }).click(function(e) { e.preventDefault; var wfiendadd = waterfiend?$(this).parents('.charmpage').attr('class').split(' ')[1]:'' if (!$('table#charmLogSubmit'+(waterfiend?'-':'')+wfiendadd).length) { var charmpage = wgPageName+(waterfiend?(wfiendadd=='ghorrock'?' (Ghorrock)':):)+'/Charm log' waterfiend?$('*[id^="charmLogSubmit"]:not([id$="'+wfiendadd+'"]), hr.chHr').remove:'' $(this).parent.after(submitTable+'') waterfiend?$(this).parent.next.attr('id', $(this).parent.next.attr('id')+'-'+wfiendadd):'' $('button#clSubmit').click(function {			if (parseInt($('#chTotalKills').val)<50) {				alert('The total amount of kills you submitted is below the required amount of 50. Please only submit charm data for 50 kills or more.')			} else if (parseInt($('#chTotalKills').val)>10000) {				alert('The total amount of kills you submitted it above the allowed amount. Please do not try to add results you made up.');			} else {				var total=$('#chTotalKills'), gold=$('#chGold'), green=$('#chGreen'), crimson=$('#chCrimson'), blue=$('#chBlue')				if (gold.val.length==0||green.val.length==0||crimson.val.length==0||blue.val.length==0) {					if (confirm('The inputs that were left empty will default to 0. Continue?')) {						gold.val.length==0?gold.val('0'):;						green.val.length==0?green.val('0'):;						crimson.val.length==0?crimson.val('0'):						blue.val.length==0?blue.val('0'):;						$(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison')						submitCharmLog(charmpage)					}				} else {					$(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison')					submitCharmLog(charmpage)				}			}		}) }	})	}) } else if (wgPageName.match('/Charm_log') && $('#charmguide').length) { $(document).ready(function {		$('#charmguide').html('Submit charm data: '+submitTable)		var charmpage = wgPageName;		$('button#clSubmit').click(function { if (parseInt($('#chTotalKills').val)<50||$('#chTotalKills').val=='') { alert('The total amount of kills you submitted is below the required amount of 50. Please only submit charm data for 50 kills or more.') } else { var total=$('#chTotalKills'), gold=$('#chGold'), green=$('#chGreen'), crimson=$('#chCrimson'), blue=$('#chBlue') if (gold.val.length==0||green.val.length==0||crimson.val.length==0||blue.val.length==0) { if (confirm('The inputs that were left empty will default to 0. Continue?')) { gold.val.length==0?gold.val('0'):''; green.val.length==0?green.val('0'):''; crimson.val.length==0?crimson.val('0'):'' blue.val.length==0?blue.val('0'):''; $(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison') submitCharmLog(charmpage) }				} else { $(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison') submitCharmLog(charmpage) }			}		})	}) } function submitCharmLog(charmpage) { callAPI({		'action': 'query',		'prop': 'info|revisions',		'intoken': 'edit',		'titles': charmpage.replace(/(.*)\/Charm[ _]log/, 'Charm:$1')+'|'+charmpage,		'rvprop': 'content',		'indexpageids': 'true',	}, 'GET', function(response) { console.log('test')		var summary = 'Submitted charm data using the box on the '+(wgPageName.match('/Charm_log')?'submission ':)+'page.'		var submitpage = response.query.pages[response.query.pageids[0]]		var datapage = response.query.pages[response.query.pageids[1]]		var curdata = datapage.revisions[0]['*'].replace(/(.|\n)*\|charms=/i, '|charms=').replace(/\n\}\}(.|\n)*/, ).split('\n')		datapage = datapage.title=='Charm:'+wgPageName.replace('_',' ')?submitpage:datapage		submitpage = datapage.title=='Charm:'+wgPageName.replace('_',' ')?submitpage:datapage		var total=$('#chTotalKills').val, gold=$('#chGold').val, green=$('#chGreen').val, crimson=$('#chCrimson').val, blue=$('#chBlue').val var total=parseInt(total), gold=parseInt(gold), green=parseInt(green), crimson=parseInt(crimson), blue=parseInt(blue) var perkill = parseInt(curdata[0].replace('|charms=','')) var ea = perkill; var curtot=curdata[1].replace('|kills=',), curgold=curdata[2].replace('|gold=',), curgreen=curdata[3].replace('|green=',), curcrimson=curdata[4].replace('|crimson=',), curblue=curdata[5].replace('|blue=','') curtot=parseInt(curtot), curgold=parseInt(curgold), curgreen=parseInt(curgreen), curcrimson=parseInt(curcrimson), curblue=parseInt(curblue) if (!isInt(gold/perkill)||!isInt(green/perkill)||!isInt(crimson/perkill)||!isInt(blue/perkill)||(gold+green+crimson+blue)/perkill>total) { alert('It is not possible to get that amount of charms. You '+((gold+green+crimson+blue)/perkill>total?'submitted more charm drops than kills':"submitted an amount of charms that can't be divided by "+perkill+', the amount of charms you get per kill')) $('button#clSubmit').removeAttr('disabled').html('Submit').next.remove } else { if (curtot>500&&(diff(curgold/curtot,gold/total,ea)>0.1 || diff(curgreen/curtot,green/total,ea)>0.1 || diff(curcrimson/curtot,crimson/total,ea)>0.1 || diff(curblue/curtot,blue/total,ea)>0.1)) { if (curtot>500&&(diff(curgold/curtot,gold/total,ea)>0.25 || diff(curgreen/curtot,green/total,ea)>0.25 || diff(curcrimson/curtot,crimson/total,ea)>0.25 || diff(curblue/curtot,blue/total,ea)>0.25) || (gold+green+crimson+blue == 0)) { alert('Please do not try to use this script to vandalise the wiki. If you do that, you will be blocked.'); return false; } else { summary += ' /*(Possible vandalism/spam)*/' }			}			var template = '{{Charm log submission\n|kills='+total+'\n|gold='+gold+'\n|green='+green+'\n|crimson='+crimson+'\n|blue='+blue+'\n\}\}' if (submitpage.revisions[0]['*'].match(new RegExp(template))) { alert('thank you for your submission. The page will now be reloaded.') document.location.replace(wgArticlePath.replace('$1', wgPageName)) return false; }			$('#loadmsg').html('Submitting edit...') callAPI({				'minor': 'yes',				'summary': summary,				'action': 'edit',				'title': charmpage,				'basetimestamp': submitpage.revisions[0].timestamp,				'startimestamp': submitpage.starttimestamp,				'token': submitpage.edittoken,				'appendtext': template			}, 'POST', function(response) {				alert('thank you for your submission. The page will now be reloaded.')				$('button#clSubmit').removeAttr('disabled').html('Submit').next.remove				document.location.replace(wgArticlePath.replace('$1', wgPageName))			}); }	}) }