This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.disableAutoFill.js
219 lines (181 loc) · 7.11 KB
/
jquery.disableAutoFill.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Jquery - DisableAutoFill plugin
* The easiest solution for disabling Google Chrome auto-fill, auto-complete functions.
*
* @license MIT
* @version 1.2.8
* @author Terry, https://github.com/terrylinooo/
* @updated 2019-04-03
* @link https://github.com/terrylinooo/jquery.disableAutoFill
*/
(function($) {
'use strict';
var realFields = [];
var realFieldsMapper = {};
var realPasswordMapper = [];
var tmpPasswordMapper = [];
var passwordLenMapper = [];
// An Object for Helper functions.
var _helper = {};
// Extend the Array: add "insert" function.
Array.prototype.insert = function (index, item) {
this.splice(index, 0, item);
};
/**
* Helper function - passwordListener
* - Hide the original password string.
*
* @param {object} obj jQuery DOM object (form)
* @param {object} settings plugin settings.
*/
_helper.passwordListener = function(obj, settings) {
var passObj = (settings.passwordField === '') ? '.disabledAutoFillPassword' : settings.passwordField;
if (obj.find('[type=password]').length > 0) {
obj.find('[type=password]').attr('type', 'text').addClass('disabledAutoFillPassword');
}
obj.on('keyup', passObj, function () {
if (!this.id) {
this.id = Math.random().toString(36).substring(5);
}
if (!realPasswordMapper.hasOwnProperty(this.id)) {
realPasswordMapper[this.id] = [];
}
var realPassword = realPasswordMapper[this.id];
tmpPasswordMapper[this.id] = $(this).val();
var tmpPassword = tmpPasswordMapper[this.id];
passwordLenMapper[this.id] = tmpPassword.length;
var passwordLen = passwordLenMapper[this.id];
// Get current keyup character position.
var currKeyupPos = this.selectionStart;
for (var i = 0; i < passwordLen; i++) {
if (tmpPassword[i] !== settings.hidingChar) {
realPassword[i] = tmpPassword[i];
}
}
if (passwordLen < realPassword.length) {
var diff = realPassword.length - passwordLen;
var key = event.keyCode || event.charCode;
// Check if last keypress was backspace or delete
if (key == 8 || key == 46) {
realPassword.splice(currKeyupPos, diff);
}
// User highlighted and overwrote a portion of the password
else {
realPassword.splice(currKeyupPos - 1, diff + 1);
realPassword.insert(currKeyupPos - 1, tmpPassword[currKeyupPos - 1]);
}
}
$(this).val(tmpPassword.replace(/./g, settings.hidingChar));
if (settings.debugMode) {
console.log('Current keyup position: ' + currKeyupPos);
console.log('Password length: ' + passwordLen);
console.log('Real password:');
console.log(realPassword);
}
});
}
/**
* Helper function - formSubmitListener
* - Replace submit button to normal button to make sure everything works fine.
*
* @param {object} obj jQuery DOM object (form)
* @param {object} settings plugin settings.
*/
_helper.formSubmitListener = function(obj, settings) {
var btnObj = (settings.submitButton == '') ? '.disableAutoFillSubmit' : settings.submitButton;
obj.on('click', btnObj, function(event) {
_helper.restoreInput(obj, settings);
if (settings.callback.call()) {
if (settings.debugMode) {
console.log(obj.serialize())
} else {
// Native HTML form validation requires "type=submit" to work with.
if (settings.html5FormValidate) {
$(btnObj).attr('type', 'submit').trigger('submit');
// Change "type=submit" back to "type=button".
setTimeout(function() {
$(btnObj).attr('type', 'button');
}, 1000);
} else {
obj.submit();
}
}
}
});
};
/**
* Helper function - ramdomizeInput
* - Add random chars on "name" attribute to avid Browser remember what you submitted before.
*
* @param {object} obj jQuery DOM object (form)
* @param {object} settings plugin settings.
*/
_helper.randomizeInput = function(obj, settings) {
obj.find('input').each(function(i) {
realFields[i] = $(this).attr('name');
if(realFieldsMapper[realFields[i]]) {
$(this).attr('name', realFieldsMapper[realFields[i]]);
} else {
var randomName = Math.random().toString(36).replace(/[^a-z]+/g, '');
$(this).attr('name', randomName);
realFieldsMapper[realFields[i]] = randomName;
}
});
};
/**
* Helper function - restoreInput
* - Remove random chars on "name" attribute, so we can submit correct data then.
* - Restore password from star signs to original input password.
*
* @param {object} obj jQuery DOM object (form)
* @param {object} settings plugin settings.
*/
_helper.restoreInput = function(obj, settings) {
if (settings.randomizeInputName) {
obj.find('input').each(function(i) {
$(this).attr('name', realFields[i]);
});
}
if (settings.textToPassword) {
obj.find(settings.passwordField).attr('type', 'password');
}
obj.find(settings.passwordField).each(function (i) {
$(this).val(realPasswordMapper[this.id].join(''));
});
};
/**
* Core function
*/
$.fn.disableAutoFill = function(options) {
var settings = $.extend(
{},
$.fn.disableAutoFill.defaults,
options
);
// Add autocomplete attribute to form, and set it to 'off'
this.attr('autocomplete', 'off');
if (this.find('[type=submit]').length > 0) {
this.find('[type=submit]').addClass('disableAutoFillSubmit').attr('type', 'button');
}
if (settings.submitButton != '') {
this.find(settings.submitButton).addClass('disableAutoFillSubmit').attr('type', 'button');
}
if (settings.randomizeInputName) {
_helper.randomizeInput(this, settings);
}
_helper.passwordListener(this, settings);
_helper.formSubmitListener(this, settings);
};
$.fn.disableAutoFill.defaults = {
debugMode: false,
textToPassword: true,
randomizeInputName: true,
passwordField: '',
hidingChar: '●',
html5FormValidate: false,
submitButton: '',
callback: function() {
return true;
},
};
})(jQuery);