11package com .t34400 .webviewtexture ;
22
3- public class WebViewJavaScriptConstants
4- {
5- public static final String ANDROID_INTERFACE_INSTANCE_NAME = "Android" ;
3+ public class WebViewJavaScriptConstants {
4+ public static final String ANDROID_INTERFACE_INSTANCE_NAME = "Android" ;
65
7- public static final String SCRIPT__ADD_INPUT_FOCUS_LISTENER =
8- "if (typeof currentFocusedInput === 'undefined') {" +
9- " currentFocusedInput = null;" +
10- "}" +
11- "(function() {" +
12- " var inputs = document.querySelectorAll('input, textarea');" +
13- " inputs.forEach(function(input) {" +
14- " if(input._eventHandlersAdded) {" +
15- " return;" +
16- " }" +
17- " input.addEventListener('focus', function() {" +
18- " currentFocusedInput = input;" +
19- " console.log('onInputFocusAcquired()');" +
20- " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusAcquired(input.type, input.value);" +
21- " });" +
22- " input.addEventListener('blur', function() {" +
23- " currentFocusedInput = null;" +
24- " console.log('onInputFocusLost()');" +
25- " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusLost();" +
26- " });" +
27- " input._eventHandlersAdded = true;" +
28- " });" +
29- "" +
30- " function processNode(node, array) {" +
31- " if(node.tagName === 'INPUT' || node.tagName === 'TEXTAREA') {" +
32- " array.push(node);" +
33- " }" +
34- " if(node.childNodes) {" +
35- " node.childNodes.forEach((childNode) => {" +
36- " processNode(childNode, array);" +
37- " });" +
38- " }" +
39- " }" +
40- "" +
41- " const mutationCallback = (mutationsList, observer) => {" +
42- " for (const mutation of mutationsList) {" +
43- " if (mutation.type === 'childList') {" +
44- " const addedNodes = Array.from(mutation.addedNodes);" +
45- " const addedInputs = [];" +
46- " addedNodes.forEach((addedNode) => processNode(addedNode, addedInputs));" +
47- " " +
48- " for (const addedInput of addedInputs) {" +
49- " addedInput.addEventListener('focus', function() {" +
50- " currentFocusedInput = input;" +
51- " console.log('onInputFocusAcquired()');" +
52- " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusAcquired(addedInput.type, addedInput.value);" +
53- " });" +
54- " addedInput.addEventListener('blur', function() {" +
55- " currentFocusedInput = null;" +
56- " console.log('onInputFocusLost()');" +
57- " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusLost();" +
58- " });" +
59- " addedInput._eventHandlersAdded = true;" +
60- " }" +
61- " " +
62- " const removedInputs = Array.from(mutation.removedNodes).filter(node => node.tagName === 'INPUT');" +
63- " for (const removedInput of removedInputs) {" +
64- " if(removedInput === currentFocusedInput) {" +
65- " currentFocusedInput = null;" +
66- " console.log('onInputFocusLost()');" +
67- " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusLost();" +
68- " }" +
69- " }" +
70- " }" +
71- " }" +
72- " };" +
73- "" +
74- " const observer = new MutationObserver(mutationCallback);" +
75- " const config = { childList: true, subtree: true };" +
76- " var parentElement = document.getElementsByTagName('BODY')[0];" +
77- " if (typeof parentElement === 'undefined') {" +
78- " document.addEventListener('DOMContentLoaded', function() {" +
79- " parentElement = document.getElementsByTagName('BODY')[0];" +
80- " observer.observe(parentElement, config);" +
81- " });" +
82- " } else {" +
83- " observer.observe(parentElement, config);" +
84- " }" +
85- "} ());" ;
6+ public static final String SCRIPT__ADD_INPUT_FOCUS_LISTENER = "if (typeof currentFocusedInput === 'undefined') {"
7+ +
8+ " currentFocusedInput = null;" +
9+ " canRemoveFocus = true;" +
10+ "}" +
11+ "(function() {" +
12+ " function startCooltime() {" +
13+ " canRemoveFocus = false;" +
14+ " setTimeout(() => {" +
15+ " canRemoveFocus = true;" +
16+ " }, 500);" +
17+ " }" +
18+ " var inputs = document.querySelectorAll('input, textarea');" +
19+ " inputs.forEach(function(input) {" +
20+ " if(input._eventHandlersAdded) {" +
21+ " return;" +
22+ " }" +
23+ " input.addEventListener('focus', function() {" +
24+ " currentFocusedInput = input;" +
25+ " startCooltime();" +
26+ " console.log('onInputFocusAcquired()');" +
27+ " " + ANDROID_INTERFACE_INSTANCE_NAME
28+ + ".onInputFocusAcquired(input.type, input.value);" +
29+ " });" +
30+ " input.addEventListener('blur', function() {" +
31+ " currentFocusedInput = null;" +
32+ " console.log('onInputFocusLost()');" +
33+ " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusLost();" +
34+ " });" +
35+ " input._eventHandlersAdded = true;" +
36+ " });" +
37+ "" +
38+ " function processNode(node, array) {" +
39+ " if(node.tagName === 'INPUT' || node.tagName === 'TEXTAREA') {" +
40+ " array.push(node);" +
41+ " }" +
42+ " if(node.childNodes) {" +
43+ " node.childNodes.forEach((childNode) => {" +
44+ " processNode(childNode, array);" +
45+ " });" +
46+ " }" +
47+ " }" +
48+ "" +
49+ " const mutationCallback = (mutationsList, observer) => {" +
50+ " for (const mutation of mutationsList) {" +
51+ " if (mutation.type === 'childList') {" +
52+ " const addedNodes = Array.from(mutation.addedNodes);" +
53+ " const addedInputs = [];" +
54+ " addedNodes.forEach((addedNode) => processNode(addedNode, addedInputs));" +
55+ " " +
56+ " for (const addedInput of addedInputs) {" +
57+ " if(addedInput._eventHandlersAdded) {" +
58+ " return;" +
59+ " }" +
60+ " addedInput.addEventListener('focus', function() {" +
61+ " currentFocusedInput = addedInput;" +
62+ " startCooltime();" +
63+ " console.log('onInputFocusAcquired()');" +
64+ " " + ANDROID_INTERFACE_INSTANCE_NAME
65+ + ".onInputFocusAcquired(addedInput.type, addedInput.value);" +
66+ " });" +
67+ " addedInput.addEventListener('blur', function() {" +
68+ " if (addedInput != currentFocusedInput) {" +
69+ " return;" +
70+ " }" +
71+ " currentFocusedInput = null;" +
72+ " console.log('onInputFocusLost()');" +
73+ " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusLost();" +
74+ " });" +
75+ " addedInput._eventHandlersAdded = true;" +
76+ " }" +
77+ " " +
78+ " const removedInputs = Array.from(mutation.removedNodes).filter(node => node.tagName === 'INPUT');"
79+ +
80+ " for (const removedInput of removedInputs) {" +
81+ " if(removedInput === currentFocusedInput) {" +
82+ " currentFocusedInput = null;" +
83+ " console.log('onInputFocusLost()');" +
84+ " " + ANDROID_INTERFACE_INSTANCE_NAME + ".onInputFocusLost();" +
85+ " }" +
86+ " }" +
87+ " }" +
88+ " }" +
89+ " };" +
90+ "" +
91+ " const observer = new MutationObserver(mutationCallback);" +
92+ " const config = { childList: true, subtree: true };" +
93+ " var parentElement = document.getElementsByTagName('BODY')[0];" +
94+ " if (typeof parentElement === 'undefined') {" +
95+ " document.addEventListener('DOMContentLoaded', function() {" +
96+ " parentElement = document.getElementsByTagName('BODY')[0];" +
97+ " observer.observe(parentElement, config);" +
98+ " });" +
99+ " } else {" +
100+ " observer.observe(parentElement, config);" +
101+ " }" +
102+ "} ());" ;
86103
87- public static final String SCRIPT__SET_INPUT_VALUE =
88- "var newValue = '%s';" +
89- "var focusedElement = document.activeElement;" +
90- "if (focusedElement && focusedElement.tagName === 'INPUT') {" +
91- " var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;" +
92- " nativeInputValueSetter.call(focusedElement, newValue);" +
93- " focusedElement.value = newValue;" +
94- " focusedElement.dispatchEvent(new Event('input', { bubbles: true }));" +
95- "} else if (focusedElement && focusedElement.tagName === 'TEXTAREA') {" +
96- " var nativeTextAreaValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set;" +
97- " nativeTextAreaValueSetter.call(focusedElement, newValue);" +
98- " focusedElement.value = newValue;" +
99- " focusedElement.dispatchEvent(new Event('input', { bubbles: true }));" +
100- "}" ;
104+ public static final String SCRIPT__SET_INPUT_VALUE = "var newValue = '%s';" +
105+ "var focusedElement = document.activeElement;" +
106+ "if (focusedElement && focusedElement.tagName === 'INPUT') {" +
107+ " var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;"
108+ +
109+ " nativeInputValueSetter.call(focusedElement, newValue);" +
110+ " focusedElement.value = newValue;" +
111+ " focusedElement.dispatchEvent(new Event('input', { bubbles: true }));" +
112+ "} else if (focusedElement && focusedElement.tagName === 'TEXTAREA') {" +
113+ " var nativeTextAreaValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set;"
114+ +
115+ " nativeTextAreaValueSetter.call(focusedElement, newValue);" +
116+ " focusedElement.value = newValue;" +
117+ " focusedElement.dispatchEvent(new Event('input', { bubbles: true }));" +
118+ "}" ;
101119
102- public static final String SCRIPT__REMOVE_FOCUS =
103- "var focusedElement = document.activeElement;" +
104- "if (focusedElement && (focusedElement.tagName === 'INPUT' || focusedElement.tagName === 'TEXTAREA')) {" +
105- " document.body.dispatchEvent(new KeyboardEvent('keydown', { bubbles:true, cancelable: true, key: 'Enter' }));" +
106- " focusedElement.blur();" +
107- "}" ;
120+ public static final String SCRIPT__REMOVE_FOCUS = "var focusedElement = document.activeElement;" +
121+ "if (!canRemoveFocus) {" +
122+ " return;" +
123+ "}" +
124+ "if (focusedElement && (focusedElement.tagName === 'INPUT' || focusedElement.tagName === 'TEXTAREA')) {"
125+ +
126+ " document.body.dispatchEvent(new KeyboardEvent('keydown', { bubbles:true, cancelable: true, key: 'Enter' }));"
127+ +
128+ " focusedElement.blur();" +
129+ "}" ;
108130}
0 commit comments