From 5bc66c5fc972313bd5ede83d4572a4cb57f17ad6 Mon Sep 17 00:00:00 2001 From: TheNetsky <56271887+TheNetsky@users.noreply.github.com> Date: Mon, 2 Sep 2024 12:13:38 +0200 Subject: [PATCH] 1.4.10 - Fixed searches for some users --- package.json | 2 +- src/functions/activities/ReadToEarn.ts | 12 +++++------ src/functions/activities/Search.ts | 30 +++++++++++++++----------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index d07d0bb..e5c6701 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "microsoft-rewards-script", - "version": "1.4.9", + "version": "1.4.10", "description": "Automatically do tasks for Microsoft Rewards but in TS!", "main": "index.js", "engines": { diff --git a/src/functions/activities/ReadToEarn.ts b/src/functions/activities/ReadToEarn.ts index 02c0041..e3dd174 100644 --- a/src/functions/activities/ReadToEarn.ts +++ b/src/functions/activities/ReadToEarn.ts @@ -11,7 +11,6 @@ export class ReadToEarn extends Workers { this.bot.log('READ-TO-EARN', 'Starting Read to Earn') try { - let geoLocale = data.userProfile.attributes.country geoLocale = (this.bot.config.searchSettings.useGeoLocaleQueries && geoLocale.length === 2) ? geoLocale.toLowerCase() : 'us' @@ -26,7 +25,7 @@ export class ReadToEarn extends Workers { } const userDataResponse = await axios(userDataRequest) const userData = (await userDataResponse.data).response - let balance: number = userData.balance + let userBalance = userData.balance const jsonData = { amount: 1, @@ -38,7 +37,8 @@ export class ReadToEarn extends Workers { } } - for (let i = 0; i < 10; ++i) { + const articleCount = 10 + for (let i = 0; i < articleCount; ++i) { jsonData.id = randomBytes(64).toString('hex') const claimRequest = { url: 'https://prod.rewardsplatform.microsoft.com/dapi/me/activities', @@ -55,12 +55,12 @@ export class ReadToEarn extends Workers { const claimResponse = await axios(claimRequest) const newBalance = (await claimResponse.data).response.balance - if (newBalance == balance) { + if (newBalance == userBalance) { this.bot.log('READ-TO-EARN', 'Read all available articles') break } else { - balance = newBalance - this.bot.log('READ-TO-EARN', `Read article ${i + 1}`) + this.bot.log('READ-TO-EARN', `Read article ${i + 1} of ${articleCount} max | Gained ${newBalance - userBalance} Points`) + userBalance = newBalance await this.bot.utils.wait(Math.floor(this.bot.utils.randomNumber(this.bot.config.searchSettings.searchDelay.min, this.bot.config.searchSettings.searchDelay.max))) } } diff --git a/src/functions/activities/Search.ts b/src/functions/activities/Search.ts index eb146a5..b6990c6 100644 --- a/src/functions/activities/Search.ts +++ b/src/functions/activities/Search.ts @@ -10,11 +10,11 @@ import { GoogleSearch } from '../../interface/Search' export class Search extends Workers { - - private searchPageURL = 'https://bing.com' + private bingHome = 'https://bing.com' + private searchPageURL = '' public async doSearch(page: Page, data: DashboardData) { - this.bot.log('SEARCH-BING', 'Starting bing searches') + this.bot.log('SEARCH-BING', 'Starting Bing searches') page = await this.bot.browser.utils.getLatestTab(page) @@ -34,7 +34,7 @@ export class Search extends Workers { googleSearchQueries = [...new Set(googleSearchQueries)] // Go to bing - await page.goto(this.searchPageURL) + await page.goto(this.searchPageURL ? this.searchPageURL : this.bingHome) let maxLoop = 0 // If the loop hits 10 this when not gaining any points, we're assuming it's stuck. If it ddoesn't continue after 5 more searches with alternative queries, abort search @@ -133,11 +133,12 @@ export class Search extends Workers { private async bingSearch(searchPage: Page, query: string) { const platformControlKey = platform() === 'darwin' ? 'Meta' : 'Control' - - // Try a max of 5 times for (let i = 0; i < 5; i++) { try { + // This page had already been set to the Bing.com page or the previous search listing, we just need to select it + searchPage = await this.bot.browser.utils.getLatestTab(searchPage) + // Go to top of the page await searchPage.evaluate(() => { window.scrollTo(0, 0) @@ -163,6 +164,7 @@ export class Search extends Workers { // Bing.com in Chrome opens a new tab when searching const resultPage = await this.bot.browser.utils.getLatestTab(searchPage) + this.searchPageURL = new URL(resultPage.url()).href // Set the results page if (this.bot.config.searchSettings.scrollRandomResults) { await this.bot.utils.wait(2000) @@ -185,6 +187,7 @@ export class Search extends Workers { break } + this.bot.log('SEARCH-BING', 'Search failed, An error occurred:' + error, 'error') this.bot.log('SEARCH-BING', `Retrying search, attempt ${i}/5`, 'warn') @@ -286,16 +289,15 @@ export class Search extends Workers { private async clickRandomLink(page: Page) { try { - await page.click('#b_results .b_algo h2', { timeout: 2000 }).catch(() => { }) // Since we don't really care if it did it or not - // Will get current tab if no new one is created + // Will get current tab if no new one is created, this will always be the visited site or the result page if it failed to click let lastTab = await this.bot.browser.utils.getLatestTab(page) // Stay for 10 seconds await this.bot.utils.wait(10_000) - let lastTabURL = new URL(lastTab.url()) // Get new tab info + let lastTabURL = new URL(lastTab.url()) // Get new tab info, this is the website we've visited // Check if the URL is different from the original one, don't loop more than 5 times. let i = 0 @@ -325,11 +327,15 @@ export class Search extends Workers { // If only 1 tab is open, open a new one to search in } else if (tabs.length === 1) { const newPage = await browser.newPage() - await newPage.goto(this.searchPageURL) + await this.bot.utils.wait(1000) + await newPage.goto(this.bingHome) + await this.bot.utils.wait(3000) + this.searchPageURL = newPage.url() - // Else go back one page, this means the correct amount is open + // Else reset the last tab back to the search listing } else { - await lastTab.goBack().catch(() => { }) + lastTab = await this.bot.browser.utils.getLatestTab(lastTab) + await lastTab.goto(this.searchPageURL) } }