summaryrefslogtreecommitdiff
path: root/extensions/ConfirmEdit/resources/ext.confirmEdit.asirra.js
blob: 34296d03869ffd34b1540f0a4d6ac7a8e9b89571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*======================================================================*\
|| #################################################################### ||
|| # Asirra module for ConfirmEdit by Bachsau                         # ||
|| # ---------------------------------------------------------------- # ||
|| # This code is released into public domain, in the hope that it    # ||
|| # will be useful, but without any warranty.                        # ||
|| # ------------ YOU CAN DO WITH IT WHATEVER YOU LIKE! ------------- # ||
|| #################################################################### ||
\*======================================================================*/

jQuery( function( $ ) {
	// Selectors for create account, login, and page edit forms.
	var asirraform = $( 'form#userlogin2, #userloginForm form, form#editform' );
	var submitButtonClicked = document.createElement("input");
	var passThroughFormSubmit = false;

	function PrepareSubmit() {
		submitButtonClicked.type = "hidden";
		var inputFields = asirraform.find( "input" );
		for (var i=0; i<inputFields.length; i++) {
			if (inputFields[i].type === "submit") {
				inputFields[i].onclick = function(event) {
					submitButtonClicked.name = this.name;
					submitButtonClicked.value = this.value;
				}
			}
		}

		asirraform.submit( function() {
			return MySubmitForm();
		} );
	}

	function MySubmitForm() {
		if (passThroughFormSubmit) {
			return true;
		}
		Asirra_CheckIfHuman(HumanCheckComplete);
		return false;
	}

	function HumanCheckComplete(isHuman) {
		if (!isHuman) {
			window.alert( mediaWiki.msg( 'asirra-failed' ) );
		} else {
			asirraform.append(submitButtonClicked);
			passThroughFormSubmit = true;
			asirraform.submit();
		}
	}

	PrepareSubmit();

} );