mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-03-12 00:45:14 +00:00
Update chrome.js
This commit is contained in:
parent
3c1eefa68e
commit
d1c1559fe3
159
lib/chrome.js
159
lib/chrome.js
|
@ -24,7 +24,7 @@ var ChromeObject = function() {
|
|||
"port": 1080
|
||||
};
|
||||
this.inPrivate = false;
|
||||
|
||||
|
||||
// dependencies
|
||||
this.oAutoIt = null;
|
||||
|
||||
|
@ -34,10 +34,11 @@ var ChromeObject = function() {
|
|||
this.ws = Websocket.create();
|
||||
this.isAttached = false;
|
||||
this.pageList = [];
|
||||
this.title = "";
|
||||
|
||||
this.create = function() {
|
||||
this.oAutoIt = AutoItX.create().getInterface();
|
||||
return this;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.setBinPath = function(path) {
|
||||
|
@ -85,19 +86,26 @@ var ChromeObject = function() {
|
|||
return this;
|
||||
};
|
||||
|
||||
this.setProxyPort = function(n) {
|
||||
this.proxy.port = n;
|
||||
this.setProxyPort = function(port) {
|
||||
this.proxy.port = port;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.setDebuggingPort = function(port) {
|
||||
this.debuggingPort = port;
|
||||
console.log("Debugging port setted: " + port);
|
||||
console.log("Enabled debugging port:", port);
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
this.setPageId = function(s) {
|
||||
this.pageId = s;
|
||||
if (s == null) {
|
||||
var pageList = this.getPageList();
|
||||
if (pageList instanceof Array && pageList.length > 0) {
|
||||
this.pageId = pageList[0].id;
|
||||
}
|
||||
} else {
|
||||
this.pageId = s;
|
||||
}
|
||||
};
|
||||
|
||||
this.createShoutcut = function(url) {
|
||||
|
@ -222,7 +230,7 @@ var ChromeObject = function() {
|
|||
|
||||
// set proxy configuration
|
||||
if (this.proxy != null) {
|
||||
console.log("Proxy enabled:", this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port);
|
||||
console.log("Enabled proxy server:", this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port);
|
||||
cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port + "\"");
|
||||
}
|
||||
|
||||
|
@ -251,7 +259,9 @@ var ChromeObject = function() {
|
|||
|
||||
if (this.debuggingPort > 0) {
|
||||
try {
|
||||
pageList = JSON.parse(HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"));
|
||||
var responseText = HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json");
|
||||
console.info(responseText);
|
||||
pageList = JSON.parse(responseText);
|
||||
this.pageList = pageList;
|
||||
return pageList;
|
||||
} catch (e) {
|
||||
|
@ -270,16 +280,16 @@ var ChromeObject = function() {
|
|||
});
|
||||
return (pages.length > 0 ? pages[0] : null);
|
||||
};
|
||||
|
||||
|
||||
this.getPagesByUrl = function(url) {
|
||||
return this.getPageList().filter(function(x) {
|
||||
return (x.url == url);
|
||||
return (x.url.indexOf(url) == 0);
|
||||
});
|
||||
};
|
||||
|
||||
this.getPagesByTitle = function(title) {
|
||||
return this.getPageList().filter(function(x) {
|
||||
return (x.title == title);
|
||||
return (x.title.indexOf(title) == 0);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -296,18 +306,15 @@ var ChromeObject = function() {
|
|||
pageEventId++;
|
||||
console.info("ChromeObject().sendPageRPC() -> Sent");
|
||||
} else {
|
||||
var pageList = this.getPageList();
|
||||
if (pageList instanceof Array && pageList.length > 0) {
|
||||
this.pageId = pageList[0].id;
|
||||
if (this.pageId != "") {
|
||||
result = this.sendPageRPC(method, params);
|
||||
} else {
|
||||
console.error("Got invaild list of pages");
|
||||
}
|
||||
this.setPageId(null);
|
||||
if (this.pageId != "") {
|
||||
result = this.sendPageRPC(method, params);
|
||||
} else {
|
||||
console.error("Page not found");
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("ChromeObject.sendPageRPC() -> " + e.message);
|
||||
console.log("ChromeObject.sendPageRPC() ->", e.message);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -335,8 +342,9 @@ var ChromeObject = function() {
|
|||
|
||||
this.getEvaluatedValue = function(expression) {
|
||||
try {
|
||||
var response = this.evaluate(expression);
|
||||
return JSON.parse(response).result.result.value;
|
||||
var responseText = this.evaluate(expression);
|
||||
console.info(responseText);
|
||||
return JSON.parse(responseText).result.result.value;
|
||||
} catch (e) {
|
||||
console.error("ChromeObject.getEvaluatedValue() -> " + e.message);
|
||||
}
|
||||
|
@ -357,33 +365,31 @@ var ChromeObject = function() {
|
|||
this.focus = function() {
|
||||
if (this.debuggingPort > 0) {
|
||||
try {
|
||||
// set page id
|
||||
if (this.pageId == "") {
|
||||
this.setPageId(null);
|
||||
}
|
||||
|
||||
// get current title
|
||||
var _title = this.getTitle();
|
||||
this.title = this.getTitle();
|
||||
|
||||
// implementation of focus()
|
||||
// _focus()
|
||||
this._focus();
|
||||
|
||||
// change webpage title to original
|
||||
this.setTitle(_title);
|
||||
} catch (e) {
|
||||
console.error("ChromeObject._focus() -> " + e.message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
this.blur = function() {
|
||||
this.setTitle(this.title);
|
||||
};
|
||||
|
||||
this._focus = function() {
|
||||
if (this.debuggingPort > 0) {
|
||||
try {
|
||||
// if page ID is empty
|
||||
if (this.pageId == "") {
|
||||
var pageList = this.getPageList();
|
||||
if (pageList instanceof Array && pageList.length > 0) {
|
||||
this.pageId = pageList[0].id;
|
||||
}
|
||||
}
|
||||
|
||||
// change webpage title for focusing window
|
||||
this.setTitle(this.pageId);
|
||||
this.setTitle(this.title + " " + this.pageId.substring(0, 6));
|
||||
|
||||
// find window by title
|
||||
var pageList = this.getPageList();
|
||||
|
@ -396,8 +402,8 @@ var ChromeObject = function() {
|
|||
}
|
||||
};
|
||||
|
||||
this.scrollToBottom = function() {
|
||||
return this.evaluate('window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight)');
|
||||
this.getScrollHeight = function(selector) {
|
||||
return parseInt(this.getEvaluatedValue('document.querySelector("' + selector + '").scrollHeight'));
|
||||
};
|
||||
|
||||
this.focusWithoutActivate = function() {
|
||||
|
@ -434,7 +440,7 @@ var ChromeObject = function() {
|
|||
var w = this.getRandomInt(bX * 3, sX - bX);
|
||||
var h = this.getRandomInt(bY, sY - bY);
|
||||
this.oAutoIt.WinMove(this.pageId, "", x, y, w, h);
|
||||
|
||||
|
||||
// change webpage title to original
|
||||
this.setTitle(_title);
|
||||
};
|
||||
|
@ -468,9 +474,12 @@ var ChromeObject = function() {
|
|||
};
|
||||
|
||||
this.setTitle = function(title) {
|
||||
var i = 0, repeat = 2;
|
||||
|
||||
if (this.debuggingPort > 0) {
|
||||
for (var i = 0; i < 3; i++) {
|
||||
while (i < repeat) {
|
||||
this.evaluate('document.title = "' + title + '"');
|
||||
i++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -490,14 +499,48 @@ var ChromeObject = function() {
|
|||
};
|
||||
};
|
||||
|
||||
this.getElementPosition = function(selector) {
|
||||
var result = this.getEvaluatedValue('(function() { var el = document.querySelector("' + selector + '").getBoundingClientRect(); return [el.left, el.top].join(","); })();');
|
||||
this.getElementPosition = function(selector) {
|
||||
var result = this.getEvaluatedValue('(function() { var rect = document.querySelector("' + selector + '").getBoundingClientRect(); return [parseInt(rect.left), parseInt(rect.top), parseInt(rect.x), parseInt(rect.y), parseInt(rect.right), parseInt(rect.bottom)].join(","); })();');
|
||||
var pos = result.split(',');
|
||||
return {
|
||||
"x": parseInt(pos[0]),
|
||||
"y": parseInt(pos[1])
|
||||
};
|
||||
};
|
||||
if (pos.length == 6) {
|
||||
return {
|
||||
"x": parseInt(pos[0]),
|
||||
"y": parseInt(pos[1]),
|
||||
"a": parseInt(pos[2]),
|
||||
"b": parseInt(pos[3]),
|
||||
"g": parseInt(pos[4]),
|
||||
"d": parseInt(pos[5])
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
this.getNestedElementPosition = function(selector, subSelector, searchText) {
|
||||
var 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 "";'
|
||||
+ ' }'
|
||||
+ '})()'
|
||||
;
|
||||
var result = this.getEvaluatedValue(s);
|
||||
var pos = result.split(',');
|
||||
if (pos.length == 6) {
|
||||
return {
|
||||
"x": parseInt(pos[0]),
|
||||
"y": parseInt(pos[1]),
|
||||
"a": parseInt(pos[2]),
|
||||
"b": parseInt(pos[3]),
|
||||
"g": parseInt(pos[4]),
|
||||
"d": parseInt(pos[5])
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
this.getPageHeight = function() {
|
||||
var height = 0;
|
||||
|
@ -570,10 +613,12 @@ var ChromeObject = function() {
|
|||
return this.evaluate('(function(rect, dx, dy) { window.scrollTo(rect.x + dx, rect.y + dy); })(document.querySelector("' + selector + '").getBoundingClientRect(), parseInt("' + (dx || 0) + '"), parseInt("' + (dy || 0) + '"))');
|
||||
};
|
||||
|
||||
this.scrollTo = function(x, y) {
|
||||
return this.evaluate('window.scrollTo(parseInt(' + x + '), parseInt(' + y + '))');
|
||||
};
|
||||
|
||||
this.scrollBy = function(dx, dy) {
|
||||
var dx = (typeof(dx) !== "undefined" ? dx : '0');
|
||||
var dy = (typeof(dy) !== "undefined" ? dy : '0');
|
||||
return this.evaluate('window.scrollBy(' + dx + ', ' + dy + ')');
|
||||
return this.evaluate('window.scrollBy(parseInt(' + dx + '), parseInt(' + dy + '))');
|
||||
};
|
||||
|
||||
this.reload = function() {
|
||||
|
@ -601,12 +646,10 @@ var ChromeObject = function() {
|
|||
this.oAutoIt.Send("{SPACE}");
|
||||
};
|
||||
|
||||
this.setValue = function(selector, value) {
|
||||
var s = encodeURIComponent(value);
|
||||
return this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")');
|
||||
};
|
||||
|
||||
this.
|
||||
this.setValue = function(selector, value) {
|
||||
var s = encodeURIComponent(value);
|
||||
return this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")');
|
||||
};
|
||||
|
||||
this.create();
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user