Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview Adds listeners that are used to handle forms, enabling autofill | |
| 7 * and the replacement method to dismiss the keyboard needed because of the | |
| 8 * Autofill keyboard accessory. | |
| 9 */ | |
| 10 | |
| 11 goog.provide('__crWeb.form'); | |
| 12 | |
| 13 goog.require('__crWeb.message'); | |
| 14 | |
| 15 /** Beginning of anonymous object */ | |
| 16 (function() { | |
| 17 | |
| 18 /** | |
| 19 * Focus and input events for form elements are messaged to the main | |
| 20 * application for broadcast to CRWWebControllerObservers. | |
| 21 * This is done with a single event handler for each type being added to the | |
| 22 * main document element which checks the source element of the event; this | |
| 23 * is much easier to manage than adding handlers to individual elements. | |
| 24 * @private | |
| 25 */ | |
| 26 var formActivity_ = function(evt) { | |
| 27 var srcElement = evt.srcElement; | |
| 28 var fieldName = srcElement.name || ''; | |
| 29 var value = srcElement.value || ''; | |
| 30 | |
| 31 var msg = { | |
| 32 'command': 'form.activity', | |
| 33 'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement.form), | |
| 34 'fieldName': fieldName, | |
| 35 'type': evt.type, | |
| 36 'value': value | |
| 37 }; | |
| 38 __gCrWeb.message.invokeOnHost(msg); | |
| 39 }; | |
| 40 | |
| 41 /** | |
| 42 * Focus events performed on the 'capture' phase otherwise they are often | |
| 43 * not received. | |
| 44 */ | |
| 45 document.addEventListener('focus', formActivity_, true); | |
| 46 document.addEventListener('blur', formActivity_, true); | |
| 47 document.addEventListener('change', formActivity_, true); | |
| 48 | |
| 49 /** | |
| 50 * Text input is watched at the bubbling phase as this seems adequate in | |
| 51 * practice and it is less obtrusive to page scripts than capture phase. | |
| 52 */ | |
| 53 document.addEventListener('input', formActivity_, false); | |
| 54 document.addEventListener('keyup', formActivity_, false); | |
| 55 | |
| 56 /** | |
| 57 * Capture form submit actions. | |
| 58 */ | |
| 59 document.addEventListener('submit', function(evt) { | |
| 60 var action; | |
| 61 if (evt['defaultPrevented']) | |
| 62 return; | |
| 63 action = evt.target.getAttribute('action'); | |
| 64 // Default action is to re-submit to same page. | |
| 65 if (!action) { | |
| 66 action = document.location.href; | |
| 67 } | |
| 68 __gCrWeb.message.invokeOnHost({ | |
| 69 'command': 'document.submit', | |
| 70 'formName': __gCrWeb.common.getFormIdentifier(evt.srcElement), | |
| 71 'href': getFullyQualifiedURL_(action) | |
| 72 }); | |
| 73 }, false); | |
| 74 | |
| 75 /** @private */ | |
| 76 function getFullyQualifiedUrl_(originalURL) { | |
|
Eugene But (OOO till 7-30)
2017/04/10 21:42:07
nit: How about |var getFullyQualifiedUrl_ = functi
danyao
2017/04/10 22:12:59
Done.
| |
| 77 // A dummy anchor (never added to the document) is used to obtain the | |
| 78 // fully-qualified URL of |originalURL|. | |
| 79 var anchor = document.createElement('a'); | |
| 80 anchor.href = originalURL; | |
| 81 return anchor.href; | |
| 82 }; | |
| 83 | |
| 84 /** Flush the message queue. */ | |
| 85 if (__gCrWeb.message) { | |
| 86 __gCrWeb.message.invokeQueues(); | |
| 87 } | |
| 88 | |
| 89 }()); // End of anonymous object | |
| OLD | NEW |