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_text_indicators is an array that contains text indicator elements
133 vc_text_indicators = [],
136 * vc_made_hint shows the state tooltip making.
138 vc_made_hint = false,
141 * vc_tag_index stores index number of the set of tooltips in web page.
142 * this variable prevents making duplicated tooltips.
147 * prototypes of overridable custom function
149 function vc_custom_pre_generate_hints() {
150 vc_print_log('No custom script');
153 function vc_custom_pre_show_hints() {
154 vc_print_log('No custom script');
157 function vc_custom_pre_remove_hints() {
158 vc_print_log('No custom script');
161 function vc_custom_pre_hide_hints() {
162 vc_print_log('No custom script');
164 /* ======================== */
167 * vc_custom_add_xpath_query functino adds given condition to xpath query
169 * @param className the name of the class of DOM elements
170 * @param DOMtype(optional) the type name of DOM element
172 * @remind this function must be called in page specific script
174 function vc_custom_add_xpath_query(className, DOMtype) {
175 if (undefined == DOMtype) {
179 vc_xpath_query += " | .//xhtml:" + DOMtype + "[contains(@class, '" + className + "')]";
183 * vc_is_child check if element is a child of other element
185 * @param child child to check the parent of
186 * @param parent to test to
188 * @return true if elements are related, false otherwise
190 function vc_is_child(child, parent) {
191 var node = child.parentNode;
192 while (node && node != document.body) {
193 if (node === parent) {
196 node = node.parentNode;
202 * vc_is_hidden function checks if the element given as parameter is hidden due to its CSS style
204 * @param elem element to be checked
205 * @param predecessor predecessor of elem to check visibility if elementFromPoint doesn't return elem
207 * @return true if element is invisible, false otherwise
209 function vc_is_hidden(elem, predecessor) {
210 if (elem == undefined) {
214 /* if predecesor is no found assume parent is one */
215 if (!predecessor && elem.parentNode) {
216 predecessor = elem.parentNode;
218 /* check visibility in css style */
219 var doc = elem.ownerDocument,
220 win = doc.defaultView,
221 computedStyle = win.getComputedStyle(elem, null);
223 if (computedStyle.getPropertyValue('visibility') !== 'visible' || computedStyle.getPropertyValue('display') === 'none') {
227 /* object is visible check if it is on current screen */
228 var rect = elem.getBoundingClientRect(),
229 x = rect.left + (rect.width / 2),
230 y = rect.top + (rect.height / 2),
233 if (y < 0 && x >= 0) {
235 } else if (y < 0 && x < 0) {
237 } else if (y >= 0 && x < 0) {
241 point = doc.elementFromPoint(x, y);
242 /* if elementFromPoint returns HTMLBodyElement it probably is a scroll left/right list and it is hidden */
243 if (point === doc.body) {
246 /* if elementFromPoint returns predecessor it is visible */
247 if (point && predecessor && vc_is_child(point, predecessor)) {
255 * vc_is_visible function checks if the element provided as argument is currently visible on screen
257 * @param elem element to be checked
258 * @param scr screen information
259 * @param isTextLink determines if the element is text link
261 * @return true if visible false otherwise
263 function vc_is_visible(elem, scr, isTextLink) {
264 if (elem == undefined) {
272 bottom : window.innerHeight,
273 right : window.innerWidth
276 // if only two parameters passed to function assume isTextLink is false
277 isTextLink = typeof isTextLink !== 'undefined' ? isTextLink : false;
279 var docy = elem.ownerDocument,
280 win = docy.defaultView,
281 computedStyle = win.getComputedStyle(elem, null);
283 /* element is not visible if it is fully transparent except select box */
284 if (parseFloat(computedStyle.getPropertyValue('opacity')) === 0.0) {
288 var rect = elem.getBoundingClientRect();
290 /* element is not visible if it is not on screen */
291 if (!rect || rect.top >= scr.bottom || rect.bottom <= scr.top || rect.left >= scr.right ||
292 rect.right <= scr.left) {
296 /* element is not visible if it is hidden and it is not a text hyperlink */
297 if (!isTextLink && vc_is_hidden(elem, null)) {
305 * vc_make_hint procedure creates invisible hint for element provided as argument
307 * @param elem element for which hint is to be created
308 * @param child if child is passed, the hint will be positioned according to position of child
310 function vc_make_hint(elem, child) {
311 // vc_print_log(elem);
314 rect = child.getBoundingClientRect();
316 if (!rect || elem.vc_tag_index == vc_tag_index) {
320 elem.vc_tag_index = vc_tag_index;
322 var doc = elem.ownerDocument;
324 /** hint object holds clickable element, type of element and created hint (HTMLSpanElement)
325 * @member elem clickable target element.
326 * @member type the type of @elem.
327 * @member child child element of @elem
328 * @member span tooltip element of @elem
337 if(elem instanceof HTMLAreaElement){
341 /* not all input objects are supported */
342 if (elem instanceof HTMLInputElement) {
343 if (elem.type === 'text' || elem.type === 'password' || elem.type === 'datetime' || elem.type === 'email' ||
344 elem.type === 'search' || elem.type === 'number' || elem.type === 'url') {
346 } else if (elem.type !== 'button' && elem.type !== 'submit' && elem.type !== 'color' && elem.type !== 'radio' &&
347 elem.type !== 'checkbox' && elem.type !== 'file' && elem.type !== 'submit' && elem.type !== 'image') {
350 /* consider TextArea element as input element */
351 } else if (elem instanceof HTMLTextAreaElement) {
355 /** span object is the visual representation of hint */
356 var span = doc.createElement('span');
357 span.id = 'vc_tooltip';
358 span.className = 'vc_number_tag_normal vc_tooltip_' + hint.type;
360 if (hint.type == 'input') {
361 span.classList.add('vc_tip_tier_first');
362 } else if (rect.height > 30) {
363 span.classList.add('vc_tip_tier_second');
365 span.classList.add('vc_tip_tier_third');
370 /* appends hint to vc_hint_div container */
371 vc_hint_div.appendChild(hint.span);
372 vc_page_hints.push(hint);
376 * nsResolver function that is used for resolving namespace in webpage document
378 * @param prefix prefix of the namespace
380 * @return namespace url
382 function nsResolver(prefix) {
384 'xhtml' : 'http://www.w3.org/1999/xhtml' //add other namespaces if needed
386 return namespace[prefix] || null;
390 * vc_generate_hints function creates hints for all clickable elements on web page
392 * @param win window element (can be different if we analyse iframe)
394 function vc_generate_hints(win) {
395 vc_print_log('== start generate hints' + win);
398 /* if win is not passed assume the global window variable */
408 vc_print_log('== some error occured in win.document' + e);
412 /* Custom pre process to generate hints */
413 vc_custom_pre_generate_hints();
415 var start = vc_page_hints.length;
417 vc_hint_div = doc.createElement('div');
418 vc_hint_div.id = 'vc_tooltip_area';
419 vc_hint_div.style.display = 'block';
421 vc_text_indicators = [];
423 /* return result as snapshot not iterator, due to problems with undefined objects */
424 vc_print_log('== start doc.evaluate');
425 var snapshot = doc.evaluate(vc_xpath_query, doc.body, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
426 vc_print_log('== snapshot length : ' + snapshot.snapshotLength);
428 /* set current screen dimensions */
432 bottom : win.innerHeight,
433 right : win.innerWidth
436 /* analyse if a hint is needed for each returned object */
437 for (var i = 0; i < snapshot.snapshotLength; i++) {
438 var element = snapshot.snapshotItem(i),
439 rect = element.getBoundingClientRect();
441 vc_made_hint = false;
442 /* executed very rarely */
443 if (rect.width === 0 || rect.height === 0) {
444 for (var j = 0; j < element.childNodes.length; j++) {
445 if (element.childNodes[j].nodeType !== 1) {
449 var style = doc.defaultView.getComputedStyle(element.childNodes[j], null);
450 if (style && style.getPropertyValue('float') !== 'none' && vc_is_visible(element.childNodes[j], vc_scr)) {
451 vc_make_hint(element, element.childNodes[j]);
462 if (vc_is_visible(element, vc_scr) && !element.attributes.disabled) {
463 if ((element instanceof HTMLButtonElement || element.classList.contains('ui-btn') || element.getAttribute('role') == 'button')) {
464 if (element.textContent.trim().replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').length > 0) {
465 vc_text_indicators[vc_text_indicators.length] = element;
467 vc_make_hint(element, element);
470 } else if (element.getAttribute('role') == 'radio' || element.getAttribute('role') == 'checkbox' || element.getAttribute('role') == 'combobox') {
471 vc_make_hint(element, element);
473 /* if the element is an anchor without text, make hint */
474 } else if (element instanceof HTMLAnchorElement) {
475 if (element.textContent.trim().length > 0) {
476 vc_text_indicators[vc_text_indicators.length] = element;
479 var cStyle = window.getComputedStyle(element, false);
480 if (cStyle.backgroundImage !== 'none' && cStyle.backgroundImage !== 'inherit') {
481 vc_make_hint(element, element);
483 } else if (element.textContent.trim().length == 0) {
484 /* if element has child with background or image element*/
486 var childs = element.querySelectorAll('*');
487 for (var sp = 0; sp < childs.length; ++sp) {
488 if (vc_is_visible(childs[sp], vc_scr)) {
489 var cstyle = win.getComputedStyle(childs[sp], false);
490 if (childs[sp].nodeName == 'IMG' || cstyle.overflow == 'hidden'
491 || (cstyle.backgroundImage !== 'none' && cstyle.backgroundImage !== 'inherit')) {
492 vc_make_hint(element, childs[sp]);
501 vc_make_hint(element, element);
506 if (isNaN(element.textContent.trim())) {
507 vc_find_img_element(element, null, element);
510 } else if (element instanceof HTMLAreaElement || element.nodeName == 'INPUT') {
511 vc_make_hint(element, element);
513 vc_find_img_element(element, null, element);
514 if (vc_made_hint == false) {
515 vc_make_hint(element, element);
521 /* append whole div with hints to body */
523 doc.body.insertBefore(vc_hint_div, doc.body.firstChild);
525 //append all text indicators to body
529 end : vc_page_hints.length - 1
533 /* analyse iframes of the website */
534 if (win === window) {
535 var frames = document.querySelectorAll('iframe');
536 for (var f = 0; f < frames.length; f++) {
539 elem = frames[f].contentWindow.document;
541 vc_bad_iframes.push(frames[f]);
547 bottom : win.innerHeight,
548 right : win.innerWidth
550 if (elem == undefined) {
551 vc_bad_iframes.push(frames[f]);
554 if (elem != undefined && !vc_is_visible(frames[f], vc_scr)) {
557 // vc_generate_hints(frames[f].contentWindow);
561 vc_flag_hint_exist = true;
562 vc_print_log('== end');
566 * vc_find_img_element function find element containing image. if found then call a function to make a tooltip.
568 * @param elem element to search for text
569 * @param parent parent element of the @elem
570 * @param snapshotElem the first @elem when vc_generate_hints calls this function
572 function vc_find_img_element(elem, parent, snapshotElem) {
574 if (elem.nodeType == 1) {
575 var cStyle = document.defaultView.getComputedStyle(elem, false);
576 if ((vc_is_visible(elem, vc_scr) && cStyle && cStyle.backgroundImage != 'none' && cStyle.backgroundImage != 'inherit' || elem.nodeName == 'IMG')) {
577 vc_make_hint(snapshotElem, elem);
583 var children = elem.childNodes;
584 for (var ch = 0; !vc_made_hint && ch < children.length; ch++) {
585 vc_find_img_element(children[ch], elem, snapshotElem);
592 * vc_show_hints function displays hints numbering from 1
594 function vc_show_hints() {
595 /* Custom pre process to show hints */
596 vc_custom_pre_show_hints();
598 if (vc_flag_log == true) {
599 if (vc_asr_result == undefined) {
600 vc_asr_result = document.createElement('div');
601 vc_asr_result.id = 'vc_asr_result';
602 vc_asr_result.innerHTML = 'ASR Result';
603 document.body.appendChild(vc_asr_result);
606 if (vc_rec_result == undefined) {
607 vc_rec_result = document.createElement('div');
608 vc_rec_result.id = 'vc_rec_result';
609 document.body.appendChild(vc_rec_result);
614 for (var d = 0; d < vc_docs.length; d++) {
615 var doc = vc_docs[d];
616 for (var i = doc.start; i <= doc.end; i++) {
617 var win = doc.doc.defaultView,
618 hint = vc_page_hints[i],
619 rect = hint.child.getClientRects()[0];
624 bottom : win.innerHeight,
625 right : win.innerWidth
628 hint.span.style.display = 'block';
637 position_factor_left;
639 if (vc_flag_conflict) {
640 position_factor_top = 18;
641 position_factor_left = 18;
642 hint.span.className = 'vc_text_indicator_conflict_normal vc_tooltip_normal vc_tip_tier_second';
644 position_factor_top = 15;
645 position_factor_left = 15;
648 if (rect.top < position_factor_top) {
649 topPos = win.pageYOffset;
651 topPos = rect.top + win.pageYOffset - position_factor_top;
654 if (rect.left < position_factor_left) {
655 leftPos = win.pageXOffset;
657 leftPos = rect.left + win.pageXOffset - position_factor_left;
660 if (hint.type == 'area') {
661 coord = hint.elem.coords.split(',');
662 leftPos = parseFloat(leftPos) + parseFloat(coord[0]);
663 topPos = parseFloat(topPos) + parseFloat(coord[1]);
665 hint.span.style.top = topPos + 'px';
666 hint.span.style.left = leftPos + 'px';
667 hint.span.textContent = vc_hint_num;
669 hint.span.style.display = 'block';
671 if (-1 == vc_visible_hints.indexOf(hint)) {
672 vc_visible_hints.push(hint);
679 // for (var bf = 0; bf < vc_bad_iframes.length; ++bf) {
680 // vc_result_highlight(vc_bad_iframes[bf]);
685 * vc_remove_hints function removes all hints in the web page. It deletes both visible and hidden hints
687 function vc_remove_hints() {
688 /* Custom pre process to remove hints */
689 vc_custom_pre_remove_hints();
691 vc_remove_highlight();
693 for (var d = 0; d < vc_docs.length; d++) {
694 var doc = vc_docs[d].doc;
695 var tooltipArea = doc.querySelector('#vc_tooltip_area');
696 if (null !== tooltipArea && tooltipArea.parentNode) {
697 tooltipArea.parentNode.removeChild(tooltipArea);
700 vc_visible_hints = [];
704 /** if the rotation occured refresh the window information */
708 bottom : window.innerHeight,
709 right : window.innerWidth
713 vc_flag_hint_exist = false;
714 vc_flag_conflict = false;
718 * vc_hide_hints function hides the hints from the screen. Hints elements still exist
720 function vc_hide_hints(focusedExist) {
721 /* Custom pre process to hide hints */
722 if (true != focusedExist) {
723 vc_custom_pre_hide_hints();
726 for (var i = 0; i < vc_visible_hints.length; i++) {
727 var hint = vc_visible_hints[i];
728 if (hint.span.classList.contains('vc_tooltip_focus') == false) {
729 hint.span.style.display = 'none';
730 hint.span.style.top = 0;
731 hint.span.style.left = 0;
737 * vc_remove_popup function removes popup.
738 * This function is safe to call if the popup doesnt exist.
740 function vc_remove_popup() {
741 var node = document.querySelector('#vc_popup');
742 if (null !== node && node.parentNode) {
743 node.parentNode.removeChild(node);
748 * vc_show_popup function displays a floating div over the web page with text in it for timout milliseconds
750 * @param text text to be shown on the popup
751 * @param timeout time for which the popup should be shown in milliseconds. If timeout is 0 popup must be deleted manually
753 function vc_show_popup(text, timeout) {
754 /* remove any existing popup */
757 /* overlay creates a light grey area over the whole website */
758 var overlay = document.createElement('div');
759 overlay.id = 'vc_popup';
761 /* vcpopup is the main popup window, that is centered over the overlay */
762 var vcpopup = document.createElement('div');
763 vcpopup.id = 'vc_popup_content';
765 /* content is used to display text and center it vertically inside vcpopup */
766 var content = document.createElement('div');
767 content.style.display = 'table-cell';
768 content.style.fontFamily = '"Open Sans", "Helvetica", sans-serif';
769 content.style.verticalAlign = 'middle';
770 content.innerHTML = text;
772 vcpopup.appendChild(content);
773 overlay.appendChild(vcpopup);
774 document.body.appendChild(overlay);
776 /* if timeout is set to 0, display popup until not deleted manually */
778 setTimeout(function () {
787 * vc_remove_highlight removes highlight from conflicting links
789 * @param selectedNum the number of target tooltip
791 function vc_remove_highlight(selectedNum) {
793 var high = document.querySelectorAll('#vc_highlight');
794 for (var cn = 0; cn < high.length; cn++) {
795 if ((cn + 1) != selectedNum) {
796 high[cn].parentNode.removeChild(high[cn]);
800 var highlightArea = document.querySelector('#vc_highlight_area');
802 highlightArea.parentNode.removeChild(highlightArea);
808 * vc_result_highlight creates highlight box over elem
810 * @param elem element to be highlighted
812 function vc_result_highlight(elem) {
813 var doc = elem.ownerDocument,
816 if (vc_is_visible(elem) == false && elem.querySelector('img')) {
817 rect = elem.querySelector('img').getClientRects()[0];
819 rect = elem.getClientRects()[0];
826 vc_high_div = document.querySelector('#vc_highlight_area');
829 vc_high_div = doc.createElement('div');
830 vc_high_div.id = 'vc_highlight_area';
831 doc.body.appendChild(vc_high_div);
834 var high_span = doc.createElement('div');
835 high_span.id = 'vc_highlight';
837 /* top left position of the element */
838 var leftPos = (rect.left > vc_scr.left ? rect.left : vc_scr.left);
839 var topPos = (rect.top > vc_scr.top ? rect.top : vc_scr.top);
841 /* adjust top left position with page scroll */
842 topPos += window.pageYOffset - 3;
843 leftPos += window.pageXOffset - 3;
845 high_span.style.left = leftPos + 'px';
846 high_span.style.top = topPos + 'px';
847 high_span.style.width = rect.width + 'px';
848 high_span.style.height = (rect.height + 6) + 'px';
850 /* if element's dimensions are 0x0 create highlight for it's children */
851 if (rect.width === 0 || rect.height === 0) {
852 var height = topPos + rect.height;
853 var width = leftPos + rect.width;
854 for (var i = 0; i < elem.children.length; i++) {
855 var child = elem.children[i];
856 var chRect = child.getClientRects()[0];
860 if (chRect.top + chRect.height > height) {
861 height = chRect.top + chRect.height;
863 if (chRect.left + chRect.width > width) {
864 width = chRect.left + chRect.width;
867 high_span.style.height = height - topPos + 'px';
868 high_span.style.width = width - leftPos + 'px';
871 vc_high_div.appendChild(high_span);
875 * vc_generate_conflict_hints function creates hints for all conflicting highlighted hints
877 function vc_generate_conflict_hints() {
878 vc_hint_div = document.querySelector('#vc_tooltip_area');
881 vc_hint_div = document.createElement('div');
882 vc_hint_div.id = 'vc_tooltip_area';
883 document.body.insertBefore(vc_hint_div, document.body.firstChild);
889 bottom : window.innerHeight,
890 right : window.innerWidth
894 var conflicts = vc_conflict_links;
896 for (var i = 0; i < conflicts.length; i++) {
897 var elem = conflicts[i];
900 vc_result_highlight(elem);
901 if (vc_is_visible(elem, vc_scr) == false && elem.querySelector('img') != null) {
902 rect = elem.querySelector('img').getClientRects()[0];
904 rect = elem.getClientRects()[0];
913 position_factor_top = 20, //[TODO]move at proper place
914 position_factor_left = 28,
916 span = document.createElement('span');
917 span.id = 'vc_tooltip';
919 if (rect.top < position_factor_top) {
920 topPos = window.pageYOffset + 'px';
922 topPos = rect.top + window.pageYOffset - position_factor_top + 'px';
925 if (rect.left < position_factor_left) {
926 leftPos = window.pageXOffset + 'px';
928 leftPos = rect.left + window.pageXOffset - position_factor_left + 'px';
931 /* tooltip style, !please keep styles sorted */
933 hint.span.style.top = topPos;
934 hint.span.style.left = leftPos;
935 vc_hint_div.appendChild(hint.span);
938 hint.child = vc_high_div.childNodes[i];
939 vc_page_hints[vc_page_hints.length] = hint;
945 end : vc_page_hints.length - 1
948 vc_flag_hint_exist = true;
949 vc_flag_conflict = true;
950 vc_conflict_timer = setTimeout(vc_remove_hints, 5000);
956 * vc_trigger_event function makes custom DOM event
958 * @param node target element taking event
959 * @param eventType type of the custom event
961 function vc_trigger_event(node, eventType) {
963 var clickEvent = new Event(eventType);
964 node.dispatchEvent(clickEvent);
966 console.log('error ouccured', e);
971 * vc_do_action function creates event for selected element.
973 * @param targetElem selected element
974 * @param numberTag number of selected tooltip
975 * @param value(option) text entered into selected input element
977 * @return array including coordinates of the @targetElem and the number of targetElem.
979 function vc_do_action(targetElem, numberTag, value) {
980 vc_print_log('=== Inside vc_do_action');
981 vc_print_log(targetElem.elem.outerHTML);
983 var elem = targetElem.elem,
984 type = targetElem.type,
992 /* Create click effect element */
993 effect = document.createElement('div');
994 effect.id = 'vc_click_effect';
996 if (numberTag != undefined) {
997 position = numberTag.getBoundingClientRect();
998 effect.style.left = (position.left + (position.width / 2) - 35) + 'px'
999 effect.style.top = (position.top + (position.height / 2) - 35) + 'px'
1001 numberTag.classList.add('vc_tooltip_focus');
1002 numberTag.style.display = 'block';
1005 if (vc_flag_conflict) {
1006 vc_remove_highlight(numberTag.textContent.trim());
1008 vc_hide_hints(true);
1010 position = elem.getClientRects()[0];
1011 effect.style.left = (position.left - 35) + 'px'
1012 effect.style.top = (position.top - 35) + 'px'
1015 vc_result_highlight(elem);
1016 elem.classList.add('vc_text_focus');
1018 vc_text_indicators[vc_text_indicators.length] = elem;
1021 document.body.insertBefore(effect, document.body.firstChild);
1023 /* Occurring events */
1025 if (type == 'input' && value != undefined) {
1026 setTimeout(function () {
1028 vc_trigger_event(elem, 'keyup');
1029 vc_trigger_event(elem, 'input');
1036 document.body.removeChild(effect);
1043 setTimeout(function () {
1044 if (vc_asr_result != undefined) {
1045 vc_asr_result.parentElement.removeChild(vc_asr_result);
1046 vc_asr_result = undefined;
1048 if (vc_rec_result != undefined) {
1049 vc_rec_result.parentElement.removeChild(vc_rec_result);
1050 vc_rec_result = undefined;
1053 if (numberTag == undefined) {
1054 elem.classList.remove('vc_text_focus');
1057 document.body.removeChild(effect);
1063 vc_print_log('=== Click Finish');
1066 var rect = elem.getClientRects()[0],
1067 x = rect.left + (rect.width / 2),
1068 y = rect.top + (rect.height / 2);
1070 vc_print_log('[ERROR] Target element is not valid')
1080 return [1, x / window.innerWidth, y / window.innerHeight];
1088 * vc_click function triggers click on element connected to cmd and param. Assumption is made that tooltips are numbers!
1090 * @param cmd tooltip tag number from voice-control
1091 * @param param text parameter from voice-control
1093 function vc_click(cmd, param) {
1094 vc_print_log('== start click cmd = ' + cmd + ', param = ' + param + ', visible tags = ' + vc_visible_hints.length);
1096 if (vc_flag_log == true) {
1097 if (vc_asr_result != undefined) {
1098 vc_asr_result.innerHTML = param;
1101 setTimeout(function () {
1102 if (vc_asr_result != undefined) {
1103 vc_asr_result.parentElement.removeChild(vc_asr_result);
1104 vc_asr_result = undefined;
1106 if (vc_rec_result != undefined) {
1107 vc_rec_result.parentElement.removeChild(vc_rec_result);
1108 vc_rec_result = undefined;
1113 /* pre-defined rule checker */
1114 if (vc_check_web_control(param) == true) {
1120 // if cmd has a numerical value
1121 if (isNaN(cmd) == false) {
1122 vc_print_log('== numbering tag click');
1124 // when cmd is floating point value without parameter
1125 // search exactly the same text
1126 if (Number.isInteger(cmd) == false && param == '') {
1127 for (var i = 0; i < vc_text_indicators.length; ++i) {
1128 if (vc_text_indicators[i].textContent.trim().indexOf(cmd) != -1) {
1129 return vc_do_action({
1130 elem : vc_text_indicators[i]
1135 var list = vc_selector([cmd.toString()]);
1137 for (var i = 0; i < list.length; i++) {
1138 if (list[i].childElementCount == 0 && list[i].textContent.trim().indexOf(cmd) != -1) {
1139 return vc_do_action({
1146 // when cmd is valid integer value
1147 if (Number.isInteger(cmd) == true && cmd <= vc_visible_hints.length) {
1148 var hint = vc_visible_hints[cmd - 1];
1150 // insert text into input element
1151 if (hint.type === 'input' && param != '') {
1152 return vc_do_action(hint, hint.span, param);
1153 } else if (param == '') {
1154 if (!vc_flag_conflict) {
1155 vc_conflict_links = [];
1156 vc_conflict_links.push(hint.elem);
1158 for (var i = 0; i < vc_text_indicators.length; ++i) {
1159 if (vc_text_indicators[i].textContent.trim() == cmd) {
1160 vc_conflict_links.push(vc_text_indicators[i]);
1164 if (vc_conflict_links.length > 1) {
1166 vc_generate_conflict_hints();
1167 return [vc_conflict_links.length, 0, 0];
1171 return vc_do_action(hint, hint.span);
1175 // when there is no matched tooltip
1178 return vc_search_text(cmd.toString().concat(' ', param).trim());
1182 return vc_search_text(param);
1189 * vc_search_text function search the element that include same text with @param.
1191 * @param param param from voice-control.
1193 function vc_search_text(param) {
1194 /* phase 1. search full text in the webpage */
1195 /* first, compare with links in html documents */
1196 vc_print_log('=== start searching(text level)');
1198 if (vc_flag_log == true) {
1199 vc_rec_result.style.background = 'rgba(0, 200, 100, 1)';
1205 bottom : window.innerHeight,
1206 right : window.innerWidth
1209 vc_conflict_links = [];
1211 for (var i = 0; i < vc_text_indicators.length; ++i) {
1212 if (vc_is_visible(vc_text_indicators[i], vc_scr, true) &&
1213 vc_text_indicators[i].textContent.replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase().indexOf(param.replace(/ /g, '').toLowerCase()) !== -1) {
1214 vc_conflict_links.push(vc_text_indicators[i]);
1218 if (vc_conflict_links.length == 1) {
1219 /* if there is one result */
1220 return vc_do_action({
1221 elem : vc_conflict_links[0]
1223 } else if (vc_conflict_links.length > 1) {
1224 vc_generate_conflict_hints();
1226 return [vc_conflict_links.length, 0, 0];
1229 /* second, compare with whole text of elements in html documents */
1230 var resultTokenArr = param.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').split(' '),
1231 el = vc_selector(resultTokenArr),
1234 for (var i = 0; el.length > i; i++) {
1237 if (temp.childElementCount === 0) {
1238 var curStr = temp.textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
1239 if (curStr == preStr || vc_is_visible(temp) == false) {
1245 vc_conflict_links.push(temp);
1249 if (vc_conflict_links.length == 1) {
1250 return vc_do_action({
1251 elem : vc_conflict_links[0]
1253 } else if (vc_conflict_links.length > 1) {
1254 vc_generate_conflict_hints();
1256 return [vc_conflict_links.length, 0, 0];
1259 * vc_search_word depend on the voice control language.
1260 * This function increase the accuracy of the searching result using word and letter level searching.
1261 * If the language is changed, the definition of the function will be changed by reloading the .js file.
1263 temp = vc_search_word(param, true);
1264 if (temp != undefined) {
1265 return vc_do_action({
1269 //vc_show_popup('There is no such kind of string.<br>(' + param + ')', 1500);
1278 * vc_scroll_event_firing function move the scroll.
1280 * @param evt keyword to move the scroll.
1282 function vc_scroll_event_firing(evt) {
1283 if (evt == 'DOWN') {
1284 window.scrollBy(0, 500);
1285 } else if (evt == 'UP') {
1286 window.scrollBy(0, -500);
1287 } else if (evt == 'TOP') {
1288 window.scrollTo(0, 0);
1293 * vc_print_html function print out log on vc_log_area and console for debug.
1294 * If vc_flag_log is not true, then the logs will only print on console.
1296 * @param text log text to print out.
1298 function vc_print_log(text) {
1300 var time = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ':' + d.getMilliseconds();
1301 console.log(time, '[', vc_print_log.caller.name, ']', text);
1303 if (vc_flag_log == true) {
1304 vc_log_area.value += time + ' [' + vc_print_log.caller.name + '] ' + text + '\n';
1309 * vc_print_html function print out raw source of the html document for debug.
1311 function vc_print_html() {
1312 if (vc_html_area == undefined) {
1313 vc_html_area = document.createElement('textarea');
1314 vc_html_area.disabled = true;
1315 vc_html_area.id = 'vc_html_area';
1317 document.body.appendChild(vc_html_area);
1319 vc_html_area.value = location.href.toString() + '\n' + document.querySelector('html').innerHTML;
1321 document.body.removeChild(vc_html_area);
1322 vc_html_area = undefined;
1327 * vc_search_text function is called by voice-control-webview.cpp.
1328 * this function call some group of functions refered to @phase.
1330 * @param phase key value to call a group of functions.
1331 * @param data function data from voice-control-webview.cpp.
1333 function vc_request_action(phase, data) {
1334 vc_print_log('phase :' + phase + ',data :' + data);
1335 if ('SHOW_TOOLTIP' == phase) {
1336 clearTimeout(vc_conflict_timer);
1338 /* create & show tooltips to recognize the voice */
1339 if (!vc_flag_conflict && false == vc_flag_hint_exist) {
1341 vc_generate_hints();
1344 } else if ('HIDE_TOOLTIP' == phase) {
1346 if (!vc_flag_conflict) {
1349 clearTimeout(vc_conflict_timer);
1350 vc_conflict_timer = setTimeout(vc_remove_hints, 5000);
1352 } else if ('RESULT' == phase) {
1353 /* do action by the result */
1354 vc_all_elem = document.querySelectorAll('body *:not(script)');
1355 var result = vc_correct_parameter(data);
1357 return vc_click(result.cmd, result.param);
1358 } else if ('REMOVE_TOOLTIP' == phase) {
1359 clearTimeout(vc_conflict_timer);
1368 * vc_selector function is a selector function to find the elements include whole text in @strArr.
1370 * @param strArr group of strings to find elements.
1372 * ex) vc_selector(['abc', 'def']);
1374 function vc_selector(strArr) {
1376 for (var i = 0; i < vc_all_elem.length; i++) {
1377 var temp = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase();
1380 for (j = 0; j < strArr.length; j++) {
1381 if (temp.includes(strArr[j].toLowerCase()) == false) {
1386 if (strArr.length == j && vc_is_visible(vc_all_elem[i], null, true)) {
1387 result.push(vc_all_elem[i]);
1395 * vc_get_url_path function returns url path of the view.
1397 * @return part of the url(host name + path name). it is used to read page specific script.
1399 function vc_get_url_path() {
1400 var hostName = document.location.hostname;
1401 var pathName = document.location.pathname;
1403 pathName = pathName.replace(/\//gi, '.');
1405 return hostName + '/' + pathName;
1409 * vc_get_conflict_status function returns the conflict status.
1411 * @return 1 if vc_flag_conflict is true. Otherwise 0.
1413 function vc_get_conflict_status() {
1414 if (vc_flag_conflict) {
1422 * vc_init function initialize some elements and styles to use voice control.
1424 function vc_init() {
1425 /* log element initialize */
1427 vc_log_area.disabled = true;
1428 vc_log_area.id = 'vc_log_area';
1429 vc_log_area.style.visibility = 'hidden';
1431 document.body.appendChild(vc_log_area);
1434 /* add tooltip style element */
1435 var vc_style_node = document.createElement('style');
1436 var vc_style_text = document.createTextNode(
1437 '#vc_tooltip_area {\
1439 position : absolute;\
1440 pointer-events: none;\
1442 z-index : 2147483647;\
1444 #vc_html_area, #vc_log_area {\
1445 background-color : rgba(0, 0, 0, 0.5) !important;\
1446 color : white !important;\
1450 padding : 10px 10px 10px 10px;\
1454 z-index : 2147483647;\
1456 #vc_popup_content {\
1457 background-color : #87cff7;\
1459 border-radius : 20px;\
1460 box-shadow : 0 2px 6px rgba(0, 0, 0, 0.3), 0 3px 8px rgba(0, 0, 0, 0.2);\
1466 line-height : 1.5em;\
1470 position : absolute;\
1472 text-align : center;\
1475 z-index : 2147483647;\
1478 background-color : rgba(0,0,0,0.2);\
1484 z-index : 2147483647;\
1486 #vc_highlight_area {\
1488 position : absolute;\
1489 pointer-events: none;\
1491 z-index : 2147483646;\
1494 border-radius: 5px;\
1497 position : absolute;\
1498 pointer-events: none;\
1499 word-break : normal;\
1500 background-color: rgba(0, 234, 255, 0.2);\
1503 border-radius: 70px;\
1506 pointer-events: none;\
1508 z-index : 2147483647;\
1509 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%);\
1511 .vc_number_tag_normal {\
1512 border : 2px solid;\
1513 border-color : rgba(255, 255, 255, 0);\
1514 border-radius : 5px;\
1515 box-shadow : 0px 2px 2px rgba(0, 0, 0, 0.3);\
1516 position: absolute;\
1517 pointer-events: none;\
1518 text-align: center;\
1519 font-family: "Samsung0ne600";\
1521 z-index : 2147483647;\
1522 word-break : normal;\
1524 .vc_text_indicator_conflict_normal {\
1526 border-color: rgba(255, 255, 255, 0);\
1527 border-radius: 20px;\
1528 box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);\
1529 position: absolute;\
1530 pointer-events: none;\
1531 text-align: center;\
1533 font-family: "Samsung0ne600";\
1534 z-index: 2147483647;\
1536 .vc_tooltip_focus {\
1537 border-color: rgba(255, 255, 255, 1);\
1540 color: #ff0000 !important;\
1541 font-weight: bolder !important;\
1544 background : rgba(0, 0, 0, 0.8);\
1545 border-radius : 20px;\
1549 padding-top : 10px;\
1550 padding-bottom : 10px;\
1551 position : absolute;\
1552 text-align : center;\
1556 z-index : 2147483647;\
1559 border-radius : 40px;\
1561 position : absolute;\
1565 z-index : 2147483647;\
1567 .vc_tooltip_input {\
1568 background-color : rgba(60, 140, 250, 0.75);\
1570 .vc_tooltip_normal, .vc_tooltip_area {\
1571 background-color : rgba(60, 185, 165, 0.75);\
1573 .vc_tip_tier_first {\
1579 .vc_tip_tier_second {\
1585 .vc_tip_tier_third {\
1591 @keyframes vc_tooltip_show {\
1599 @keyframes vc_tooltip_click {\
1601 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);\
1604 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);\
1607 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);\
1610 @keyframes vc_click_effect {\
1612 transform: scale(0.3, 0.3);\
1616 transform: scale(0.5, 0.5);\
1620 transform: scale(1, 1);\
1624 vc_style_node.appendChild(vc_style_text);
1625 document.head.appendChild(vc_style_node);
1627 var frames = window.frames;
1629 for (var i = 0; i < frames.length; i++) {
1631 var doc = frames[i].document;
1632 var vc_frame_style_node = doc.createElement('style');
1633 var vc_frame_style_text = doc.createTextNode(vc_style_node.textContent);
1635 vc_frame_style_node.appendChild(vc_frame_style_text);
1636 doc.head.appendChild(vc_frame_style_node);
1642 /* remove indicators on scroll */
1643 window.addEventListener('scroll', function () { //[TODO] need to remove the listener