OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Installs Autofill management functions on the |__gCrWeb| object. | 5 // Installs Autofill management functions on the |__gCrWeb| object. |
6 // | 6 // |
7 // It scans the DOM, extracting and storing forms and returns a JSON string | 7 // It scans the DOM, extracting and storing forms and returns a JSON string |
8 // representing an array of objects, each of which represents an Autofill form | 8 // representing an array of objects, each of which represents an Autofill form |
9 // with information about a form to be filled and/or submitted and it can be | 9 // with information about a form to be filled and/or submitted and it can be |
10 // translated to struct FormData | 10 // translated to struct FormData |
11 // (chromium/src/components/autofill/common/form_data.h) for further processing. | 11 // (chromium/src/components/autofill/core/common/form_data.h) for further |
| 12 // processing. |
12 | 13 |
13 /** | 14 /** |
14 * Namespace for this file. It depends on |__gCrWeb| having already been | 15 * Namespace for this file. It depends on |__gCrWeb| having already been |
15 * injected. | 16 * injected. |
16 */ | 17 */ |
17 __gCrWeb['autofill'] = {}; | 18 __gCrWeb['autofill'] = {}; |
18 | 19 |
19 /** | 20 /** |
20 * The maximum length allowed for form data. | 21 * The maximum length allowed for form data. |
21 * | 22 * |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 * chromium/src/components/autofill/content/renderer/form_autofill_util.h | 92 * chromium/src/components/autofill/content/renderer/form_autofill_util.h |
92 */ | 93 */ |
93 __gCrWeb.autofill.EXTRACT_MASK_OPTIONS = 1 << 2; | 94 __gCrWeb.autofill.EXTRACT_MASK_OPTIONS = 1 << 2; |
94 | 95 |
95 /** | 96 /** |
96 * The last element that was autofilled. | 97 * The last element that was autofilled. |
97 */ | 98 */ |
98 __gCrWeb.autofill.lastAutoFilledElement = null; | 99 __gCrWeb.autofill.lastAutoFilledElement = null; |
99 | 100 |
100 /** | 101 /** |
| 102 * The last element that was active (used to restore focus if necessary). |
| 103 */ |
| 104 __gCrWeb.autofill.lastActiveElement = null; |
| 105 |
| 106 /** |
101 * Scans DOM and returns a JSON string representation of forms and form | 107 * Scans DOM and returns a JSON string representation of forms and form |
102 * extraction results. | 108 * extraction results. |
103 * @param {int} requiredFields The minimum number of fields forms must have to | 109 * @param {int} requiredFields The minimum number of fields forms must have to |
104 * be extracted. | 110 * be extracted. |
105 * @param {int} requirements The requirements mask for forms, e.g. autocomplete | 111 * @param {int} requirements The requirements mask for forms, e.g. autocomplete |
106 * attribute state. | 112 * attribute state. |
107 * @return {string} A JSON encoded object with object['forms'] containing the | 113 * @return {string} A JSON encoded object with object['forms'] containing the |
108 * forms data and object['has_more_forms'] indicating if there are more | 114 * forms data and object['has_more_forms'] indicating if there are more |
109 * forms to extract. | 115 * forms to extract. |
110 */ | 116 */ |
111 __gCrWeb.autofill['extractForms'] = function(requiredFields, requirements) { | 117 __gCrWeb.autofill['extractForms'] = function(requiredFields, requirements) { |
112 var forms = []; | 118 var forms = []; |
113 // Protect against custom implementation of Array.toJSON in host pages. | 119 // Protect against custom implementation of Array.toJSON in host pages. |
114 forms.toJSON = null; | 120 forms.toJSON = null; |
115 // TODO(chenyu): check if any preparation is needed for information such as | 121 // TODO(chenyu): check if any preparation is needed for information such as |
116 // user_submitted or the one added in core.js is sufficient. | 122 // user_submitted or the one added in core.js is sufficient. |
117 var hasMoreForms = __gCrWeb.autofill.extractFormsAndFormElements( | 123 var hasMoreForms = __gCrWeb.autofill.extractFormsAndFormElements( |
118 window, | 124 window, |
119 requiredFields, | 125 requiredFields, |
120 requirements, | 126 requirements, |
121 forms); | 127 forms); |
122 var results = new __gCrWeb.common.JSONSafeObject; | 128 var results = new __gCrWeb.common.JSONSafeObject; |
123 results['has_more_forms'] = hasMoreForms; | 129 results['has_more_forms'] = hasMoreForms; |
124 results['forms'] = forms; | 130 results['forms'] = forms; |
125 return __gCrWeb.stringify(results); | 131 return __gCrWeb.stringify(results); |
126 }; | 132 }; |
127 | 133 |
128 /** | 134 /** |
| 135 * Stores the current active element. This is used to make the element active |
| 136 * again in case the web view loses focus when a dialog is presented over it. |
| 137 */ |
| 138 __gCrWeb.autofill['storeActiveElement'] = function() { |
| 139 __gCrWeb.autofill.lastActiveElement = document.activeElement; |
| 140 } |
| 141 |
| 142 /** |
129 * Fills data into the active form field. | 143 * Fills data into the active form field. |
130 * | 144 * |
131 * @param {Object} data The data to fill in. | 145 * @param {Object} data The data to fill in. |
132 */ | 146 */ |
133 __gCrWeb.autofill['fillActiveFormField'] = function(data) { | 147 __gCrWeb.autofill['fillActiveFormField'] = function(data) { |
134 var activeElement = document.activeElement; | 148 var activeElement = document.activeElement; |
| 149 if (__gCrWeb.autofill.lastActiveElement) { |
| 150 activeElement = __gCrWeb.autofill.lastActiveElement; |
| 151 activeElement.focus(); |
| 152 __gCrWeb.autofill.lastActiveElement = null; |
| 153 } |
135 if (data['name'] !== __gCrWeb['common'].nameForAutofill(activeElement)) { | 154 if (data['name'] !== __gCrWeb['common'].nameForAutofill(activeElement)) { |
136 return; | 155 return; |
137 } | 156 } |
138 __gCrWeb.autofill.lastAutoFilledElement = activeElement; | 157 __gCrWeb.autofill.lastAutoFilledElement = activeElement; |
139 __gCrWeb.autofill.fillFormField(data, activeElement); | 158 __gCrWeb.autofill.fillFormField(data, activeElement); |
140 }; | 159 }; |
141 | 160 |
142 /** | 161 /** |
143 * Fills a number of fields in the same named form. | 162 * Fills a number of fields in the same named form. |
144 * | 163 * |
(...skipping 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1382 continue; | 1401 continue; |
1383 } | 1402 } |
1384 var elementName = __gCrWeb['common'].nameForAutofill(element); | 1403 var elementName = __gCrWeb['common'].nameForAutofill(element); |
1385 var value = formData[elementName]; | 1404 var value = formData[elementName]; |
1386 if (value) { | 1405 if (value) { |
1387 element.placeholder = value; | 1406 element.placeholder = value; |
1388 } | 1407 } |
1389 } | 1408 } |
1390 } | 1409 } |
1391 }; | 1410 }; |
OLD | NEW |