Merge pull request #268 from gnh1201/dev
Some checks are pending
CodeQL / Analyze (javascript) (push) Waiting to run

Introduce the accessor and improve the CDP protocol interface
This commit is contained in:
Namhyeon Go 2025-06-07 04:47:19 +09:00 committed by GitHub
commit a1eeb31070
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 81 additions and 40 deletions

View File

@ -14,15 +14,15 @@ var AutoIt = require("lib/autoit");
var Toolkit = require("lib/toolkit");
var ExtraMath = require("lib/extramath");
// for remote debugging
var pageEventId = 0;
// for Chromium-based browsers
var pageEventId = new STD.Accessor(0);
var publisherName = new STD.Accessor("chrome");
var ChromeObject = function() {
STD.EventTarget.apply(this, arguments); // Set event-attachable object
this.publisherName = "chrome";
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application\\chrome.exe";
this.workingDirectory = null;
this.binPath = null;
this.profileName = "Default";
this.userDataDir = null;
@ -34,7 +34,7 @@ var ChromeObject = function() {
"protocol": "socks5",
"host": "127.0.0.1",
"port": 1080,
"ua": null
"userAgent": null
};
this.inPrivate = false;
@ -63,6 +63,8 @@ var ChromeObject = function() {
this.oAutoIt = AutoIt.create();
this.baseScreenX = 1;
this.baseScreenY = (!this.isAppMode ? 84 : 32);
this.setPublisherName(publisherName.get());
return this;
};
@ -77,8 +79,11 @@ var ChromeObject = function() {
this.setProfile = function(profileName, installedDir) {
this.profileName = (profileName == "Default" ? "Chrome" : profileName);
this.workingDirectory = this.workingDirectory.replace(":installedDir", installedDir);
this.binPath = this.binPath.replace(":installedDir", installedDir);
if (installedDir != null) {
this.installedDir = installedDir;
}
this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir);
this.binPath = this.binPath.replace(":installedDir", this.installedDir);
return this;
};
@ -102,7 +107,7 @@ var ChromeObject = function() {
if (dirname != null) {
this.userDataDir = dirname;
} else {
this.userDataDir = SYS.getEnvString("APPDATA") + "\\WelsonJS\\" + this.publisherName + "_user_profile";
this.userDataDir = SYS.getEnvString("APPDATA") + "\\WelsonJS\\" + publisherName.get() + "_user_profile";
}
return this;
};
@ -188,7 +193,7 @@ var ChromeObject = function() {
cmd.push("\"--user-data-dir=" + this.userDataDir + "\"");
cmd.push("\"" + url + "\"");
SHELL.createShoutcut(this.publisherName + " (" + this.profileName + ")", cmd.join(' '), SYS.getCurrentScriptDirectory());
SHELL.createShoutcut(publisherName.get() + " (" + this.profileName + ")", cmd.join(' '), SYS.getCurrentScriptDirectory());
};
this.setInPrivate = function(flag) {
@ -196,13 +201,13 @@ var ChromeObject = function() {
return this;
};
this.setUserAgent = function(ua) {
this.userAgent = ua;
this.setUserAgent = function(userAgent) {
this.userAgent = userAgent;
return this;
};
this.addUserAgent = function(ua) {
this.userAgents.push(ua);
this.addUserAgent = function(userAgent) {
this.userAgents.push(userAgent);
return this;
};
@ -223,8 +228,7 @@ var ChromeObject = function() {
// if the file does not exists, Check the 32bit installation folder again
if (!FILE.fileExists(this.binPath)) {
this.workingDirectory = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\:installedDir\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\:installedDir\\Application\\chrome.exe";
this.setPublisherName("chrome.x86");
this.setProfile(this.profileName, this.installedDir);
}
@ -282,8 +286,8 @@ var ChromeObject = function() {
if (this.proxy != null && this.isPreventProxy != true) {
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 + "\"");
if (this.proxy.ua != null) {
this.setUserAgent(this.proxy.ua);
if (this.proxy.userAgent != null) {
this.setUserAgent(this.proxy.userAgent);
}
}
@ -400,11 +404,11 @@ var ChromeObject = function() {
try {
if (this.pageId != "") {
result = this.ws.send("ws://127.0.0.1:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({
"id": pageEventId,
"id": pageEventId.get(),
"method": method,
"params": params
}));
pageEventId++;
pageEventId.set(pageEventId.get() + 1);
console.log("ChromeObject().sendPageRPC() -> Sent");
} else {
this.setPageId(null);
@ -1246,12 +1250,14 @@ var ChromeObject = function() {
this.setVendor = function(vendor) {
this.setPublisherName(vendor);
console.warn("Deprecated: Please use setPublisherName");
return this;
};
this.setPublisherName = function(publisherName) {
publisherName = publisherName.toLowerCase();
this.setPublisherName = function(_publisherName) {
publisherName.set(_publisherName.toLowerCase());
switch (publisherName) {
switch (publisherName.get()) {
case "msedge":
this.workingDirectory = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Microsoft\\Edge\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Microsoft\\Edge\\Application\\msedge.exe";
@ -1261,6 +1267,11 @@ var ChromeObject = function() {
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application\\chrome.exe";
break;
case "chrome.x86":
this.workingDirectory = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\:installedDir\\Application\\chrome.exe";
break;
case "chromium":
this.workingDirectory = SYS.getEnvString("LOCALAPPDATA") + "\\Chromium\\Application";
@ -1275,6 +1286,7 @@ var ChromeObject = function() {
case "whale":
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Naver\\Naver Whale\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Naver\\Naver Whale\\Application\\whale.exe";
this.baseScreenY = 82;
break;
case "brave":
@ -1414,7 +1426,7 @@ exports.startDebugInPrivate = function(url, proxy, profileName, debuggingPort, i
;
};
exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.4.18";
exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.4.20";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;

View File

@ -539,31 +539,59 @@ GeneratorFunction.Yield = function(message) {
GeneratorFunction.Yield.prototype = new Error();
GeneratorFunction.Yield.prototype.constructor = GeneratorFunction.Yield;
function StdStorage() {
this.data = {};
this.length = 0;
function StdAccessor(initialValue) {
var value = initialValue;
this.commit = function() {
this.length = Object.keys(data).length;
this.get = function() {
return value;
};
this.key = function(idx) {
var keyName = Object.keys(data)[idx];
return data[keyName];
this.set = function(v) {
value = v;
};
}
StdAccessor.isAccessor = function(obj) {
return obj instanceof StdAccessor;
};
function StdStorage() {
var data = new StdAccessor({});
var length = new StdAccessor(0);
function commit() {
length.set(Object.keys(data.get()).length);
}
this.setItem = function(keyName, keyValue) {
data[keyName] = keyValue;
this.commit();
var d = data.get();
d[keyName] = keyValue;
data.set(d);
commit();
};
this.getItem = function(keyName) {
return data[keyName];
return data.get()[keyName];
};
this.removeItem = function(keyName) {
delete data[keyName];
this.commit();
var d = data.get();
delete d[keyName];
data.set(d);
commit();
};
this.clear = function() {
this.data = {};
this.commit();
data.set({});
commit();
};
this.key = function(idx) {
var d = data.get();
return Object.keys(d)[idx];
};
this.length = function() {
return length.get();
};
}
@ -581,13 +609,14 @@ global.parseEnv = parseEnv;
exports.Event = StdEvent;
exports.EventTarget = StdEventTarget;
exports.Accessor = StdAccessor;
exports.Storage = StdStorage;
exports.alert = alert;
exports.confirm = confirm;
exports.prompt = prompt;
exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.16";
exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.17";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;