-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.disableAutofill.js
210 lines (183 loc) · 5.25 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
(function($) {
$.fn.disableAutoFill = function(options) {
"use strict";
var defaults = {
onSubmit: false
};
var self = {
settings: defaults,
submitHandlerSet: false,
/**
* Disable autofill for one input
* @param {Object} $input jQuery element
*/
disableAutoFill: function($input, options) {
self.settings = $.extend({}, defaults, options || {});
if (self.isBrowser('safari')) {
self.alterLabel($input);
self.alterName($input);
self.alterId($input);
}
$input.attr('autocomplete', 'off');
},
/**
* Change input's name
* Make sure Safari wont detect the word "name" in the name attribute
* otherwise Safari will enable autofill
* @param {Object} $input jQuery element
*/
alterName: function ($input) {
$input.attr('data-original-name', $input.attr('name'));
// Find unique name attribute value
var new_name = false;
var iteration = 0;
while (iteration < 10 && !new_name) {
new_name = self.random();
if (self.checkAttributeExists('name', new_name)) {
new_name = false;
}
}
if (new_name) {
$input.attr('name', new_name);
self.setFormSubmitHandler($input);
}
},
/**
* Change input's id
* Make sure Safari wont detect the word "name" in the id attribute
* otherwise Safari will enable autofill
* @param {Object} $input jQuery element
*/
alterId: function ($input) {
$input.attr('data-original-id', $input.attr('id'));
// Find unique id attribute value
var new_id = false;
var iteration = 0;
while (iteration < 10 && !new_id) {
new_id = self.random();
if (self.checkAttributeExists('id', new_id)) {
new_id = false;
}
}
if (new_id) {
$input.attr('id', new_id);
self.setFormSubmitHandler($input);
}
},
/**
* Reset input's name and id to its initial values before submitting the form
* @param {Object} $input jQuery element
*/
setFormSubmitHandler: function ($input) {
if (self.submitHandlerSet) {
return;
}
self.submitHandlerSet = true;
var $form = $input.closest('form');
if ($form.length > 0) {
$form.submit(function(event) {
var id = $input.attr('data-original-id');
if (id) {
$input.attr('id', id);
}
var name = $input.attr('data-original-name');
if (name) {
$input.attr('name', name);
}
if (typeof self.settings.onSubmit === 'function') {
var callback = self.settings.onSubmit;
return callback(event);
}
});
}
},
/**
* Make sure Safari wont detect the word "name" in the label
* otherwise Safari will enable autofill
* This injects <span> tags within the label
* @param {Object} $input jQuery element
*/
alterLabel: function ($input) {
var $label = self.findLabel($input);
if ($label && $label.length > 0) {
var text = $label.text();
var array = text.split('');
text = array.join('<span></span>');
$label.html(text);
}
},
/**
* Find label element of an input
* see http://stackoverflow.com/questions/4844594/jquery-select-the-associated-label-element-of-a-input-field
* @param $input
* @returns {*}
*/
findLabel: function ($input) {
var $label = $('label[for="'+$input.attr('id')+'"]');
if ($label.length > 0) {
return $label;
}
var $parentElem = $input.parent();
var $parentTagName = $parentElem.get(0).tagName.toLowerCase();
if ($parentTagName == "label") {
return $parentElem;
}
return null;
},
/**
* Generate a random string
* @returns {string}
*/
random: function () {
var text = '';
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i=0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
},
/**
* Check if there is an existing DOM element with a given attribute matching a given value
* @param {string} attributeName
* @param {string} attributeValue
* @returns {boolean}
*/
checkAttributeExists: function (attributeName, attributeValue) {
return $('['+attributeName+'='+attributeValue+']').length > 0;
},
/**
* Detect current Web browser
* @param {string} browser
* @returns {boolean}
*/
isBrowser: function (browser) {
// http://stackoverflow.com/questions/5899783/detect-safari-using-jquery
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_explorer = navigator.userAgent.indexOf('MSIE') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_safari = navigator.userAgent.indexOf("Safari") > -1;
var is_opera = navigator.userAgent.toLowerCase().indexOf("op") > -1;
if ((is_chrome)&&(is_safari)) {is_safari=false;}
if ((is_chrome)&&(is_opera)) {is_chrome=false;}
if (browser === 'chrome') {
return is_chrome;
}
if (browser === 'explorer') {
return is_explorer;
}
if (browser === 'firefox') {
return is_firefox;
}
if (browser === 'safari') {
return is_safari;
}
if (browser === 'opera') {
return is_opera;
}
return false;
}
};
self.disableAutoFill(this, options);
return this;
};
}(jQuery));