diff --git a/lib/chrome.js b/lib/chrome.js index c32f8e8..d2430b9 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -346,19 +346,19 @@ var ChromeObject = function() { return JSON.parse(responseText).result.result.value; } catch (e) { console.error("ChromeObject.getEvaluatedValue() ->", e.message); - return ""; + return ""; } }; this.exit = function() { return this.sendPageRPC("Browser.close", {}); }; - - this.close = function() { - var response = this.sendPageRPC("Page.close", {}); - this.setPageId(null); - return response; - }; + + this.close = function() { + var response = this.sendPageRPC("Page.close", {}); + this.setPageId(null); + return response; + }; this.terminate = function() { try { @@ -526,40 +526,40 @@ var ChromeObject = function() { "g": -1, "d": -1 }; - } + } }; this.getNestedElementPosition = function(selector, subSelector, searchText) { var s = ''; - - if (searchText.indexOf(':p(') != 0) { - s += '(function() {' - + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' - + ' return (x.querySelector("' + subSelector + '").innerText.indexOf(decodeURIComponent("' + encodeURIComponent(searchText) + '")) > -1);' - + ' });' - + ' 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(",");' - + ' } else {' - + ' return "";' - + ' }' - + '})()' - ; - } else { - var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.indexOf(')'))); - s += '(function() {' - + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' - + ' return (Math.random() < ' + p + ');' - + ' });' - + ' 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(",");' - + ' } else {' - + ' return "";' - + ' }' - + '})()' - ; - } + + if (searchText.indexOf(':p(') != 0) { + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + + ' return (x.querySelector("' + subSelector + '").innerText.indexOf(decodeURIComponent("' + encodeURIComponent(searchText) + '")) > -1);' + + ' });' + + ' 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(",");' + + ' } else {' + + ' return "";' + + ' }' + + '})()' + ; + } else { + var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.indexOf(')'))); + s += '(function() {' + + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + + ' return (Math.random() < ' + p + ');' + + ' });' + + ' 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(",");' + + ' } else {' + + ' return "";' + + ' }' + + '})()' + ; + } var result = this.getEvaluatedValue(s); var pos = result.split(','); @@ -581,7 +581,7 @@ var ChromeObject = function() { "g": -1, "d": -1 }; - } + } }; this.getPageHeight = function() { @@ -647,7 +647,7 @@ var ChromeObject = function() { } } else { var p = parseFloat(selector.substring(selector.indexOf('(') + 1, selector.indexOf(')'))); - var _selector = selector.substring(0, selector.indexOf(':')); + var _selector = selector.substring(0, selector.indexOf(':')); return this.evaluate('(function(obj, p) { var element = Object.values(obj).find(function() { return (Math.random() < p); }); if(element) element.click(); })(document.querySelectorAll("' + _selector + '"), ' + p + ')'); } };