diff --git a/lib/chrome.js b/lib/chrome.js index 51e84d2..f68e3ae 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -553,35 +553,37 @@ var ChromeObject = function() { } }; - this.getNestedElementPosition = function(selector, subSelector, searchText) { + this.getNestedElementPosition = function(selector, subSelector, searchText, startIndex) { var s = ''; + var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); - if (searchText.indexOf(':p(') != 0) { + if (searchText.indexOf(':tokenize(') == 0) { + searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var el = x.querySelector("' + subSelector + '");' - + ' var keywords = decodeURIComponent("' + encodeURIComponent(searchText) + '").split(" ");' + + ' var keywords = decodeURIComponent("' + encodeURIComponent(searchText) + '").trim().split(" ");' + ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";' + ' return (text.split(" ").filter(function(w) { return keywords.indexOf(w) > -1; }).length >= keywords.length);' - + ' });' + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + ' if (elements.length > 0) {' + ' var rect = elements[0].getBoundingClientRect();' - + ' return [parseInt(rect.left), parseInt(rect.top), parseInt(rect.x), parseInt(rect.y), parseInt(rect.right), parseInt(rect.bottom)].join(",");' + + ' return [parseInt(rect.left), parseInt(rect.top), window.pageXOffset + parseInt(rect.left), window.pageYOffset + parseInt(rect.top), parseInt(rect.width), parseInt(rect.height)].join(",");' + ' } else {' + ' return "";' + ' }' + '})()' ; - } else { - var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.indexOf(')'))); + } else if (searchText.indexOf(':p(') == 0) { + var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'))); if (p > 0) { s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' return (Math.random() < ' + p + ');' - + ' });' + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + ' if (elements.length > 0) {' - + ' var rect = elements[0].querySelector("' + subSelector + '").getBoundingClientRect();' - + ' return [parseInt(rect.left), parseInt(rect.top), parseInt(rect.x), parseInt(rect.y), parseInt(rect.right), parseInt(rect.bottom)].join(",");' + + ' var rect = elements[0].getBoundingClientRect();' + + ' return [parseInt(rect.left), parseInt(rect.top), window.pageXOffset + parseInt(rect.left), window.pageYOffset + parseInt(rect.top), parseInt(rect.width), parseInt(rect.height)].join(",");' + ' } else {' + ' return "";' + ' }' @@ -590,16 +592,33 @@ var ChromeObject = function() { } else { s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '"));' + + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + ' if (elements.length > 0) {' + ' var k = Math.floor(Math.random() * elements.length);' - + ' var rect = elements[k].querySelector("' + subSelector + '").getBoundingClientRect();' - + ' return [parseInt(rect.left), parseInt(rect.top), parseInt(rect.x), parseInt(rect.y), parseInt(rect.right), parseInt(rect.bottom)].join(",");' + + ' var rect = elements[k].getBoundingClientRect();' + + ' return [parseInt(rect.left), parseInt(rect.top), window.pageXOffset + parseInt(rect.left), window.pageYOffset + parseInt(rect.top), parseInt(rect.width), parseInt(rect.height)].join(",");' + ' } else {' + ' return "";' + ' }' + '})()' ; } + } else { + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + + ' var el = x.querySelector("' + subSelector + '");' + + ' var searchText = decodeURIComponent("' + encodeURIComponent(searchText) + '").trim();' + + ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";' + + ' return (text.indexOf(searchText) > -1);' + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + + ' if (elements.length > 0) {' + + ' var rect = elements[0].getBoundingClientRect();' + + ' return [parseInt(rect.left), parseInt(rect.top), window.pageXOffset + parseInt(rect.left), window.pageYOffset + parseInt(rect.top), parseInt(rect.width), parseInt(rect.height)].join(",");' + + ' } else {' + + ' return "";' + + ' }' + + '})()' + ; } var result = this.getEvaluatedValue(s); @@ -610,8 +629,8 @@ var ChromeObject = function() { "y": parseInt(pos[1]), "a": parseInt(pos[2]), "b": parseInt(pos[3]), - "g": parseInt(pos[4]), - "d": parseInt(pos[5]) + "w": parseInt(pos[4]), + "h": parseInt(pos[5]) }; } else { return { @@ -619,12 +638,71 @@ var ChromeObject = function() { "y": -1, "a": -1, "b": -1, - "g": -1, - "d": -1 + "w": -1, + "h": -1 }; } }; + this.triggerEventOnNestedElement = function(eventName, selector, subSelector, searchText, startIndex) { + var s = ''; + var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); + + if (searchText.indexOf(':tokenize(') == 0) { + searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + + ' var el = x.querySelector("' + subSelector + '");' + + ' var keywords = decodeURIComponent("' + encodeURIComponent(searchText) + '").trim().split(" ");' + + ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";' + + ' return (text.split(" ").filter(function(w) { return keywords.indexOf(w) > -1; }).length >= keywords.length);' + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + + ' if (elements.length > 0) {' + + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + + ' }' + + '})()' + ; + } else if (searchText.indexOf(':p(') == 0) { + var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'))); + if (p > 0) { + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + + ' return (Math.random() < ' + p + ');' + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + + ' if (elements.length > 0) {' + + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + + ' }' + + '})()' + ; + } else { + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '"));' + + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + + ' if (elements.length > 0) {' + + ' var k = Math.floor(Math.random() * elements.length);' + + ' elements[k].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + + ' }' + + '})()' + ; + } + } else { + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + + ' var el = x.querySelector("' + subSelector + '");' + + ' var searchText = decodeURIComponent("' + encodeURIComponent(searchText) + '").trim();' + + ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";' + + ' return (text.indexOf(searchText) > -1);' + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + + ' if (elements.length > 0) {' + + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + + ' }' + + '})()' + ; + } + + return this.evaluate(s); + }; + this.getNestedElementIndex = function(selector, subSelector, searchText) { var s = '';