User:Ricordisamoa/AlertStyler.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*<nowiki>
 *
 * AlertStyler.js by [[User:Ricordisamoa]]
 *
 * overrides the default JavaScript 'alert' function, used by many Gadgets and UserScripts
 * uses jQueryUI 'dialog'
 * can disable future similar messages with jQuery.jStorage
 *
*/
mw.loader.using(["jquery.ui","jquery.jStorage"],function(){
	window.alert=function(text){
		if(
			!$.jStorage.get("AlertStyler-dontShowAgain")||
			($.jStorage.get("AlertStyler-dontShowAgain")&&$.grep(
				$.jStorage.get("AlertStyler-dontShowAgain"),
				function(v,i){
					return $.compareArray(v,[wgPageName,text]);
				}
			).length==0)
		){
			var dialog=
			$("<div>")
			.append(
				typeof(text)!="undefined"?(text
				.replace(/&/g,"&amp;")
				.replace(/"/g,"&quot;")
				.replace(/'/g,"&#39;")
				.replace(/</g,"&lt;")
				.replace(/>/g,"&gt;")
				.replace(/[\n\r]/g,"<br/>")
				):"undefined"
	        );
			if($.jStorage.storageAvailable())
				var dontShowAgain=$("<input>")
				.attr("type","checkbox")
				.prependTo(
					$("<label>")
					.appendTo(dialog.append("<br/>"))
					.append("don't show again on this page")
				);
			mw.addDialog({
				title:"JavaScript alert",
				content:dialog,
				buttons:{
					"OK":function(){
						$(this).dialog("close");
						if(dontShowAgain.prop("checked")==true){
							if($.jStorage.storageAvailable()){
								var dsa=[];
								if($.jStorage.get("AlertStyler-dontShowAgain")) dsa=$.jStorage.get("AlertStyler-dontShowAgain");
								dsa.push([wgPageName,text]);
								$.jStorage.set("AlertStyler-dontShowAgain",dsa);
							}
							else throw new Error("No storage available");
						}
					}
				}
			});
		}
	};
	$(document).ready(function(){
		$(mw.util.addPortletLink("p-tb","#","AlertStyler.reset","AlertStyler-pref-reset","reset AlertStyler preferences"))
		.click(function(event){
			event.preventDefault();
			$.jStorage.deleteKey("AlertStyler-dontShowAgain");
			mw.util.jsMessage($.jStorage.get("AlertStyler-dontShowAgain")?
			"An error has been encountered while resetting AlertStyler preferences.":
			"AlertStyler preferences has been reset successfully.");
		});
	});
});
//</nowiki>