RuneScape Wiki
m (as per thread)
m (this is what I meant :))
Line 10: Line 10:
 
importScript('User:Bluefire2/getHSData.js');
 
importScript('User:Bluefire2/getHSData.js');
   
(function() {
+
(function( $ ) {
 
var skill = ( typeof customSkill !== 'undefined' ? customSkill : false ); // assuming a calculator can only have one skill, and that that skill won't change
 
var skill = ( typeof customSkill !== 'undefined' ? customSkill : false ); // assuming a calculator can only have one skill, and that that skill won't change
   
Line 79: Line 79:
 
}
 
}
 
// tee hee
 
// tee hee
jQuery.expr[':'].regex = function(elem, index, match) {
+
$.expr[':'].regex = function(elem, index, match) {
 
var matchParams = match[3].split(','),
 
var matchParams = match[3].split(','),
 
validLabels = /^(data|css):/,
 
validLabels = /^(data|css):/,
Line 89: Line 89:
 
regexFlags = 'ig',
 
regexFlags = 'ig',
 
regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
 
regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
return regex.test(jQuery(elem)[attr.method](attr.property));
+
return regex.test($(elem)[attr.method](attr.property));
 
}
 
}
})();
+
})( jQuery );

Revision as of 09:58, 28 July 2014

/**
 * Configuration:
 ** customSkill
 **** Type: String
 **** The skill of the calculator. If unspecified, the script will try to detect it automatically.
 ** customInsert
 **** Type: Function(String level, String XP)
 **** If the default insertion function won't work because the calculator has a weird format, specify this.
 */ 
importScript('User:Bluefire2/getHSData.js');

(function( $ ) {
	var skill = ( typeof customSkill !== 'undefined' ? customSkill : false ); // assuming a calculator can only have one skill, and that that skill won't change

	function insertData( level, XP ) {
		var levelOrXP = $(':regex(id, ^jcForm) select:first'),
			start = $(':regex(id, ^jcForm) input:first');
		if( levelOrXP.val() === 'Level' ) {
			start.val( level );
		} else {
			start.val( XP );
		}
	}
	var insertData = ( typeof customInsert === 'undefined' ? function( level, XP ) {
			var levelOrXP = $(':regex(id, ^jcForm) select:first'),
				start = $(':regex(id, ^jcForm) input:first');
			if( levelOrXP.val() === 'Level' ) {
				start.val( level );
			} else {
				start.val( XP );
			}
		}
		: customInsert );
	function importStats( playerName, skill ) {
		getHSData( playerName, function( stats ) {
			var data = stats[ skill ];
			insertData = ( typeof customInsert === 'undefined' ? insertData : customInsert );
			insertData( data[ 0 ], data[ 1 ]);
		}, function( msg ) {
			alert( 'Unable to load data. Please try again later.' );
			console.log( msg );
		});
	}
	function init() {
		calc = ( typeof calc === 'undefined' ? wgCanonicalNamespace === 'Calculator' : calc );
		if( calc ) {
			// assign the correct skill
			if( !skill ) {
				$.each( skills, function( index ) {
					if( wgPageName.contains( this ) ) {
						skill = skills[ index ];
						return false;
					}
				});
			}
			// create the form
			var wrapper = $('<div id="bfWrapper"></div>'),
				button = $('<button id="bfGo">Import stats</button>'),
				label = $('<label name="bfPlayer">Player name: </label>'),
				input = $('<input type="text" id="bfPlayer" name="bfPlayer" />');
			wrapper.append( label )
				.append( input )
				.append( button );
			$(':regex(id, ^jc)').before( wrapper );
			$("#bfGo").on( 'click', function() {
				var player = ( $('#bfPlayer').val() !== '' ? $('#bfPlayer').val() : false );
				if( player ) {
					importStats( player, skill );
				} else {
					alert( 'Please specify a player name!' );
				}
			});
		}
	}
	// let's go
	$( init );
	String.prototype.contains = function( substring ) {
		return this.indexOf( substring ) !== -1;
	}
	// tee hee
	$.expr[':'].regex = function(elem, index, match) {
		var matchParams = match[3].split(','),
			validLabels = /^(data|css):/,
			attr = {
				method: matchParams[0].match(validLabels) ? 
							matchParams[0].split(':')[0] : 'attr',
				property: matchParams.shift().replace(validLabels,'')
			},
			regexFlags = 'ig',
			regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
		return regex.test($(elem)[attr.method](attr.property));
	}
})( jQuery );