From b42d257ab9b24d481ef93215aa2b693b75b493cc Mon Sep 17 00:00:00 2001 From: Lei PAN Date: Thu, 25 May 2023 23:52:43 +0200 Subject: [PATCH] can close the last page --- package.json | 2 - src/appointment.js | 1 - src/models/OCRResult.js | 1 + src/workers/CommandorPage.js | 87 ++++++++++++++++++++++++++++-------- src/workers/OCRChecker.js | 3 ++ 5 files changed, 72 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 1450668..c3183e8 100644 --- a/package.json +++ b/package.json @@ -13,14 +13,12 @@ "@nodegui/nodegui": "^0.57.1", "alert": "^5.1.1", "axios": "^0.27.2", - "electron-squirrel-startup": "^1.0.0", "mongodb": "^4.9.1", "mongoose": "^6.5.4", "node-schedule": "^2.1.0", "node-tesseract-ocr": "^2.2.1", "node-wget": "^0.4.3", "node-xlsx": "^0.21.0", - "opencv4nodejs": "wandenberg/opencv4nodejs", "playwright": "^1.32.1", "puppeteer": "^15.5.0", "read-ini-file": "^3.0.1", diff --git a/src/appointment.js b/src/appointment.js index 774c63d..94cb0dd 100644 --- a/src/appointment.js +++ b/src/appointment.js @@ -198,7 +198,6 @@ async function startBookWithNumbers(startNumber, endNumber, selectedStore, pathT console.log('Output was:\n', output); } // start chrome - device.shell("am start -n com.android.chrome/com.google.android.apps.chrome.Main") // device.shell("am start -n com.brave.browser/com.google.android.apps.chrome.Main") return attributedPort } diff --git a/src/models/OCRResult.js b/src/models/OCRResult.js index e13a766..13dfef4 100644 --- a/src/models/OCRResult.js +++ b/src/models/OCRResult.js @@ -9,6 +9,7 @@ const OCRResult = { PAGE_OPTIMIZATION: Symbol("PAGE_OPTIMIZATION"), CONFIRM_RESEND_FORM: Symbol("CONFIRM_RESEND_FORM"), RECHECK: Symbol("RECHECK"), + CLOSED: Symbol("CLOSED"), RECAPTCHA_ERROR: Symbol("RECAPTCHA_ERROR"), } module.exports = OCRResult \ No newline at end of file diff --git a/src/workers/CommandorPage.js b/src/workers/CommandorPage.js index ba3e64c..abf6c5c 100644 --- a/src/workers/CommandorPage.js +++ b/src/workers/CommandorPage.js @@ -14,6 +14,7 @@ const SlidingCaptchaSolver = require("./SlidingCaptchaSolver"); const OCRChecker = require("./OCRChecker"); const {browser} = require("yarn/lib/cli"); const {disconnect} = require("mongoose"); +const {execSync} = require("child_process"); // const RDV_URL = "http://192.168.0.44:8000/test_appointment.html" const RDV_URL = "https://rendezvousparis.hermes.com/client/register"; const BLANK_URL = "about:blank" @@ -81,7 +82,7 @@ class CommandorPage { this.isEmailFilled = false; this.isCountryChoosen = false; this.isPhoneInput = false; - this.firstStart = false; + this.firstStart = true; } @@ -131,29 +132,39 @@ class CommandorPage { } async loadPage() { + this.device.shell("am start -n com.android.chrome/com.google.android.apps.chrome.Main") + await delay(5 * 1000); // Connect to the device. logWithDevice("loadPage() called, with port:" + this.port, this.device); + if (this.browser) { + logWithDevice("browser.isConnected is " + this.browser.isConnected(), this.device); + } try { - this.browser = await puppeteer.connect({ - browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser", - headless: false, defaultViewport: null - }) - this.page = await this.browser.newPage(); + logWithDevice("connecting to browser:" + this.port, this.device); + while (this.browser === undefined || !this.browser.isConnected()) { + this.browser = await puppeteer.connect({ + browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser", + headless: false, defaultViewport: null + }) + // let pages = await this.browser.pages(); + // this.page = pages[0]; + this.page = await this.browser.newPage(); - this.page.on("load", (loadedPage) => { - this.onPageLoad(loadedPage) - }) + this.page.on("load", (loadedPage) => { + this.onPageLoad(loadedPage); + }) - this.page.on("response", (response) => { - this.onResponse(response) - }) + this.page.on("response", (response) => { + this.onResponse(response) + }) + await delay(3 * 1000); + } } catch (e) { await this.checkResultWithOcr() console.log(e) // this.isTerminated = true } if (this.page !== undefined) { - try { logWithDevice("will open google", this.device) if (!this.isTerminated) { @@ -170,7 +181,8 @@ class CommandorPage { await delay(10 * 1000); await this.clickOnlineAppointment(); await delay(2000); - await this.clickOnlineAppointment(); + if (!this.isTerminated) + await this.clickOnlineAppointment(); } else { this.isTerminated = true; } @@ -191,7 +203,6 @@ class CommandorPage { // } // this.page.close() - // this.device.close() clearInterval(intervalTask) cancel() // return this.browser @@ -618,7 +629,7 @@ class CommandorPage { let reserve = ReserveResultPojo.create_from_contact(this.contact, id, url, this.choosedStore, publishType); reserve.source_from = this.device.model(); await this.mongoManager.saveReserveToDb(reserve.to_mongo_dict()) - await this.deleteFromBlackList() + // await this.deleteFromBlackList() // await this.resetBrowser() this.isTerminated = true } @@ -719,19 +730,34 @@ class CommandorPage { switch (checkResult) { case OCRResult.FILL_FIELD: console.log("browser.isConnected: " + this.browser.isConnected()); - if (!this.browser.isConnected()) { + while (!this.browser.isConnected()) { + logWithDevice("trying to connect to browser", this.device) + // let cmd = 'adb -s ' + this.device.serial() + " forward tcp:" + this.port + " localabstract:chrome_devtools_remote"; + // console.log("cmd is " + cmd); + // const output = execSync(cmd, {encoding: 'utf-8'}); // the default is 'buffer' + // console.log('Output was:\n', output); this.browser = await puppeteer.connect({ browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser", headless: false, defaultViewport: null }) let pages = await this.browser.pages(); - this.page = pages[0]; + pages.forEach((currentPage) => { + if (currentPage.url() === RDV_URL) { + this.page = currentPage; + } + }) + await this.page.bringToFront(); + // this.page = pages; + // this.page.await + await delay(5 * 1000); } await this.fillFields(this.page) break; case OCRResult.SUCCESS: // reconnect to page and get url - await this.connect_to_browser(OCRResult.SUCCESS); + if (!this.isTerminated) { + await this.connect_to_browser(OCRResult.SUCCESS); + } break; case OCRResult.RECAPTCHA_ERROR: this.isTerminated = true; @@ -763,6 +789,10 @@ class CommandorPage { this.firstStart = true; await this.loadPage() break; + case OCRResult.CLOSED: + await this.closePage(); + // this.isTerminated = true; + break; default: await delay(5000); checkResult = ocrChecker.get_result(); @@ -773,6 +803,25 @@ class CommandorPage { } } + async closePage() { + while (!this.browser.isConnected()) { + logWithDevice("trying to connect to browser", this.device) + this.browser = await puppeteer.connect({ + browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser", + headless: false, defaultViewport: null + }) + await delay(2 * 1000); + } + console.log("will close all pages"); + let pages = await this.browser.pages(); + await pages[0].close() + console.log("page closed"); + // pages.forEach((currentPage) => { + // currentPage.close(); + // }) + // await this.browser.close(); + } + async tapLaterBtn() { logWithDevice("tapLaterBtn", this.device) let model = this.device.model(); diff --git a/src/workers/OCRChecker.js b/src/workers/OCRChecker.js index ffa39b9..8c8bd5d 100644 --- a/src/workers/OCRChecker.js +++ b/src/workers/OCRChecker.js @@ -32,6 +32,7 @@ const PAGE_OPTIMIZATION_CHROME_FR_2 = "Vous pouvez modifier vos options a tout m const PAGE_OPTIMIZATION_CHROME_FR_3 = "Vous pouvez effectuer des modifications" const ONLINE_APPOINTMENT = "Online Appointment" const CONFIRM_RESEND_FORM_FR = "Confirmer le nouvel envoi" +const CLOSED_MESSAGE_FR = "Depuis plus de 130 ans" class OCRChecker { @@ -72,6 +73,8 @@ class OCRChecker { return OCRResult.PAGE_OPTIMIZATION } else if (result.includes(CONFIRM_RESEND_FORM_FR)) { return OCRResult.CONFIRM_RESEND_FORM + } else if (result.includes(CLOSED_MESSAGE_FR)) { + return OCRResult.CLOSED } }