Change version check to only macos

This commit is contained in:
naibo 2023-12-22 04:16:36 +08:00
parent 56b06c467a
commit 1982cb3c63
4 changed files with 1308 additions and 1292 deletions

View File

@ -1835,16 +1835,20 @@ class BrowserThread(Thread):
content = self.browser.title
elif p["contentType"] == 7:
# 获取整个网页的高度和宽度
height = self.browser.execute_script(
"return document.body.scrollHeight")
width = self.browser.execute_script(
"return document.body.scrollWidth")
size = self.browser.get_window_size()
width = size["width"]
height = size["height"]
# 调整浏览器窗口的大小
self.browser.set_window_size(width, height)
element.screenshot("Data/Task_" + str(self.id) + "/" + self.saveName +
"/" + str(time.time()) + ".png")
# 截图完成后,将浏览器的窗口大小设置为原来的大小
self.browser.set_window_size(width, height)
elif p["contentType"] == 8:
try:
size = self.browser.get_window_size()
width = size["width"]
height = size["height"]
screenshot = element.screenshot_as_png
screenshot_stream = io.BytesIO(screenshot)
# 使用Pillow库打开截图并转换为灰度图像
@ -1858,6 +1862,7 @@ class BrowserThread(Thread):
image_bytes = f.read()
content = ocr.classification(image_bytes)
os.remove(location)
self.browser.set_window_size(width, height)
# 使用Tesseract OCR引擎识别图像中的文本
# content = pytesseract.image_to_string(image, lang='chi_sim+eng')
except Exception as e:
@ -2258,13 +2263,21 @@ if __name__ == '__main__':
tmp_user_data_folder = os.path.join(tmp_user_folder_parent, "user_data_" + str(id) + "_" + str(time.time()).replace(".","") + "_" + random_string)
tmp_options[i]["tmp_user_data_folder"] = tmp_user_data_folder
if os.path.exists(tmp_user_data_folder):
try:
shutil.rmtree(tmp_user_data_folder)
except:
pass
print(f"Copying user data folder to: {tmp_user_data_folder}, please wait...")
print(f"正在复制用户信息目录到: {tmp_user_data_folder},请稍等...")
if os.path.exists(absolute_user_data_folder):
try:
shutil.copytree(absolute_user_data_folder, tmp_user_data_folder)
print("User data folder copied successfully, if you exit the program before it finishes, please delete the temporary user data folder manually.")
print("用户信息目录复制成功,如果程序在运行过程中被手动退出,请手动删除临时用户信息目录。")
except:
tmp_user_data_folder = absolute_user_data_folder
print("Copy user data folder failed, use the original folder.")
print("复制用户信息目录失败,使用原始目录。")
else:
tmp_user_data_folder = absolute_user_data_folder
print("Cannot find user data folder, create a new folder.")

Binary file not shown.

Binary file not shown.

View File

@ -17,12 +17,12 @@ const {
StaleElementReferenceException,
} = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { ServiceBuilder } = require("selenium-webdriver/chrome");
const { rootCertificates } = require("tls");
const { exit } = require("process");
const {ServiceBuilder} = require("selenium-webdriver/chrome");
const {rootCertificates} = require("tls");
const {exit} = require("process");
const path = require("path");
const fs = require("fs");
const { exec, spawn, execFile } = require("child_process");
const {exec, spawn, execFile} = require("child_process");
const iconPath = path.join(__dirname, "favicon.ico");
const task_server = require(path.join(__dirname, "server.js"));
const util = require("util");
@ -38,7 +38,7 @@ let config_context = JSON.parse(
if (config.debug) {
let logPath = "info.log";
let logFile = fs.createWriteStream(logPath, { flags: "a" });
let logFile = fs.createWriteStream(logPath, {flags: "a"});
console.log = function () {
logFile.write(util.format.apply(null, arguments) + "\n");
process.stdout.write(util.format.apply(null, arguments) + "\n");
@ -150,10 +150,10 @@ function createWindow() {
config.user_data_folder +
"&copyright=" +
config.copyright,
{ extraHeaders: "pragma: no-cache\n" }
{extraHeaders: "pragma: no-cache\n"}
);
// 隐藏菜单栏
const { Menu } = require("electron");
const {Menu} = require("electron");
Menu.setApplicationMenu(null);
mainWindow.on("close", function (e) {
if (process.platform !== "darwin") {
@ -360,7 +360,7 @@ async function beginInvoke(msg, ws) {
server_address;
}
console.log(url);
flowchart_window.loadURL(url, { extraHeaders: "pragma: no-cache\n" });
flowchart_window.loadURL(url, {extraHeaders: "pragma: no-cache\n"});
}
mainWindow.hide();
// Prints the currently focused window bounds.
@ -373,7 +373,7 @@ async function beginInvoke(msg, ws) {
config_context.user_data_folder == undefined ||
config_context.user_data_folder == ""
) {
const { windowManager } = require("node-window-manager");
const {windowManager} = require("node-window-manager");
const window = windowManager.getActiveWindow();
console.log(window);
windowManager.requestAccessibility();
@ -425,7 +425,7 @@ async function beginInvoke(msg, ws) {
if (type.indexOf("Click") >= 0) {
await click_element(element, type);
} else if (type.indexOf("Move") >= 0) {
await driver.actions().move({ origin: element }).perform();
await driver.actions().move({origin: element}).perform();
}
}
} else {
@ -459,7 +459,7 @@ async function beginInvoke(msg, ws) {
if (xpath.includes("point(")) {
xpath = "//body";
}
let elementInfo = { iframe: parameters.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: parameters.iframe, xpath: xpath, id: -1};
//用于跳转到元素位置
let element = await findElementAcrossAllWindows(elementInfo);
} else if (option == 3) {
@ -476,7 +476,7 @@ async function beginInvoke(msg, ws) {
}
xpath = parent_xpath + xpath;
}
let elementInfo = { iframe: param.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: param.iframe, xpath: xpath, id: -1};
let element = await findElementAcrossAllWindows(elementInfo);
} else if (option == 11) {
let params = parameters.params; //所有的提取数据参数
@ -493,7 +493,7 @@ async function beginInvoke(msg, ws) {
}
xpath = parent_xpath + xpath;
}
let elementInfo = { iframe: param.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: param.iframe, xpath: xpath, id: -1};
let element = await findElementAcrossAllWindows(elementInfo);
} else if (option == 8) {
let loopType = parameters.loopType;
@ -504,7 +504,7 @@ async function beginInvoke(msg, ws) {
} else if (loopType == 2) {
xpath = parameters.pathList.split("\n")[0].trim();
}
let elementInfo = { iframe: parameters.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: parameters.iframe, xpath: xpath, id: -1};
let element = await findElementAcrossAllWindows(elementInfo);
} else if (loopType == 5) {
//JavaScript命令返回值
@ -567,7 +567,7 @@ async function beginInvoke(msg, ws) {
}
}
}
send_message_to_browser(JSON.stringify({ type: "trial", message: msg }));
send_message_to_browser(JSON.stringify({type: "trial", message: msg}));
} else {
//试运行
try {
@ -627,7 +627,7 @@ async function beginInvoke(msg, ws) {
if (xpath.includes("point(")) {
xpath = "//body";
}
let elementInfo = { iframe: parameters.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: parameters.iframe, xpath: xpath, id: -1};
if (parameters.useLoop && !parameters.xpath.includes("point(")) {
let parent_node = JSON.parse(msg.message.parentNode);
let parent_xpath = parent_node.parameters.xpath;
@ -668,14 +668,14 @@ async function beginInvoke(msg, ws) {
}
}
} else if (option == 7) {
await driver.actions().move({ origin: element }).perform();
await driver.actions().move({origin: element}).perform();
}
await execute_js(
parameters.afterJS,
element,
parameters.afterJSWaitTime
);
send_message_to_browser(JSON.stringify({ type: "cancelSelection" }));
send_message_to_browser(JSON.stringify({type: "cancelSelection"}));
} else if (option == 3) {
//提取数据
notify_browser(
@ -698,7 +698,7 @@ async function beginInvoke(msg, ws) {
}
xpath = parent_xpath + xpath;
}
let elementInfo = { iframe: param.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: param.iframe, xpath: xpath, id: -1};
let element = await findElementAcrossAllWindows(
elementInfo,
(notifyBrowser = false)
@ -862,7 +862,7 @@ async function beginInvoke(msg, ws) {
}
xpath = parent_xpath + xpath;
}
let elementInfo = { iframe: param.iframe, xpath: xpath, id: -1 };
let elementInfo = {iframe: param.iframe, xpath: xpath, id: -1};
let element = await findElementAcrossAllWindows(
elementInfo,
(notifyBrowser = false)
@ -961,7 +961,7 @@ async function beginInvoke(msg, ws) {
.map((cookie) => `${cookie.name}=${cookie.value}`)
.join("\n");
socket_flowchart.send(
JSON.stringify({ type: "GetCookies", message: cookiesText })
JSON.stringify({type: "GetCookies", message: cookiesText})
);
} catch {
console.log("Cannot get Cookies");
@ -1069,9 +1069,9 @@ function send_message_to_browser(message) {
}
const WebSocket = require("ws");
const { all } = require("express/lib/application");
const { copy } = require("selenium-webdriver/io");
let wss = new WebSocket.Server({ port: websocket_port });
const {all} = require("express/lib/application");
const {copy} = require("selenium-webdriver/io");
let wss = new WebSocket.Server({port: websocket_port});
wss.on("connection", function (ws) {
ws.on("message", async function (message, isBinary) {
let msg = JSON.parse(message.toString());
@ -1114,12 +1114,12 @@ wss.on("connection", function (ws) {
await driver
.manage()
.window()
.setRect({ width: width, height: height + 10 });
.setRect({width: width, height: height + 10});
// height = height - 1;
await driver
.manage()
.window()
.setRect({ width: width, height: height });
.setRect({width: width, height: height});
}
await new Promise((resolve) => setTimeout(resolve, 2000));
handle_pairs[msg.message.id] = current_handle;
@ -1132,7 +1132,7 @@ wss.on("connection", function (ws) {
msg.message.title
);
socket_flowchart.send(
JSON.stringify({ type: "title", data: { title: msg.message.title } })
JSON.stringify({type: "title", data: {title: msg.message.title}})
);
allWindowSockets.push(ws);
allWindowScoketNames.push(msg.message.id);
@ -1237,7 +1237,7 @@ async function runBrowser(lang = "en", user_data_folder = "", mobile = false) {
.build();
await driver
.manage()
.setTimeouts({ implicit: 3, pageLoad: 10000, script: 10000 });
.setTimeouts({implicit: 3, pageLoad: 10000, script: 10000});
await driver.executeScript(
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
);
@ -1256,7 +1256,7 @@ async function runBrowser(lang = "en", user_data_folder = "", mobile = false) {
await driver
.manage()
.window()
.setRect({ width: width * 1.2, height: height });
.setRect({width: width * 1.2, height: height});
}
try {
if (mobile) {
@ -1326,7 +1326,7 @@ function handleOpenBrowser(
mobile.toString();
}
// and load the index.html of the app.
flowchart_window.loadURL(url, { extraHeaders: "pragma: no-cache\n" });
flowchart_window.loadURL(url, {extraHeaders: "pragma: no-cache\n"});
if (process.platform != "darwin") {
flowchart_window.hide();
}
@ -1337,7 +1337,7 @@ function handleOpenBrowser(
}
function handleOpenInvoke(event, lang = "en") {
invoke_window = new BrowserWindow({ icon: iconPath });
invoke_window = new BrowserWindow({icon: iconPath});
let url = "";
language = lang;
if (lang == "en") {
@ -1353,7 +1353,7 @@ function handleOpenInvoke(event, lang = "en") {
"&lang=zh";
}
// and load the index.html of the app.
invoke_window.loadURL(url, { extraHeaders: "pragma: no-cache\n" });
invoke_window.loadURL(url, {extraHeaders: "pragma: no-cache\n"});
invoke_window.maximize();
mainWindow.hide();
invoke_window.on("close", function (event) {
@ -1367,7 +1367,7 @@ function handleOpenInvoke(event, lang = "en") {
app.whenReady().then(() => {
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
details.requestHeaders["Accept-Language"] = "zh";
callback({ cancel: false, requestHeaders: details.requestHeaders });
callback({cancel: false, requestHeaders: details.requestHeaders});
});
ipcMain.on("start-design", handleOpenBrowser);
ipcMain.on("start-invoke", handleOpenInvoke);
@ -1387,6 +1387,7 @@ app.whenReady().then(() => {
createWindow();
}
});
if (process.platform === "darwin") {
// 获取Chrome版本
execFile(chromeBinaryPath, ["--version"], (error, chromeStdout) => {
if (error) {
@ -1439,6 +1440,8 @@ app.whenReady().then(() => {
}
});
});
}
});
// Quit when all windows are closed, except on macOS. There, it's common