Improve error handling in sendPageRPC and getEvaluatedValue

Refactored sendPageRPC to better handle error responses and log error descriptions from the response object. Updated getEvaluatedValue to safely access the evaluated value, returning an empty string if not present. Bumped VERSIONINFO to 0.5.1.
This commit is contained in:
Namhyeon Go 2025-08-11 02:58:33 +09:00
parent 665642586b
commit 1533059e44

View File

@ -422,13 +422,13 @@ var ChromeObject = function() {
}; };
this.sendPageRPC = function(method, params) { this.sendPageRPC = function(method, params) {
var result = null; var response = null;
try { try {
if (this.pageId != "") { if (this.pageId != "") {
var url = "http://localhost:3000/devtools/page/" + this.pageId + "?port=" + this.debuggingPort; var url = "http://localhost:3000/devtools/page/" + this.pageId + "?port=" + this.debuggingPort;
try { try {
result = HTTP.create() response = HTTP.create()
.setContentType("application/json") .setContentType("application/json")
.setDataType("json") .setDataType("json")
.open("POST", url) .open("POST", url)
@ -440,9 +440,18 @@ var ChromeObject = function() {
.send() .send()
.responseBody .responseBody
; ;
// response if error
if ("result" in response) {
(function(r) {
if ("subtype" in r && r.subtype == "error") {
console.warn("[WebBrowser]", r.description);
}
})(response.result.result);
}
} catch (e) { } catch (e) {
console.warn(e.message); console.warn(e.message);
result = {}; response = {};
} }
pageEventId.set(pageEventId.get() + 1); pageEventId.set(pageEventId.get() + 1);
@ -450,7 +459,7 @@ var ChromeObject = function() {
} else { } else {
this.setPageId(null); this.setPageId(null);
if (this.pageId != "") { if (this.pageId != "") {
result = this.sendPageRPC(method, params); response = this.sendPageRPC(method, params);
} else { } else {
console.error("Page not found"); console.error("Page not found");
} }
@ -459,7 +468,7 @@ var ChromeObject = function() {
console.log("ChromeObject.sendPageRPC() ->", e.message); console.log("ChromeObject.sendPageRPC() ->", e.message);
} }
return result; return response;
}; };
this.navigate = function(url) { this.navigate = function(url) {
@ -495,7 +504,9 @@ var ChromeObject = function() {
this.getEvaluatedValue = function(expression) { this.getEvaluatedValue = function(expression) {
try { try {
var response = this.evaluate(expression); var response = this.evaluate(expression);
return response.result.result.value; return (function(r) {
return ("value" in r ? r.value : "");
})(response.result.result);
} catch (e) { } catch (e) {
console.error("ChromeObject.getEvaluatedValue() ->", e.message); console.error("ChromeObject.getEvaluatedValue() ->", e.message);
return ""; return "";
@ -1493,7 +1504,7 @@ exports.startDebugInPrivate = function(url, proxy, profileName, debuggingPort, i
exports.publisherName = publisherName; exports.publisherName = publisherName;
exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.5"; exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.5.1";
exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;