From 3b15fe19a7f60399374cf65e46d5668471848fa1 Mon Sep 17 00:00:00 2001 From: TheNetsky <56271887+TheNetsky@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:21:56 +0100 Subject: [PATCH] 1.2.5 --- package.json | 2 +- src/browser/Browser.ts | 3 +++ src/browser/BrowserFunc.ts | 2 +- src/functions/Workers.ts | 22 +++++++++++++++------- src/functions/activities/Quiz.ts | 2 +- src/functions/activities/Search.ts | 8 ++------ src/functions/activities/ThisOrThat.ts | 4 ++-- src/util/Logger.ts | 2 +- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 02c000c..bf4e3ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "microsoft-rewards-script", - "version": "1.2.4", + "version": "1.2.5", "description": "Automatically do tasks for Microsoft Rewards but in TS!", "main": "index.js", "engines": { diff --git a/src/browser/Browser.ts b/src/browser/Browser.ts index 4857ee6..7e50e99 100644 --- a/src/browser/Browser.ts +++ b/src/browser/Browser.ts @@ -30,6 +30,9 @@ class Browser { '--no-sandbox', '--mute-audio', '--disable-setuid-sandbox', + '--ignore-certificate-errors', + '--ignore-certificate-errors-spki-list', + '--ignore-ssl-errors', proxy.url ? `--proxy-server=${proxy.url}:${proxy.port}` : '' ] }) diff --git a/src/browser/BrowserFunc.ts b/src/browser/BrowserFunc.ts index 5094a97..892ac5d 100644 --- a/src/browser/BrowserFunc.ts +++ b/src/browser/BrowserFunc.ts @@ -30,7 +30,7 @@ export default class BrowserFunc { await this.bot.browser.utils.tryDismissCookieBanner(page) // Check if account is suspended - const isSuspended = await page.waitForSelector('#suspendedAccountHeader', { visible: true, timeout: 3000 }).then(() => true).catch(() => false) + const isSuspended = await page.waitForSelector('#suspendedAccountHeader', { visible: true, timeout: 2000 }).then(() => true).catch(() => false) if (isSuspended) { this.bot.log('GO-HOME', 'This account is suspended!', 'error') throw new Error('Account has been suspended!') diff --git a/src/functions/Workers.ts b/src/functions/Workers.ts index 2dfdab7..f6e8634 100644 --- a/src/functions/Workers.ts +++ b/src/functions/Workers.ts @@ -52,6 +52,9 @@ export class Workers { // Got to punch card index page in a new tab await page.goto(punchCard.parentPromotion.destinationUrl, { referer: this.bot.config.baseURL }) + // Wait for new page to load, max 10 seconds, however try regardless in case of error + await page.waitForNetworkIdle({ timeout: 10_000 }).catch(() => { }) + await this.solveActivities(page, activitiesUncompleted, punchCard) // Close the punch card index page @@ -101,7 +104,8 @@ export class Workers { // Promotion if (activity.priority === 1) { - selector = '#promo-item' + selector = await page.waitForSelector('#promo-item', { visible: true, timeout: 2000 }).then(() => true).catch(() => false) ? + '#promo-item' : activity.name } else { selector = `[data-bi-id="${activity.name}"]` } @@ -111,16 +115,20 @@ export class Workers { await page.waitForSelector(selector, { timeout: 10_000 }) // Click element, it will be opened in a new tab - await page.click(selector) - - // Cooldown - await this.bot.utils.wait(4000) + page.click(selector) // Select the new activity page const activityPage = await this.bot.browser.utils.getLatestTab(page) - // Wait for body to load - await activityPage.waitForSelector('body', { timeout: 10_000 }) + // Wait for the new tab to fully load, ignore error. + /* + Due to common false timeout on this function, we're ignoring the error regardless, if it worked then it's faster, + if it didn't then it gave enough time for the page to load. + */ + await activityPage.waitForNetworkIdle({ timeout: 10_000 }).catch(() => { }) + + // Cooldown + await this.bot.utils.wait(4000) switch (activity.promotionType) { // Quiz (Poll, Quiz or ABC) diff --git a/src/functions/activities/Quiz.ts b/src/functions/activities/Quiz.ts index 2b48dde..0e219e6 100644 --- a/src/functions/activities/Quiz.ts +++ b/src/functions/activities/Quiz.ts @@ -10,7 +10,7 @@ export class Quiz extends Workers { try { // Check if the quiz has been started or not - const quizNotStarted = await page.waitForSelector('#rqStartQuiz', { visible: true, timeout: 3000 }).then(() => true).catch(() => false) + const quizNotStarted = await page.waitForSelector('#rqStartQuiz', { visible: true, timeout: 2000 }).then(() => true).catch(() => false) if (quizNotStarted) { await page.click('#rqStartQuiz') } else { diff --git a/src/functions/activities/Search.ts b/src/functions/activities/Search.ts index f7a4bb3..b908be3 100644 --- a/src/functions/activities/Search.ts +++ b/src/functions/activities/Search.ts @@ -267,14 +267,11 @@ export class Search extends Workers { await page.click('#b_results .b_algo h2').catch(() => { }) // Since we don't really care if it did it or not - // Wait for website to load - await this.bot.utils.wait(3000) - // Will get current tab if no new one is created let lastTab = await this.bot.browser.utils.getLatestTab(page) - // Wait for the body of the new page to be loaded - await lastTab.waitForSelector('body', { timeout: 10_000 }).catch(() => { }) + // Let website load, if it doesn't load within 5 sec. exit regardless + await lastTab.waitForNetworkIdle({ timeout: 5000 }).catch(() => { }) // Check if the tab is closed or not if (!lastTab.isClosed()) { @@ -324,7 +321,6 @@ export class Search extends Workers { i++ } - } } catch (error) { this.bot.log('SEARCH-RANDOM-CLICK', 'An error occurred:' + error, 'error') diff --git a/src/functions/activities/ThisOrThat.ts b/src/functions/activities/ThisOrThat.ts index a73b6a7..f3913c4 100644 --- a/src/functions/activities/ThisOrThat.ts +++ b/src/functions/activities/ThisOrThat.ts @@ -11,7 +11,7 @@ export class ThisOrThat extends Workers { return try { // Check if the quiz has been started or not - const quizNotStarted = await page.waitForSelector('#rqStartQuiz', { visible: true, timeout: 3000 }).then(() => true).catch(() => false) + const quizNotStarted = await page.waitForSelector('#rqStartQuiz', { visible: true, timeout: 2000 }).then(() => true).catch(() => false) if (quizNotStarted) { await page.click('#rqStartQuiz') } else { @@ -24,7 +24,7 @@ export class ThisOrThat extends Workers { const quizData = await this.bot.browser.func.getQuizData(page) quizData // correctAnswer property is always null? - this.bot.log('THIS-OR-THAT', 'Completed the ThisOrthat successfully') + this.bot.log('THIS-OR-THAT', 'Completed the ThisOrThat successfully') } catch (error) { await page.close() this.bot.log('THIS-OR-THAT', 'An error occurred:' + error, 'error') diff --git a/src/util/Logger.ts b/src/util/Logger.ts index c01af21..39219da 100644 --- a/src/util/Logger.ts +++ b/src/util/Logger.ts @@ -1,7 +1,7 @@ import { Webhook } from './Webhook' export function log(title: string, message: string, type?: 'log' | 'warn' | 'error') { - const currentTime = new Date().toISOString() + const currentTime = new Date().toLocaleString() let str = ''