Includes : WinConfig

By w35l3y Last update Nov 17, 2011 — Installed 64,457 times.


Script Summary: WinConfig Function

Version: 1.0.0.7

Copyright: 2009, w35l3y (http://gm.wesley.eti.br/includes)

License: GNU GPL

Thumb Thumb Thumb Thumb Thumb Thumb

FOR SCRIPTERS/PROGRAMMERS only

If you are neither scripter nor programmer, you're discouraged to install this script directly.

Implementing

// @require        http://userscripts.org/scripts/source/54389.user.js
// @require        http://userscripts.org/scripts/source/54987.user.js

API Reference

void WinConfig.loadDefaultCss(void);
Loads the default CSS. Once loaded, the function doesn't need to be called again.
object WinConfig.init(object options);
Returns an instance of "winconfig" object based on the options passed by parameter.
options
  • string name : key name that identifies the "winconfig" object. this is used as a prefix for the stored values. see Save() for details. (default = "")
  • string type : error|explanation|question|prompt|warning or anything else (default = "")
  • string class : css class(es) you may want to add (default = "")
  • boolean load_values : determines whether the saved values should be automatically loaded (default = true)
  • string title : title (default = depends on the type)
  • string description : description (default = "")
  • array position : position (default = [-1,-1] centered)
  • array size : size (default = ["300px",0])
  • object buttons : name:{"value":"","events":{}} (default = depends on the type but basically the names are "ok","cancel" and the event is "click")
  • object sessions : nameA:{"fields":{nameB:{"value":""}}} (default = depends on the type but basically the nameA is "default" and the nameB is "text")
  • callback positiveCallback : it's a shortcut for the onclick event of the "ok" default button. the callback receives 2 parameters... the current "winconfig" object and the dom object that fired the current event.
  • callback negativeCallback : it's a shortcut for the onclick event of the "cancel" default button. the callback receives 2 parameters... the current "winconfig" object and the dom object that fired the current event.
"winconfig" object
  • object Open([parent]) : generates the dom tree for the current "winconfig" object.
  • object Save(void) : saves the field values. the pattern of stored names is winconfigName-fieldName, for example let's say the winconfig name is "name" and the field name is "test", then the stored name is "name-test" and you can retrieve its value by using GM_getValue("name-test").
  • object Close(void) : removes the dom tree from the document.
  • object FadeIn([time[, interval]]) : fades in the generated dom tree. time and interval are in miliseconds. (default = 900 and 60)
  • object FadeOut([time[, interval]]) : fades out and removes the generated dom tree at the end. time and interval are in miliseconds. (default = 900 and 60)

Examples

WinConfig.loadDefaultCss();
WinConfig.init({
	"type":"error",
	"description":"<br />An error has occurred!"
}).Open().FadeIn(0);
WinConfig.loadDefaultCss();
WinConfig.init({
	"type":"prompt",
	"description":"<br />Type something",
	"positiveCallback":function(w,e)
	{
		alert("Value: "+e.form.elements.namedItem("text").value);
		w.FadeOut();
	}
}).Open().FadeIn(0);
WinConfig.loadDefaultCss();
WinConfig.init({
	"type":"question",
	"description":"<br />Are you sure you want to proceed?"
}).Open().FadeIn(0);
WinConfig.loadDefaultCss();
WinConfig.init({
	"type":"warning",
	"description":"<br />Something has happened!"
}).Open().FadeIn(0);
WinConfig.loadDefaultCss();
WinConfig.init({
//	"name":"TrainingSchool",
	"title":"Training School : Configuration",
	"size":["500px",0],
	"description":"<br />",
	"sessions":{
		"default":{
			"fields":{
				"TempTrainingList":{"label":"Training List","help":"Training.All\t\tTrains everything\nTraining.Strength|Training.Defence\tTrains both Strength or Defence\nTraining.All&~(Training.Level)\tTrains everything except Level","default":"pet_name1:Training.All\r\npet_name2:Training.Strength|Training.Defence\r\npet_name3:Training.All&~(Training.Level)","is_multi":true},
				"PinNumber":{"label":"Pin Number","type":"password"},
				"SearchForCodestone":{"label":"Search for codestones?","type":"boolean","default":true},
				"WithdrawNp":{"label":"Withdraw Neopoints?","type":"boolean","default":true}
			}
		}
	},
	"positiveCallback":function(w,e)
	{
		var pets = e.form.elements.namedItem("TempTrainingList").textContent.split(/[\r\n]+/);
		var obj = {};
		for ( var ai = 0 , at =pets.length ; ai < at ; ++ai )
		{
			var pet = pets[ai].split(":",2);
			obj[pet[0]] = pet[1].replace(/[:;, ]+/g,"|");
		}
		GM_setValue(w.Name+"-TrainingList",uneval(obj));
		var pin = e.form.elements.namedItem("PinNumber").value;
		if (pin && !/^\d{4}$/.test(pin))
		{
			var msg = "Field 'Pin Number' must be a 4-digit number";
			WinConfig.init({"type":"error","description":"<br />"+msg}).Open().FadeIn(0);
			throw msg;
		}
		w.Save();
		w.FadeOut();
	}
}).Open().FadeIn();