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() {
721 /* Custom pre process to hide hints */
722 vc_custom_pre_hide_hints();
724 for (var i = 0; i < vc_visible_hints.length; i++) {
725 var hint = vc_visible_hints[i];
726 if (hint.span.classList.contains('vc_tooltip_focus') == false) {
727 hint.span.style.display = 'none';
728 hint.span.style.top = 0;
729 hint.span.style.left = 0;
735 * vc_remove_popup function removes popup.
736 * This function is safe to call if the popup doesnt exist.
738 function vc_remove_popup() {
739 var node = document.querySelector('#vc_popup');
740 if (null !== node && node.parentNode) {
741 node.parentNode.removeChild(node);
746 * vc_show_popup function displays a floating div over the web page with text in it for timout milliseconds
748 * @param text text to be shown on the popup
749 * @param timeout time for which the popup should be shown in milliseconds. If timeout is 0 popup must be deleted manually
751 function vc_show_popup(text, timeout) {
752 /* remove any existing popup */
755 /* overlay creates a light grey area over the whole website */
756 var overlay = document.createElement('div');
757 overlay.id = 'vc_popup';
759 /* vcpopup is the main popup window, that is centered over the overlay */
760 var vcpopup = document.createElement('div');
761 vcpopup.id = 'vc_popup_content';
763 /* content is used to display text and center it vertically inside vcpopup */
764 var content = document.createElement('div');
765 content.style.display = 'table-cell';
766 content.style.fontFamily = '"Open Sans", "Helvetica", sans-serif';
767 content.style.verticalAlign = 'middle';
768 content.innerHTML = text;
770 vcpopup.appendChild(content);
771 overlay.appendChild(vcpopup);
772 document.body.appendChild(overlay);
774 /* if timeout is set to 0, display popup until not deleted manually */
776 setTimeout(function () {
785 * vc_remove_highlight removes highlight from conflicting links
787 * @param selectedNum the number of target tooltip
789 function vc_remove_highlight(selectedNum) {
791 var high = document.querySelectorAll('#vc_highlight');
792 for (var cn = 0; cn < high.length; cn++) {
793 if ((cn + 1) != selectedNum) {
794 high[cn].parentNode.removeChild(high[cn]);
798 var highlightArea = document.querySelector('#vc_highlight_area');
800 highlightArea.parentNode.removeChild(highlightArea);
806 * vc_result_highlight creates highlight box over elem
808 * @param elem element to be highlighted
810 function vc_result_highlight(elem) {
811 var doc = elem.ownerDocument,
814 if (vc_is_visible(elem) == false && elem.querySelector('img')) {
815 rect = elem.querySelector('img').getClientRects()[0];
817 rect = elem.getClientRects()[0];
824 vc_high_div = document.querySelector('#vc_highlight_area');
827 vc_high_div = doc.createElement('div');
828 vc_high_div.id = 'vc_highlight_area';
829 doc.body.appendChild(vc_high_div);
832 var high_span = doc.createElement('div');
833 high_span.id = 'vc_highlight';
835 /* top left position of the element */
836 var leftPos = (rect.left > vc_scr.left ? rect.left : vc_scr.left);
837 var topPos = (rect.top > vc_scr.top ? rect.top : vc_scr.top);
839 /* adjust top left position with page scroll */
840 topPos += window.pageYOffset - 3;
841 leftPos += window.pageXOffset - 3;
843 high_span.style.left = leftPos + 'px';
844 high_span.style.top = topPos + 'px';
845 high_span.style.width = rect.width + 'px';
846 high_span.style.height = (rect.height + 6) + 'px';
848 /* if element's dimensions are 0x0 create highlight for it's children */
849 if (rect.width === 0 || rect.height === 0) {
850 var height = topPos + rect.height;
851 var width = leftPos + rect.width;
852 for (var i = 0; i < elem.children.length; i++) {
853 var child = elem.children[i];
854 var chRect = child.getClientRects()[0];
858 if (chRect.top + chRect.height > height) {
859 height = chRect.top + chRect.height;
861 if (chRect.left + chRect.width > width) {
862 width = chRect.left + chRect.width;
865 high_span.style.height = height - topPos + 'px';
866 high_span.style.width = width - leftPos + 'px';
869 vc_high_div.appendChild(high_span);
873 * vc_generate_conflict_hints function creates hints for all conflicting highlighted hints
875 function vc_generate_conflict_hints() {
876 vc_hint_div = document.querySelector('#vc_tooltip_area');
879 vc_hint_div = document.createElement('div');
880 vc_hint_div.id = 'vc_tooltip_area';
881 document.body.insertBefore(vc_hint_div, document.body.firstChild);
887 bottom : window.innerHeight,
888 right : window.innerWidth
892 var conflicts = vc_conflict_links;
894 for (var i = 0; i < conflicts.length; i++) {
895 var elem = conflicts[i];
898 vc_result_highlight(elem);
899 if (vc_is_visible(elem, vc_scr) == false && elem.querySelector('img') != null) {
900 rect = elem.querySelector('img').getClientRects()[0];
902 rect = elem.getClientRects()[0];
911 position_factor_top = 20, //[TODO]move at proper place
912 position_factor_left = 28,
914 span = document.createElement('span');
915 span.id = 'vc_tooltip';
917 if (rect.top < position_factor_top) {
918 topPos = window.pageYOffset + 'px';
920 topPos = rect.top + window.pageYOffset - position_factor_top + 'px';
923 if (rect.left < position_factor_left) {
924 leftPos = window.pageXOffset + 'px';
926 leftPos = rect.left + window.pageXOffset - position_factor_left + 'px';
929 /* tooltip style, !please keep styles sorted */
931 hint.span.style.top = topPos;
932 hint.span.style.left = leftPos;
933 vc_hint_div.appendChild(hint.span);
936 hint.child = vc_high_div.childNodes[i];
937 vc_page_hints[vc_page_hints.length] = hint;
943 end : vc_page_hints.length - 1
946 vc_flag_hint_exist = true;
947 vc_flag_conflict = true;
948 vc_conflict_timer = setTimeout(vc_remove_hints, 5000);
954 * vc_trigger_event function makes custom DOM event
956 * @param node target element taking event
957 * @param eventType type of the custom event
959 function vc_trigger_event(node, eventType) {
961 var clickEvent = new Event(eventType);
962 node.dispatchEvent(clickEvent);
964 console.log('error ouccured', e);
969 * vc_do_action function creates event for selected element.
971 * @param targetElem selected element
972 * @param numberTag number of selected tooltip
973 * @param value(option) text entered into selected input element
975 * @return array including coordinates of the @targetElem and the number of targetElem.
977 function vc_do_action(targetElem, numberTag, value) {
978 vc_print_log('=== Inside vc_do_action');
979 vc_print_log(targetElem.elem.outerHTML);
981 var elem = targetElem.elem,
982 type = targetElem.type,
990 /* Create click effect element */
991 effect = document.createElement('div');
992 effect.id = 'vc_click_effect';
994 if (numberTag != undefined) {
995 position = numberTag.getBoundingClientRect();
996 effect.style.left = (position.left + (position.width / 2) - 35) + 'px'
997 effect.style.top = (position.top + (position.height / 2) - 35) + 'px'
999 numberTag.classList.add('vc_tooltip_focus');
1000 numberTag.style.display = 'block';
1003 if (vc_flag_conflict) {
1004 vc_remove_highlight(numberTag.textContent.trim());
1008 position = elem.getClientRects()[0];
1009 effect.style.left = (position.left - 35) + 'px'
1010 effect.style.top = (position.top - 35) + 'px'
1013 vc_result_highlight(elem);
1014 elem.classList.add('vc_text_focus');
1016 vc_text_indicators[vc_text_indicators.length] = elem;
1019 document.body.insertBefore(effect, document.body.firstChild);
1021 /* Occurring events */
1023 if (type == 'input' && value != undefined) {
1024 setTimeout(function () {
1026 vc_trigger_event(elem, 'keyup');
1027 vc_trigger_event(elem, 'input');
1034 document.body.removeChild(effect);
1041 setTimeout(function () {
1042 if (vc_asr_result != undefined) {
1043 vc_asr_result.parentElement.removeChild(vc_asr_result);
1044 vc_asr_result = undefined;
1046 if (vc_rec_result != undefined) {
1047 vc_rec_result.parentElement.removeChild(vc_rec_result);
1048 vc_rec_result = undefined;
1051 if (numberTag == undefined) {
1052 elem.classList.remove('vc_text_focus');
1055 document.body.removeChild(effect);
1061 vc_print_log('=== Click Finish');
1064 var rect = elem.getClientRects()[0],
1065 x = rect.left + (rect.width / 2),
1066 y = rect.top + (rect.height / 2);
1068 vc_print_log('[ERROR] Target element is not valid')
1078 return [1, x / window.innerWidth, y / window.innerHeight];
1086 * vc_click function triggers click on element connected to cmd and param. Assumption is made that tooltips are numbers!
1088 * @param cmd tooltip tag number from voice-control
1089 * @param param text parameter from voice-control
1091 function vc_click(cmd, param) {
1092 vc_print_log('== start click cmd = ' + cmd + ', param = ' + param + ', visible tags = ' + vc_visible_hints.length);
1094 if (vc_flag_log == true) {
1095 if (vc_asr_result != undefined) {
1096 vc_asr_result.innerHTML = param;
1099 setTimeout(function () {
1100 if (vc_asr_result != undefined) {
1101 vc_asr_result.parentElement.removeChild(vc_asr_result);
1102 vc_asr_result = undefined;
1104 if (vc_rec_result != undefined) {
1105 vc_rec_result.parentElement.removeChild(vc_rec_result);
1106 vc_rec_result = undefined;
1111 /* pre-defined rule checker */
1112 if (vc_check_web_control(param) == true) {
1118 // if cmd has a numerical value
1119 if (isNaN(cmd) == false) {
1120 vc_print_log('== numbering tag click');
1122 // when cmd is floating point value without parameter
1123 // search exactly the same text
1124 if (Number.isInteger(cmd) == false && param == '') {
1125 for (var i = 0; i < vc_text_indicators.length; ++i) {
1126 if (vc_text_indicators[i].textContent.trim().indexOf(cmd) != -1) {
1127 return vc_do_action({
1128 elem : vc_text_indicators[i]
1133 var list = vc_selector([cmd.toString()]);
1135 for (var i = 0; i < list.length; i++) {
1136 if (list[i].childElementCount == 0 && list[i].textContent.trim().indexOf(cmd) != -1) {
1137 return vc_do_action({
1144 // when cmd is valid integer value
1145 if (Number.isInteger(cmd) == true && cmd <= vc_visible_hints.length) {
1146 var hint = vc_visible_hints[cmd - 1];
1148 // insert text into input element
1149 if (hint.type === 'input' && param != '') {
1150 return vc_do_action(hint, hint.span, param);
1151 } else if (param == '') {
1152 if (!vc_flag_conflict) {
1153 vc_conflict_links = [];
1154 vc_conflict_links.push(hint.elem);
1156 for (var i = 0; i < vc_text_indicators.length; ++i) {
1157 if (vc_text_indicators[i].textContent.trim() == cmd) {
1158 vc_conflict_links.push(vc_text_indicators[i]);
1162 if (vc_conflict_links.length > 1) {
1164 vc_generate_conflict_hints();
1165 return [vc_conflict_links.length, 0, 0];
1169 return vc_do_action(hint, hint.span);
1173 // when there is no matched tooltip
1176 return vc_search_text(cmd.toString().concat(' ', param).trim());
1180 return vc_search_text(param);
1187 * vc_search_text function search the element that include same text with @param.
1189 * @param param param from voice-control.
1191 function vc_search_text(param) {
1192 /* phase 1. search full text in the webpage */
1193 /* first, compare with links in html documents */
1194 vc_print_log('=== start searching(text level)');
1196 if (vc_flag_log == true) {
1197 vc_rec_result.style.background = 'rgba(0, 200, 100, 1)';
1203 bottom : window.innerHeight,
1204 right : window.innerWidth
1207 vc_conflict_links = [];
1209 for (var i = 0; i < vc_text_indicators.length; ++i) {
1210 if (vc_is_visible(vc_text_indicators[i], vc_scr, true) &&
1211 vc_text_indicators[i].textContent.replace(/[ `~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase().indexOf(param.replace(/ /g, '').toLowerCase()) !== -1) {
1212 vc_conflict_links.push(vc_text_indicators[i]);
1216 if (vc_conflict_links.length == 1) {
1217 /* if there is one result */
1218 return vc_do_action({
1219 elem : vc_conflict_links[0]
1221 } else if (vc_conflict_links.length > 1) {
1222 vc_generate_conflict_hints();
1224 return [vc_conflict_links.length, 0, 0];
1227 /* second, compare with whole text of elements in html documents */
1228 var resultTokenArr = param.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').split(' '),
1229 el = vc_selector(resultTokenArr),
1232 for (var i = 0; el.length > i; i++) {
1235 if (temp.childElementCount === 0) {
1236 var curStr = temp.textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
1237 if (curStr == preStr || vc_is_visible(temp) == false) {
1243 vc_conflict_links.push(temp);
1247 if (vc_conflict_links.length == 1) {
1248 return vc_do_action({
1249 elem : vc_conflict_links[0]
1251 } else if (vc_conflict_links.length > 1) {
1252 vc_generate_conflict_hints();
1254 return [vc_conflict_links.length, 0, 0];
1257 * vc_search_word depend on the voice control language.
1258 * This function increase the accuracy of the searching result using word and letter level searching.
1259 * If the language is changed, the definition of the function will be changed by reloading the .js file.
1261 temp = vc_search_word(param, true);
1262 if (temp != undefined) {
1263 return vc_do_action({
1267 //vc_show_popup('There is no such kind of string.<br>(' + param + ')', 1500);
1276 * vc_scroll_event_firing function move the scroll.
1278 * @param evt keyword to move the scroll.
1280 function vc_scroll_event_firing(evt) {
1281 if (evt == 'DOWN') {
1282 window.scrollBy(0, 500);
1283 } else if (evt == 'UP') {
1284 window.scrollBy(0, -500);
1285 } else if (evt == 'TOP') {
1286 window.scrollTo(0, 0);
1291 * vc_print_html function print out log on vc_log_area and console for debug.
1292 * If vc_flag_log is not true, then the logs will only print on console.
1294 * @param text log text to print out.
1296 function vc_print_log(text) {
1298 var time = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ':' + d.getMilliseconds();
1299 console.log(time, '[', vc_print_log.caller.name, ']', text);
1301 if (vc_flag_log == true) {
1302 vc_log_area.value += time + ' [' + vc_print_log.caller.name + '] ' + text + '\n';
1307 * vc_print_html function print out raw source of the html document for debug.
1309 function vc_print_html() {
1310 if (vc_html_area == undefined) {
1311 vc_html_area = document.createElement('textarea');
1312 vc_html_area.disabled = true;
1313 vc_html_area.id = 'vc_html_area';
1315 document.body.appendChild(vc_html_area);
1317 vc_html_area.value = location.href.toString() + '\n' + document.querySelector('html').innerHTML;
1319 document.body.removeChild(vc_html_area);
1320 vc_html_area = undefined;
1325 * vc_search_text function is called by voice-control-webview.cpp.
1326 * this function call some group of functions refered to @phase.
1328 * @param phase key value to call a group of functions.
1329 * @param data function data from voice-control-webview.cpp.
1331 function vc_request_action(phase, data) {
1332 vc_print_log('phase :' + phase + ',data :' + data);
1333 if ('SHOW_TOOLTIP' == phase) {
1334 clearTimeout(vc_conflict_timer);
1336 /* create & show tooltips to recognize the voice */
1337 if (!vc_flag_conflict && false == vc_flag_hint_exist) {
1339 vc_generate_hints();
1342 } else if ('HIDE_TOOLTIP' == phase) {
1344 if (!vc_flag_conflict) {
1347 clearTimeout(vc_conflict_timer);
1348 vc_conflict_timer = setTimeout(vc_remove_hints, 5000);
1350 } else if ('RESULT' == phase) {
1351 /* do action by the result */
1352 vc_all_elem = document.querySelectorAll('body *:not(script)');
1353 var result = vc_correct_parameter(data);
1355 return vc_click(result.cmd, result.param);
1356 } else if ('REMOVE_TOOLTIP' == phase) {
1357 clearTimeout(vc_conflict_timer);
1366 * vc_selector function is a selector function to find the elements include whole text in @strArr.
1368 * @param strArr group of strings to find elements.
1370 * ex) vc_selector(['abc', 'def']);
1372 function vc_selector(strArr) {
1374 for (var i = 0; i < vc_all_elem.length; i++) {
1375 var temp = vc_all_elem[i].textContent.replace(/[`~!‘’@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '').toLowerCase();
1378 for (j = 0; j < strArr.length; j++) {
1379 if (temp.includes(strArr[j].toLowerCase()) == false) {
1384 if (strArr.length == j && vc_is_visible(vc_all_elem[i], null, true)) {
1385 result.push(vc_all_elem[i]);
1393 * vc_get_url_path function returns url path of the view.
1395 * @return part of the url(host name + path name). it is used to read page specific script.
1397 function vc_get_url_path() {
1398 var hostName = document.location.hostname;
1399 var pathName = document.location.pathname;
1401 pathName = pathName.replace(/\//gi, '.');
1403 return hostName + '/' + pathName;
1407 * vc_get_conflict_status function returns the conflict status.
1409 * @return 1 if vc_flag_conflict is true. Otherwise 0.
1411 function vc_get_conflict_status() {
1412 if (vc_flag_conflict) {
1420 * vc_init function initialize some elements and styles to use voice control.
1422 function vc_init() {
1423 /* log element initialize */
1425 vc_log_area.disabled = true;
1426 vc_log_area.id = 'vc_log_area';
1427 vc_log_area.style.visibility = 'hidden';
1429 document.body.appendChild(vc_log_area);
1432 /* add tooltip style element */
1433 var vc_style_node = document.createElement('style');
1434 var vc_style_text = document.createTextNode(
1435 '#vc_tooltip_area {\
1437 position : absolute;\
1438 pointer-events: none;\
1440 z-index : 2147483647;\
1442 #vc_html_area, #vc_log_area {\
1443 background-color : rgba(0, 0, 0, 0.5) !important;\
1444 color : white !important;\
1448 padding : 10px 10px 10px 10px;\
1452 z-index : 2147483647;\
1454 #vc_popup_content {\
1455 background-color : #87cff7;\
1457 border-radius : 20px;\
1458 box-shadow : 0 2px 6px rgba(0, 0, 0, 0.3), 0 3px 8px rgba(0, 0, 0, 0.2);\
1464 line-height : 1.5em;\
1468 position : absolute;\
1470 text-align : center;\
1473 z-index : 2147483647;\
1476 background-color : rgba(0,0,0,0.2);\
1482 z-index : 2147483647;\
1484 #vc_highlight_area {\
1486 position : absolute;\
1487 pointer-events: none;\
1489 z-index : 2147483646;\
1492 border-radius: 5px;\
1495 position : absolute;\
1496 pointer-events: none;\
1497 word-break : normal;\
1498 background-color: rgba(0, 234, 255, 0.2);\
1501 border-radius: 70px;\
1504 pointer-events: none;\
1506 z-index : 2147483647;\
1507 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%);\
1509 .vc_number_tag_normal {\
1510 border : 2px solid;\
1511 border-color : rgba(255, 255, 255, 0);\
1512 border-radius : 5px;\
1513 box-shadow : 0px 2px 2px rgba(0, 0, 0, 0.3);\
1514 position: absolute;\
1515 pointer-events: none;\
1516 text-align: center;\
1517 font-family: "Samsung0ne600";\
1519 z-index : 2147483647;\
1520 word-break : normal;\
1522 .vc_text_indicator_conflict_normal {\
1524 border-color: rgba(255, 255, 255, 0);\
1525 border-radius: 20px;\
1526 box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);\
1527 position: absolute;\
1528 pointer-events: none;\
1529 text-align: center;\
1531 font-family: "Samsung0ne600";\
1532 z-index: 2147483647;\
1534 .vc_tooltip_focus {\
1535 border-color: rgba(255, 255, 255, 1);\
1538 color: #ff0000 !important;\
1539 font-weight: bolder !important;\
1542 background : rgba(0, 0, 0, 0.8);\
1543 border-radius : 20px;\
1547 padding-top : 10px;\
1548 padding-bottom : 10px;\
1549 position : absolute;\
1550 text-align : center;\
1554 z-index : 2147483647;\
1557 border-radius : 40px;\
1559 position : absolute;\
1563 z-index : 2147483647;\
1565 .vc_tooltip_input {\
1566 background-color : rgba(60, 140, 250, 0.75);\
1568 .vc_tooltip_normal, .vc_tooltip_area {\
1569 background-color : rgba(60, 185, 165, 0.75);\
1571 .vc_tip_tier_first {\
1577 .vc_tip_tier_second {\
1583 .vc_tip_tier_third {\
1589 @keyframes vc_tooltip_show {\
1597 @keyframes vc_tooltip_click {\
1599 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);\
1602 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);\
1605 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);\
1608 @keyframes vc_click_effect {\
1610 transform: scale(0.3, 0.3);\
1614 transform: scale(0.5, 0.5);\
1618 transform: scale(1, 1);\
1622 vc_style_node.appendChild(vc_style_text);
1623 document.head.appendChild(vc_style_node);
1625 var frames = window.frames;
1627 for (var i = 0; i < frames.length; i++) {
1629 var doc = frames[i].document;
1630 var vc_frame_style_node = doc.createElement('style');
1631 var vc_frame_style_text = doc.createTextNode(vc_style_node.textContent);
1633 vc_frame_style_node.appendChild(vc_frame_style_text);
1634 doc.head.appendChild(vc_frame_style_node);
1640 /* remove indicators on scroll */
1641 window.addEventListener('scroll', function () { //[TODO] need to remove the listener