2 * Copyright 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * This source code is a base script of web voice touch.
24 * vc_docs holds analysed documents' first hint number and last hint number
25 * @member doc document elements.
26 * @member start the first tooltip number in @doc.
27 * @member end the last tooltip number in @doc.
31 vc_conflict_timer = null,
34 * vc_conflict_links holds references to links that match the same keyword
36 vc_conflict_links = [];
39 * vc_scr holds info about screen and website
40 * @member top coordinate of the top boundary.
41 * @member left coordinate of the left boundary.
42 * @member bottom coordinate of the bottom boundary.
43 * @member right coordinate of the right boundary.
48 bottom : window.innerHeight,
49 right : window.innerWidth
53 * vc_xpath_query is a query that processes DOM tree to get clickable objects.
54 * It analyses HTML tree as it is XML and returns matching objects in one of selected forms.
56 vc_xpath_query = ".//xhtml:input[not(@type='hidden')] | .//xhtml:a | .//xhtml:area | .//xhtml:textarea | .//xhtml:button | .//xhtml:*[" +
57 "@role='link' or @role='button' or @role='checkbox' or @role='combobox' or @role='radio'] |" +
58 " .//xhtml:div[contains(@class, 'ui-btn')] | .//xhtml:div[contains(@class, 'sideBarButton')] | .//xhtml:div[contains(@class, 'buttonCaption')]",
61 * vc_page_hints holds all hints on website
66 * vc_hint_div is a DIV that gathers all hints
71 * vc_high_div is a DIV that gathers red highlight boxes
76 * vc_hint_num is used to numerate hints from 1 on every screen they're displayed
81 * vc_visible_hints holds all hints that are currently visible
83 vc_visible_hints = [],
86 * vc_bad_iframes holds iframes that should be highlighted with red color
91 * vc_log_area is a textarea element for log texts.
93 vc_log_area = document.createElement('textarea'),
96 * vc_html_area is a textarea element to read raw source code of html document in TV.
98 vc_html_area = undefined,
101 * vc_asr_result is a div element shows the asr result for debugging.
103 vc_asr_result = undefined,
106 * vc_rec_result is a div element shows the recognition result of document searching for debugging.
108 vc_rec_result = undefined,
111 * vc_flag_log is a flag variable. If it is true, vc_log_area is created. Otherwise, vc_log_area is nothing.
116 * vc_all_elem is a array that includes whole element in html document for text searching.
118 vc_all_elem = undefined,
121 * vc_flag_conflict is a flag variable. When multiple elements are selected, it is true. Otherwise, it is false.
123 vc_flag_conflict = false,
126 * vc_flag_hint_exist is a flag variable. When tooltips are already exist, it is true. Otherwise, it is false.
128 vc_flag_hint_exist = false,
131 * vc_flag_make_tooltip is a flag variable. When creating tooltip is set, it is true. Otherwise, it is false.
133 vc_flag_make_tooltip = true,
136 * vc_text_indicators is an array that contains text indicator elements
138 vc_text_indicators = [],
141 * vc_made_hint shows the state tooltip making.
143 vc_made_hint = false,
146 * vc_tag_index stores index number of the set of tooltips in web page.
147 * this variable prevents making duplicated tooltips.
152 * prototypes of overridable custom function
154 function vc_custom_pre_generate_hints() {
155 vc_print_log('No custom script');
158 function vc_custom_pre_show_hints() {
159 vc_print_log('No custom script');
162 function vc_custom_pre_remove_hints() {
163 vc_print_log('No custom script');
166 function vc_custom_pre_hide_hints() {
167 vc_print_log('No custom script');
169 /* ======================== */
172 * vc_custom_add_xpath_query functino adds given condition to xpath query
174 * @param className the name of the class of DOM elements
175 * @param DOMtype(optional) the type name of DOM element
177 * @remind this function must be called in page specific script
179 function vc_custom_add_xpath_query(className, DOMtype) {
180 if (undefined == DOMtype) {
184 vc_xpath_query += " | .//xhtml:" + DOMtype + "[contains(@class, '" + className + "')]";
188 * vc_is_child check if element is a child of other element
190 * @param child child to check the parent of
191 * @param parent to test to
193 * @return true if elements are related, false otherwise
195 function vc_is_child(child, parent) {
196 var node = child.parentNode;
197 while (node && node != document.body) {
198 if (node === parent) {
201 node = node.parentNode;
207 * vc_is_hidden function checks if the element given as parameter is hidden due to its CSS style
209 * @param elem element to be checked
210 * @param predecessor predecessor of elem to check visibility if elementFromPoint doesn't return elem
212 * @return true if element is invisible, false otherwise
214 function vc_is_hidden(elem, predecessor) {
215 if (elem == undefined) {
219 /* if predecesor is no found assume parent is one */
220 if (!predecessor && elem.parentNode) {
221 predecessor = elem.parentNode;
223 /* check visibility in css style */
224 var doc = elem.ownerDocument,
225 win = doc.defaultView,
226 computedStyle = win.getComputedStyle(elem, null);
228 if (computedStyle.getPropertyValue('visibility') !== 'visible' || computedStyle.getPropertyValue('display') === 'none') {
232 /* object is visible check if it is on current screen */
233 var rect = elem.getBoundingClientRect(),
234 x = rect.left + (rect.width / 2),
235 y = rect.top + (rect.height / 2),
238 if (y < 0 && x >= 0) {
240 } else if (y < 0 && x < 0) {
242 } else if (y >= 0 && x < 0) {
246 point = doc.elementFromPoint(x, y);
247 /* if elementFromPoint returns HTMLBodyElement it probably is a scroll left/right list and it is hidden */
248 if (point === doc.body) {
251 /* if elementFromPoint returns predecessor it is visible */
252 if (point && predecessor && vc_is_child(point, predecessor)) {
260 * vc_is_visible function checks if the element provided as argument is currently visible on screen
262 * @param elem element to be checked
263 * @param scr screen information
264 * @param isTextLink determines if the element is text link
266 * @return true if visible false otherwise
268 function vc_is_visible(elem, scr, isTextLink) {
269 if (elem == undefined) {
277 bottom : window.innerHeight,
278 right : window.innerWidth
281 // if only two parameters passed to function assume isTextLink is false
282 isTextLink = typeof isTextLink !== 'undefined' ? isTextLink : false;
284 var docy = elem.ownerDocument,
285 win = docy.defaultView,
286 computedStyle = win.getComputedStyle(elem, null);
288 /* element is not visible if it is fully transparent except select box */
289 if (parseFloat(computedStyle.getPropertyValue('opacity')) === 0.0) {
293 var rect = elem.getBoundingClientRect();
295 /* element is not visible if it is not on screen */
296 if (!rect || rect.top >= scr.bottom || rect.bottom <= scr.top || rect.left >= scr.right ||
297 rect.right <= scr.left) {
301 /* element is not visible if it is hidden and it is not a text hyperlink */
302 if (!isTextLink && vc_is_hidden(elem, null)) {
310 * vc_make_hint procedure creates invisible hint for element provided as argument
312 * @param elem element for which hint is to be created
313 * @param child if child is passed, the hint will be positioned according to position of child
315 function vc_make_hint(elem, child) {
316 // vc_print_log(elem);
317 if (false == vc_flag_make_tooltip) {
323 rect = child.getBoundingClientRect();
325 if (!rect || elem.vc_tag_index == vc_tag_index) {
329 elem.vc_tag_index = vc_tag_index;
331 var doc = elem.ownerDocument;
333 /** hint object holds clickable element, type of element and created hint (HTMLSpanElement)
334 * @member elem clickable target element.
335 * @member type the type of @elem.
336 * @member child child element of @elem
337 * @member span tooltip element of @elem
346 if(elem instanceof HTMLAreaElement){
350 /* not all input objects are supported */
351 if (elem instanceof HTMLInputElement) {
352 if (elem.type === 'text' || elem.type === 'password' || elem.type === 'datetime' || elem.type === 'email' ||
353 elem.type === 'search' || elem.type === 'number' || elem.type === 'url') {
355 } else if (elem.type !== 'button' && elem.type !== 'submit' && elem.type !== 'color' && elem.type !== 'radio' &&
356 elem.type !== 'checkbox' && elem.type !== 'file' && elem.type !== 'submit' && elem.type !== 'image') {
359 /* consider TextArea element as input element */
360 } else if (elem instanceof HTMLTextAreaElement) {
364 /** span object is the visual representation of hint */
365 var span = doc.createElement('span');
366 span.id = 'vc_tooltip';
367 span.className = 'vc_number_tag_normal vc_tooltip_' + hint.type;
369 if (hint.type == 'input') {
370 span.classList.add('vc_tip_tier_first');
371 } else if (rect.height > 30) {
372 span.classList.add('vc_tip_tier_second');
374 span.classList.add('vc_tip_tier_third');
379 /* appends hint to vc_hint_div container */
380 vc_hint_div.appendChild(hint.span);
381 vc_page_hints.push(hint);
385 * nsResolver function that is used for resolving namespace in webpage document
387 * @param prefix prefix of the namespace
389 * @return namespace url
391 function nsResolver(prefix) {
393 'xhtml' : 'http://www.w3.org/1999/xhtml' //add other namespaces if needed
395 return namespace[prefix] || null;
399 * vc_generate_hints function creates hints for all clickable elements on web page
401 * @param win window element (can be different if we analyse iframe)
403 function vc_generate_hints(win) {
404 vc_print_log('== start generate hints' + win);
407 /* if win is not passed assume the global window variable */
417 vc_print_log('== some error occured in win.document' + e);
421 /* Custom pre process to generate hints */
422 vc_custom_pre_generate_hints();
424 var start = vc_page_hints.length;
426 vc_hint_div = doc.createElement('div');
427 vc_hint_div.id = 'vc_tooltip_area';
428 vc_hint_div.style.display = 'block';
430 vc_text_indicators = [];
432 /* return result as snapshot not iterator, due to problems with undefined objects */
433 vc_print_log('== start doc.evaluate');
434 var snapshot = doc.evaluate(vc_xpath_query, doc.body, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
435 vc_print_log('== snapshot length : ' + snapshot.snapshotLength);
437 /* set current screen dimensions */
441 bottom : win.innerHeight,
442 right : win.innerWidth
445 /* analyse if a hint is needed for each returned object */
446 for (var i = 0; i < snapshot.snapshotLength; i++) {
447 var element = snapshot.snapshotItem(i),
448 rect = element.getBoundingClientRect();
450 vc_made_hint = false;
451 /* executed very rarely */
452 if (rect.width === 0 || rect.height === 0) {
453 for (var j = 0; j < element.childNodes.length; j++) {
454 if (element.childNodes[j].nodeType !== 1) {
458 var style = doc.defaultView.getComputedStyle(element.childNodes[j], null);
459 if (style && style.getPropertyValue('float') !== 'none' && vc_is_visible(element.childNodes[j], vc_scr)) {
460 vc_make_hint(element, element.childNodes[j]);
471 if (vc_is_visible(element, vc_scr) && !element.attributes.disabled) {
472 if ((element instanceof HTMLButtonElement || element.classList.contains('ui-btn') || element.getAttribute('role') == 'button')) {
473 if (element.textContent.trim().replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').length > 0) {
474 vc_text_indicators[vc_text_indicators.length] = element;
476 vc_make_hint(element, element);
479 } else if (element.getAttribute('role') == 'radio' || element.getAttribute('role') == 'checkbox' || element.getAttribute('role') == 'combobox') {
480 vc_make_hint(element, element);
482 /* if the element is an anchor without text, make hint */
483 } else if (element instanceof HTMLAnchorElement) {
484 if (element.textContent.trim().length > 0) {
485 vc_text_indicators[vc_text_indicators.length] = element;
488 var cStyle = window.getComputedStyle(element, false);
489 if (cStyle.backgroundImage !== 'none' && cStyle.backgroundImage !== 'inherit') {
490 vc_make_hint(element, element);
492 } else if (element.textContent.trim().length == 0) {
493 /* if element has child with background or image element*/
495 var childs = element.querySelectorAll('*');
496 for (var sp = 0; sp < childs.length; ++sp) {
497 if (vc_is_visible(childs[sp], vc_scr)) {
498 var cstyle = win.getComputedStyle(childs[sp], false);
499 if (childs[sp].nodeName == 'IMG' || cstyle.overflow == 'hidden'
500 || (cstyle.backgroundImage !== 'none' && cstyle.backgroundImage !== 'inherit')) {
501 vc_make_hint(element, childs[sp]);
510 vc_make_hint(element, element);
515 if (isNaN(element.textContent.trim())) {
516 vc_find_img_element(element, null, element);
519 } else if (element instanceof HTMLAreaElement || element.nodeName == 'INPUT') {
520 vc_make_hint(element, element);
522 vc_find_img_element(element, null, element);
523 if (vc_made_hint == false) {
524 vc_make_hint(element, element);
530 /* append whole div with hints to body */
532 doc.body.insertBefore(vc_hint_div, doc.body.firstChild);
534 //append all text indicators to body
538 end : vc_page_hints.length - 1
542 /* analyse iframes of the website */
543 if (win === window) {
544 var frames = document.querySelectorAll('iframe');
545 for (var f = 0; f < frames.length; f++) {
548 elem = frames[f].contentWindow.document;
550 vc_bad_iframes.push(frames[f]);
556 bottom : win.innerHeight,
557 right : win.innerWidth
559 if (elem == undefined) {
560 vc_bad_iframes.push(frames[f]);
563 if (elem != undefined && !vc_is_visible(frames[f], vc_scr)) {
566 // vc_generate_hints(frames[f].contentWindow);
570 vc_flag_hint_exist = true;
571 vc_print_log('== end');
575 * vc_find_img_element function find element containing image. if found then call a function to make a tooltip.
577 * @param elem element to search for text
578 * @param parent parent element of the @elem
579 * @param snapshotElem the first @elem when vc_generate_hints calls this function
581 function vc_find_img_element(elem, parent, snapshotElem) {
583 if (elem.nodeType == 1) {
584 var cStyle = document.defaultView.getComputedStyle(elem, false);
585 if ((vc_is_visible(elem, vc_scr) && cStyle && cStyle.backgroundImage != 'none' && cStyle.backgroundImage != 'inherit' || elem.nodeName == 'IMG')) {
586 vc_make_hint(snapshotElem, elem);
592 var children = elem.childNodes;
593 for (var ch = 0; !vc_made_hint && ch < children.length; ch++) {
594 vc_find_img_element(children[ch], elem, snapshotElem);
601 * vc_show_hints function displays hints numbering from 1
603 function vc_show_hints() {
604 /* Custom pre process to show hints */
605 vc_custom_pre_show_hints();
607 if (vc_flag_log == true) {
608 if (vc_asr_result == undefined) {
609 vc_asr_result = document.createElement('div');
610 vc_asr_result.id = 'vc_asr_result';
611 vc_asr_result.innerHTML = 'ASR Result';
612 document.body.appendChild(vc_asr_result);
615 if (vc_rec_result == undefined) {
616 vc_rec_result = document.createElement('div');
617 vc_rec_result.id = 'vc_rec_result';
618 document.body.appendChild(vc_rec_result);
623 for (var d = 0; d < vc_docs.length; d++) {
624 var doc = vc_docs[d];
625 for (var i = doc.start; i <= doc.end; i++) {
626 var win = doc.doc.defaultView,
627 hint = vc_page_hints[i],
628 rect = hint.child.getClientRects()[0];
633 bottom : win.innerHeight,
634 right : win.innerWidth
637 hint.span.style.display = 'block';
646 position_factor_left;
648 if (vc_flag_conflict) {
649 position_factor_top = 18;
650 position_factor_left = 18;
651 hint.span.className = 'vc_text_indicator_conflict_normal vc_tooltip_normal vc_tip_tier_second';
653 position_factor_top = 15;
654 position_factor_left = 15;
657 if (rect.top < position_factor_top) {
658 topPos = win.pageYOffset;
660 topPos = rect.top + win.pageYOffset - position_factor_top;
663 if (rect.left < position_factor_left) {
664 leftPos = win.pageXOffset;
666 leftPos = rect.left + win.pageXOffset - position_factor_left;
669 if (hint.type == 'area') {
670 coord = hint.elem.coords.split(',');
671 leftPos = parseFloat(leftPos) + parseFloat(coord[0]);
672 topPos = parseFloat(topPos) + parseFloat(coord[1]);
674 hint.span.style.top = topPos + 'px';
675 hint.span.style.left = leftPos + 'px';
676 hint.span.textContent = vc_hint_num;
678 hint.span.style.display = 'block';
680 if (-1 == vc_visible_hints.indexOf(hint)) {
681 vc_visible_hints.push(hint);
688 // for (var bf = 0; bf < vc_bad_iframes.length; ++bf) {
689 // vc_result_highlight(vc_bad_iframes[bf]);
694 * vc_remove_hints function removes all hints in the web page. It deletes both visible and hidden hints
696 function vc_remove_hints() {
697 /* Custom pre process to remove hints */
698 vc_custom_pre_remove_hints();
700 vc_remove_highlight();
702 for (var d = 0; d < vc_docs.length; d++) {
703 var doc = vc_docs[d].doc;
704 var tooltipArea = doc.querySelector('#vc_tooltip_area');
705 if (null !== tooltipArea && tooltipArea.parentNode) {
706 tooltipArea.parentNode.removeChild(tooltipArea);
709 vc_visible_hints = [];
713 /** if the rotation occured refresh the window information */
717 bottom : window.innerHeight,
718 right : window.innerWidth
722 vc_flag_hint_exist = false;
723 vc_flag_conflict = false;
727 * vc_hide_hints function hides the hints from the screen. Hints elements still exist
729 function vc_hide_hints() {
730 /* Custom pre process to hide hints */
731 vc_custom_pre_hide_hints();
733 for (var i = 0; i < vc_visible_hints.length; i++) {
734 var hint = vc_visible_hints[i];
735 if (hint.span.classList.contains('vc_tooltip_focus') == false) {
736 hint.span.style.display = 'none';
737 hint.span.style.top = 0;
738 hint.span.style.left = 0;
744 * vc_remove_popup function removes popup.
745 * This function is safe to call if the popup doesnt exist.
747 function vc_remove_popup() {
748 var node = document.querySelector('#vc_popup');
749 if (null !== node && node.parentNode) {
750 node.parentNode.removeChild(node);
755 * vc_show_popup function displays a floating div over the web page with text in it for timout milliseconds
757 * @param text text to be shown on the popup
758 * @param timeout time for which the popup should be shown in milliseconds. If timeout is 0 popup must be deleted manually
760 function vc_show_popup(text, timeout) {
761 /* remove any existing popup */
764 /* overlay creates a light grey area over the whole website */
765 var overlay = document.createElement('div');
766 overlay.id = 'vc_popup';
768 /* vcpopup is the main popup window, that is centered over the overlay */
769 var vcpopup = document.createElement('div');
770 vcpopup.id = 'vc_popup_content';
772 /* content is used to display text and center it vertically inside vcpopup */
773 var content = document.createElement('div');
774 content.style.display = 'table-cell';
775 content.style.fontFamily = '"Open Sans", "Helvetica", sans-serif';
776 content.style.verticalAlign = 'middle';
777 content.innerHTML = text;
779 vcpopup.appendChild(content);
780 overlay.appendChild(vcpopup);
781 document.body.appendChild(overlay);
783 /* if timeout is set to 0, display popup until not deleted manually */
785 setTimeout(function () {
794 * vc_remove_highlight removes highlight from conflicting links
796 * @param selectedNum the number of target tooltip
798 function vc_remove_highlight(selectedNum) {
800 var high = document.querySelectorAll('#vc_highlight');
801 for (var cn = 0; cn < high.length; cn++) {
802 if ((cn + 1) != selectedNum) {
803 high[cn].parentNode.removeChild(high[cn]);
807 var highlightArea = document.querySelector('#vc_highlight_area');
809 highlightArea.parentNode.removeChild(highlightArea);
815 * vc_result_highlight creates highlight box over elem
817 * @param elem element to be highlighted
819 function vc_result_highlight(elem) {
820 var doc = elem.ownerDocument,
823 if (vc_is_visible(elem) == false && elem.querySelector('img')) {
824 rect = elem.querySelector('img').getClientRects()[0];
826 rect = elem.getClientRects()[0];
833 vc_high_div = document.querySelector('#vc_highlight_area');
836 vc_high_div = doc.createElement('div');
837 vc_high_div.id = 'vc_highlight_area';
838 doc.body.appendChild(vc_high_div);
841 var high_span = doc.createElement('div');
842 high_span.id = 'vc_highlight';
844 /* top left position of the element */
845 var leftPos = (rect.left > vc_scr.left ? rect.left : vc_scr.left);
846 var topPos = (rect.top > vc_scr.top ? rect.top : vc_scr.top);
848 /* adjust top left position with page scroll */
849 topPos += window.pageYOffset - 3;
850 leftPos += window.pageXOffset - 3;
852 high_span.style.left = leftPos + 'px';
853 high_span.style.top = topPos + 'px';
854 high_span.style.width = rect.width + 'px';
855 high_span.style.height = (rect.height + 6) + 'px';
857 /* if element's dimensions are 0x0 create highlight for it's children */
858 if (rect.width === 0 || rect.height === 0) {
859 var height = topPos + rect.height;
860 var width = leftPos + rect.width;
861 for (var i = 0; i < elem.children.length; i++) {
862 var child = elem.children[i];
863 var chRect = child.getClientRects()[0];
867 if (chRect.top + chRect.height > height) {
868 height = chRect.top + chRect.height;
870 if (chRect.left + chRect.width > width) {
871 width = chRect.left + chRect.width;
874 high_span.style.height = height - topPos + 'px';
875 high_span.style.width = width - leftPos + 'px';
878 vc_high_div.appendChild(high_span);
882 * vc_generate_conflict_hints function creates hints for all conflicting highlighted hints
884 function vc_generate_conflict_hints() {
885 vc_hint_div = document.querySelector('#vc_tooltip_area');
888 vc_hint_div = document.createElement('div');
889 vc_hint_div.id = 'vc_tooltip_area';
890 document.body.insertBefore(vc_hint_div, document.body.firstChild);
896 bottom : window.innerHeight,
897 right : window.innerWidth
901 var conflicts = vc_conflict_links;
903 for (var i = 0; i < conflicts.length; i++) {
904 var elem = conflicts[i];
907 vc_result_highlight(elem);
908 if (vc_is_visible(elem, vc_scr) == false && elem.querySelector('img') != null) {
909 rect = elem.querySelector('img').getClientRects()[0];
911 rect = elem.getClientRects()[0];
920 position_factor_top = 20, //[TODO]move at proper place
921 position_factor_left = 28,
923 span = document.createElement('span');
924 span.id = 'vc_tooltip';
926 if (rect.top < position_factor_top) {
927 topPos = window.pageYOffset + 'px';
929 topPos = rect.top + window.pageYOffset - position_factor_top + 'px';
932 if (rect.left < position_factor_left) {
933 leftPos = window.pageXOffset + 'px';
935 leftPos = rect.left + window.pageXOffset - position_factor_left + 'px';
938 /* tooltip style, !please keep styles sorted */
940 hint.span.style.top = topPos;
941 hint.span.style.left = leftPos;
942 vc_hint_div.appendChild(hint.span);
945 hint.child = vc_high_div.childNodes[i];
946 vc_page_hints[vc_page_hints.length] = hint;
952 end : vc_page_hints.length - 1
955 vc_flag_hint_exist = true;
956 vc_flag_conflict = true;
957 vc_conflict_timer = setTimeout(vc_remove_hints, 5000);
963 * vc_trigger_event function makes custom DOM event
965 * @param node target element taking event
966 * @param eventType type of the custom event
968 function vc_trigger_event(node, eventType) {
970 var clickEvent = new Event(eventType);
971 node.dispatchEvent(clickEvent);
973 console.log('error ouccured', e);
978 * vc_do_action function creates event for selected element.
980 * @param targetElem selected element
981 * @param numberTag number of selected tooltip
982 * @param value(option) text entered into selected input element
984 * @return array including coordinates of the @targetElem and the number of targetElem.
986 function vc_do_action(targetElem, numberTag, value) {
987 vc_print_log('=== Inside vc_do_action');
988 vc_print_log(targetElem.elem.outerHTML);
990 var elem = targetElem.elem,
991 type = targetElem.type,
999 /* Create click effect element */
1000 effect = document.createElement('div');
1001 effect.id = 'vc_click_effect';
1003 if (numberTag != undefined) {
1004 position = numberTag.getBoundingClientRect();
1005 effect.style.left = (position.left + (position.width / 2) - 35) + 'px'
1006 effect.style.top = (position.top + (position.height / 2) - 35) + 'px'
1008 numberTag.classList.add('vc_tooltip_focus');
1009 numberTag.style.display = 'block';
1012 if (vc_flag_conflict) {
1013 vc_remove_highlight(numberTag.textContent.trim());
1017 position = elem.getClientRects()[0];
1018 effect.style.left = (position.left - 35) + 'px'
1019 effect.style.top = (position.top - 35) + 'px'
1022 vc_result_highlight(elem);
1023 elem.classList.add('vc_text_focus');
1025 vc_text_indicators[vc_text_indicators.length] = elem;
1028 document.body.insertBefore(effect, document.body.firstChild);
1030 /* Occurring events */
1032 if (type == 'input' && value != undefined) {
1033 setTimeout(function () {
1035 vc_trigger_event(elem, 'keyup');
1036 vc_trigger_event(elem, 'input');
1043 document.body.removeChild(effect);
1050 setTimeout(function () {
1051 if (vc_asr_result != undefined) {
1052 vc_asr_result.parentElement.removeChild(vc_asr_result);
1053 vc_asr_result = undefined;
1055 if (vc_rec_result != undefined) {
1056 vc_rec_result.parentElement.removeChild(vc_rec_result);
1057 vc_rec_result = undefined;
1060 if (numberTag == undefined) {
1061 elem.classList.remove('vc_text_focus');
1064 document.body.removeChild(effect);
1070 vc_print_log('=== Click Finish');
1073 var rect = elem.getClientRects()[0],
1074 x = rect.left + (rect.width / 2),
1075 y = rect.top + (rect.height / 2);
1077 vc_print_log('[ERROR] Target element is not valid')
1087 return [1, x / window.innerWidth, y / window.innerHeight];
1095 * vc_click function triggers click on element connected to cmd and param. Assumption is made that tooltips are numbers!
1097 * @param cmd tooltip tag number from voice-control
1098 * @param param text parameter from voice-control
1100 function vc_click(cmd, param) {
1101 vc_print_log('== start click cmd = ' + cmd + ', param = ' + param + ', visible tags = ' + vc_visible_hints.length);
1103 if (vc_flag_log == true) {
1104 if (vc_asr_result != undefined) {
1105 vc_asr_result.innerHTML = param;
1108 setTimeout(function () {
1109 if (vc_asr_result != undefined) {
1110 vc_asr_result.parentElement.removeChild(vc_asr_result);
1111 vc_asr_result = undefined;
1113 if (vc_rec_result != undefined) {
1114 vc_rec_result.parentElement.removeChild(vc_rec_result);
1115 vc_rec_result = undefined;
1120 /* pre-defined rule checker */
1121 if (vc_check_web_control(param) == true) {
1127 // if cmd has a numerical value
1128 if (isNaN(cmd) == false) {
1129 vc_print_log('== numbering tag click');
1131 // when cmd is floating point value without parameter
1132 // search exactly the same text
1133 if (Number.isInteger(cmd) == false && param == '') {
1134 for (var i = 0; i < vc_text_indicators.length; ++i) {
1135 if (vc_text_indicators[i].textContent.trim().indexOf(cmd) != -1) {
1136 return vc_do_action({
1137 elem : vc_text_indicators[i]
1142 var list = vc_selector([cmd.toString()]);
1144 for (var i = 0; i < list.length; i++) {
1145 if (list[i].childElementCount == 0 && list[i].textContent.trim().indexOf(cmd) != -1) {
1146 return vc_do_action({
1153 // when cmd is valid integer value
1154 if (Number.isInteger(cmd) == true && cmd <= vc_visible_hints.length) {
1155 var hint = vc_visible_hints[cmd - 1];
1157 // insert text into input element
1158 if (hint.type === 'input' && param != '') {
1159 return vc_do_action(hint, hint.span, param);
1160 } else if (param == '') {
1161 if (!vc_flag_conflict) {
1162 vc_conflict_links = [];
1163 vc_conflict_links.push(hint.elem);
1165 for (var i = 0; i < vc_text_indicators.length; ++i) {
1166 if (vc_text_indicators[i].textContent.trim() == cmd) {
1167 vc_conflict_links.push(vc_text_indicators[i]);
1171 if (vc_conflict_links.length > 1) {
1173 vc_generate_conflict_hints();
1174 return [vc_conflict_links.length, 0, 0];
1178 return vc_do_action(hint, hint.span);
1182 // when there is no matched tooltip
1185 return vc_search_text(cmd.toString().concat(' ', param).trim());
1189 return vc_search_text(param);
1194 * vc_search_text function search the element that include same text with @param.
1196 * @param param param from voice-control.
1198 function vc_search_text(param) {
1199 /* phase 1. search full text in the webpage */
1200 /* first, compare with links in html documents */
1201 vc_print_log('=== start searching(text level)');
1203 if (vc_flag_log == true) {
1204 vc_rec_result.style.background = 'rgba(0, 200, 100, 1)';
1210 bottom : window.innerHeight,
1211 right : window.innerWidth
1214 vc_conflict_links = [];
1216 for (var i = 0; i < vc_text_indicators.length; ++i) {
1217 if (vc_is_visible(vc_text_indicators[i], vc_scr, true) &&
1218 vc_text_indicators[i].textContent.replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase().indexOf(param.replace(/ /g, '').toLowerCase()) !== -1) {
1219 vc_conflict_links.push(vc_text_indicators[i]);
1223 if (vc_conflict_links.length == 1) {
1224 /* if there is one result */
1225 return vc_do_action({
1226 elem : vc_conflict_links[0]
1228 } else if (vc_conflict_links.length > 1) {
1229 vc_generate_conflict_hints();
1231 return [vc_conflict_links.length, 0, 0];
1234 /* second, compare with whole text of elements in html documents */
1235 var resultTokenArr = param.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').split(' '),
1236 el = vc_selector(resultTokenArr),
1239 for (var i = 0; el.length > i; i++) {
1242 if (temp.childElementCount === 0) {
1243 var curStr = temp.textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
1244 if (curStr == preStr || vc_is_visible(temp) == false) {
1250 vc_conflict_links.push(temp);
1254 if (vc_conflict_links.length == 1) {
1255 return vc_do_action({
1256 elem : vc_conflict_links[0]
1258 } else if (vc_conflict_links.length > 1) {
1259 vc_generate_conflict_hints();
1261 return [vc_conflict_links.length, 0, 0];
1264 * vc_search_word depend on the voice control language.
1265 * This function increase the accuracy of the searching result using word and letter level searching.
1266 * If the language is changed, the definition of the function will be changed by reloading the .js file.
1268 temp = vc_search_word(param, true);
1269 if (temp != undefined) {
1270 return vc_do_action({
1274 //vc_show_popup('There is no such kind of string.<br>(' + param + ')', 1500);
1283 * vc_scroll_event_firing function move the scroll.
1285 * @param evt keyword to move the scroll.
1287 function vc_scroll_event_firing(evt) {
1288 if (evt == 'DOWN') {
1289 window.scrollBy(0, 500);
1290 } else if (evt == 'UP') {
1291 window.scrollBy(0, -500);
1292 } else if (evt == 'TOP') {
1293 window.scrollTo(0, 0);
1298 * vc_print_html function print out log on vc_log_area and console for debug.
1299 * If vc_flag_log is not true, then the logs will only print on console.
1301 * @param text log text to print out.
1303 function vc_print_log(text) {
1305 var time = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ':' + d.getMilliseconds();
1306 console.log(time, '[', vc_print_log.caller.name, ']', text);
1308 if (vc_flag_log == true) {
1309 vc_log_area.value += time + ' [' + vc_print_log.caller.name + '] ' + text + '\n';
1314 * vc_print_html function print out raw source of the html document for debug.
1316 function vc_print_html() {
1317 if (vc_html_area == undefined) {
1318 vc_html_area = document.createElement('textarea');
1319 vc_html_area.disabled = true;
1320 vc_html_area.id = 'vc_html_area';
1322 document.body.appendChild(vc_html_area);
1324 vc_html_area.value = location.href.toString() + '\n' + document.querySelector('html').innerHTML;
1326 document.body.removeChild(vc_html_area);
1327 vc_html_area = undefined;
1332 * vc_search_text function is called by voice-control-webview.cpp.
1333 * this function call some group of functions refered to @phase.
1335 * @param phase key value to call a group of functions.
1336 * @param data function data from voice-control-webview.cpp.
1338 function vc_request_action(phase, data) {
1339 vc_print_log('phase :' + phase + ',data :' + data);
1340 if ('SHOW_TOOLTIP' == phase) {
1341 clearTimeout(vc_conflict_timer);
1343 /* create & show tooltips to recognize the voice */
1344 if (!vc_flag_conflict && false == vc_flag_hint_exist) {
1346 vc_generate_hints();
1349 } else if ('HIDE_TOOLTIP' == phase) {
1351 if (!vc_flag_conflict) {
1354 clearTimeout(vc_conflict_timer);
1355 vc_conflict_timer = setTimeout(vc_remove_hints, 5000);
1357 } else if ('RESULT' == phase) {
1358 /* do action by the result */
1359 vc_all_elem = document.querySelectorAll('body *:not(script)');
1360 var result = vc_correct_parameter(data);
1362 return vc_click(result.cmd, result.param);
1363 } else if ('REMOVE_TOOLTIP' == phase) {
1364 clearTimeout(vc_conflict_timer);
1373 * vc_selector function is a selector function to find the elements include whole text in @strArr.
1375 * @param strArr group of strings to find elements.
1377 * ex) vc_selector(['abc', 'def']);
1379 function vc_selector(strArr) {
1381 for (var i = 0; i < vc_all_elem.length; i++) {
1382 var temp = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase();
1385 for (j = 0; j < strArr.length; j++) {
1386 if (temp.includes(strArr[j].toLowerCase()) == false) {
1391 if (strArr.length == j && vc_is_visible(vc_all_elem[i], null, true)) {
1392 result.push(vc_all_elem[i]);
1400 * vc_get_url_path function returns url path of the view.
1402 * @return part of the url(host name + path name). it is used to read page specific script.
1404 function vc_get_url_path() {
1405 var hostName = document.location.hostname;
1406 var pathName = document.location.pathname;
1408 pathName = pathName.replace(/\//gi, '.');
1410 return hostName + '/' + pathName;
1414 * vc_get_conflict_status function returns the conflict status.
1416 * @return 1 if vc_flag_conflict is true. Otherwise 0.
1418 function vc_get_conflict_status() {
1419 if (vc_flag_conflict) {
1427 * vc_set_make_tooltip function sets the vc_flag_make_tooltip.
1429 * @param makeTooltip If it is 1, then set vc_flag_make_tooltip as true. Otherwise false.
1431 function vc_set_make_tooltip(makeTooltip) {
1432 if (1 == makeTooltip) {
1433 vc_flag_make_tooltip = true;
1435 vc_flag_make_tooltip = false;
1440 * vc_init function initialize some elements and styles to use voice control.
1442 function vc_init() {
1443 /* log element initialize */
1445 vc_log_area.disabled = true;
1446 vc_log_area.id = 'vc_log_area';
1447 vc_log_area.style.visibility = 'hidden';
1449 document.body.appendChild(vc_log_area);
1452 /* add tooltip style element */
1453 var vc_style_node = document.createElement('style');
1454 var vc_style_text = document.createTextNode(
1455 '#vc_tooltip_area {\
1457 position : absolute;\
1458 pointer-events: none;\
1460 z-index : 2147483647;\
1462 #vc_html_area, #vc_log_area {\
1463 background-color : rgba(0, 0, 0, 0.5) !important;\
1464 color : white !important;\
1468 padding : 10px 10px 10px 10px;\
1472 z-index : 2147483647;\
1474 #vc_popup_content {\
1475 background-color : #87cff7;\
1477 border-radius : 20px;\
1478 box-shadow : 0 2px 6px rgba(0, 0, 0, 0.3), 0 3px 8px rgba(0, 0, 0, 0.2);\
1484 line-height : 1.5em;\
1488 position : absolute;\
1490 text-align : center;\
1493 z-index : 2147483647;\
1496 background-color : rgba(0,0,0,0.2);\
1502 z-index : 2147483647;\
1504 #vc_highlight_area {\
1506 position : absolute;\
1507 pointer-events: none;\
1509 z-index : 2147483646;\
1512 border-radius: 5px;\
1515 position : absolute;\
1516 pointer-events: none;\
1517 word-break : normal;\
1518 background-color: rgba(0, 234, 255, 0.2);\
1521 border-radius: 70px;\
1524 pointer-events: none;\
1526 z-index : 2147483647;\
1527 background: linear-gradient(141deg, rgba(15, 185, 175, 0.6) 0%, rgba(30, 200, 220, 0.6) 51%, rgba(45, 180, 230, 0.6) 75%);\
1529 .vc_number_tag_normal {\
1530 border : 2px solid;\
1531 border-color : rgba(255, 255, 255, 0);\
1532 border-radius : 5px;\
1533 box-shadow : 0px 2px 2px rgba(0, 0, 0, 0.3);\
1534 position: absolute;\
1535 pointer-events: none;\
1536 text-align: center;\
1537 font-family: "Samsung0ne600";\
1539 z-index : 2147483647;\
1540 word-break : normal;\
1542 .vc_text_indicator_conflict_normal {\
1544 border-color: rgba(255, 255, 255, 0);\
1545 border-radius: 20px;\
1546 box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);\
1547 position: absolute;\
1548 pointer-events: none;\
1549 text-align: center;\
1551 font-family: "Samsung0ne600";\
1552 z-index: 2147483647;\
1554 .vc_tooltip_focus {\
1555 border-color: rgba(255, 255, 255, 1);\
1558 color: #ff0000 !important;\
1559 font-weight: bolder !important;\
1562 background : rgba(0, 0, 0, 0.8);\
1563 border-radius : 20px;\
1567 padding-top : 10px;\
1568 padding-bottom : 10px;\
1569 position : absolute;\
1570 text-align : center;\
1574 z-index : 2147483647;\
1577 border-radius : 40px;\
1579 position : absolute;\
1583 z-index : 2147483647;\
1585 .vc_tooltip_input {\
1586 background-color : rgba(60, 140, 250, 0.75);\
1588 .vc_tooltip_normal, .vc_tooltip_area {\
1589 background-color : rgba(60, 185, 165, 0.75);\
1591 .vc_tip_tier_first {\
1597 .vc_tip_tier_second {\
1603 .vc_tip_tier_third {\
1609 @keyframes vc_tooltip_show {\
1617 @keyframes vc_tooltip_click {\
1619 box-shadow: 0 0 8px 6px rgba(60, 220, 180, 0), 0 0 0px 0px rgba(255,255,255,1), 0 0 0px 0px rgba(60, 220, 180, 0);\
1622 box-shadow: 0 0 8px 6px rgba(60, 220, 180, 1), 0 0 3px 5px rgba(255,255,255,1), 0 0 6px 10px rgba(60, 220, 180, 1);\
1625 box-shadow: 0 0 8px 6px rgba(60, 220, 180, 0), 0 0 0px 20px rgba(255,255,255,0.5), 0 0 0px 20px rgba(60, 220, 180, 0);\
1628 @keyframes vc_click_effect {\
1630 transform: scale(0.3, 0.3);\
1634 transform: scale(0.5, 0.5);\
1638 transform: scale(1, 1);\
1642 vc_style_node.appendChild(vc_style_text);
1643 document.head.appendChild(vc_style_node);
1645 var frames = window.frames;
1647 for (var i = 0; i < frames.length; i++) {
1649 var doc = frames[i].document;
1650 var vc_frame_style_node = doc.createElement('style');
1651 var vc_frame_style_text = doc.createTextNode(vc_style_node.textContent);
1653 vc_frame_style_node.appendChild(vc_frame_style_text);
1654 doc.head.appendChild(vc_frame_style_node);
1660 /* remove indicators on scroll */
1661 window.addEventListener('scroll', function () { //[TODO] need to remove the listener