// ==UserScript==
// @name Safe alert
// @author TarquinWJ 
// @namespace http://www.howtocreate.co.uk/ 
// @version 2
// @description  Replaces JavaScript dialogs with a confirm dialog,
//			allowing you to choose if it should be allowed
//			to open any more.
// @ujs:category browser: enhancements
// @ujs:published 2005-11-15 00:11
// @ujs:modified 2005-11-15 00:16
// @ujs:documentation http://userjs.org/scripts/browser/enhancements/safe-alert 
// @ujs:download http://userjs.org/scripts/download/browser/enhancements/safe-alert.js
// ==/UserScript==


/* 
 * Please see
 * http://www.howtocreate.co.uk/operaStuff/userJavaScript.html#terms
 * for License and Terms of Use
 */

(function () {

/*********************
Configure options here
*********************/

//Set any of these to false to automatically assume you have blocked that dialog type (those dialogs will
//never be allowed to appear):
// Alerts:
var alertsArePermitted = true;
// Confirmation dialogs:
var confirmsArePermitted = true;
// Prompt dialogs:
var promptsArePermitted = true;

//How many confirm/prompt dialogs should be allowed before it checks if you want to allow any more:
var checkAfter = 5;

//Should you be allowed to re-enable dialogs (sites that attempt to abuse this can be blocked below):
var allowReEnable = true;

//By default, all blocked dialog requests will be silently ignored, and the script will continue to run.
//Use the options below to say if you want to say what should happen when you choose to block dialogs.
//Each of them gives you the option to deliberately throw an error (this will prevent the initiating script
//from continuing).

//What response should be used for alert dialogs?
// true = OK
// false = throw an error as soon as you tell it to start blocking
// null = throw an error if the script tries to open another alert
var useAlertResponse = true;

//What response should be used for confirm dialogs?
// true = OK
// false = Cancel
// null = throw an error
var useConfirmResponse = false;

//What response should be used for prompt dialogs?
// 'Some String' = return the specified string
// true = return the default string provided by the script
// false = Cancel
// null = throw an error
var usePromptResponse = true;

/*************************************
Configure site specific overrides here
*************************************/

//Here is an example (commented out - remove the comments if you specify your own overrides ;) where I
//test for two domains, and set overrides. Note that I use 'else if' - this improves the efficiency. If
//you have more than one override, then you should also chain your checks like this.
//
// if( location.hostname == 'www.example.com' ) {
// 	useConfirmResponse = true;
// 	usePromptResponse = '';
// 	checkAfter = 0;
// } else if( location.hostname == 'test.example.com' ) {
// 	alertsArePermitted = false;
// 	useConfirmResponse = null;
// 	usePromptResponse = null;
// 	checkAfter = 1;
// 	allowReEnable = false;
// }




/*** stop editing ***/

var realConfirm = window.confirm, realPrompt = window.prompt, realUndef = window.undefined, confirmCount = 0, promptCount = 0;

window.alert = function (oMsg) {
	if( alertsArePermitted ) {
		alertsArePermitted = realConfirm(oMsg+'\n\nDo you want to allow this page to open more alerts?');
		if( !alertsArePermitted && !useAlertResponse && ( typeof(useAlertResponse) == typeof(true) ) ) {
			throw({name:'DialogBlockedError',message:'Started blocking alerts'});
		}
	} else if( !useAlertResponse ) {
		throw({name:'DialogBlockedError',message:'Alert blocked'});
	}
};

window.confirm = function (oMsg) {
	if( confirmsArePermitted && ( ( confirmCount < checkAfter ) || ( confirmsArePermitted = realConfirm('This page is trying to open another confirmation dialog.\n\nDo you want to allow this page to open more confirmation dialogs?') ) ) ) {
		if( confirmCount >= checkAfter ) { confirmCount = 1; } else { confirmCount++; }
		return realConfirm(oMsg);
	} else {
		if( typeof(useConfirmResponse) == typeof(true) ) { return useConfirmResponse; }
		throw({name:'DialogBlockedError',message:'Confirmation dialog blocked'});
	}
};

window.prompt = function (oMsg,oDefault) {
	if( promptsArePermitted && ( ( promptCount < checkAfter ) || ( promptsArePermitted = realConfirm('This page is trying to open another prompt dialog.\n\nDo you want to allow this page to open more prompt dialogs?') ) ) ) {
		if( promptCount >= checkAfter ) { promptCount = 1; } else { promptCount++; }
		return realPrompt(oMsg,oDefault);
	} else {
		if( typeof(usePromptResponse) == typeof('') ) { return usePromptResponse; }
		if( typeof(usePromptResponse) == typeof(true) ) {
			return usePromptResponse ? ( '' + oDefault + '' ) : realUndef;
		}
		throw({name:'DialogBlockedError',message:'Prompt dialog blocked'});
	}
};

opera.resetDialogPermissions = function (oAlert,oConfirm,oPrompt) {
	if( !allowReEnable ) { return; }
	alertsArePermitted = oAlert;
	confirmsArePermitted = oConfirm;
	promptsArePermitted = oPrompt;
	if( oConfirm ) { confirmCount = 0; }
	if( oPrompt ) { promptCount = 0; }
};

})();