diff --git a/src/account_x.ts b/src/account_x.ts index 61b9c691..f9976cdb 100644 --- a/src/account_x.ts +++ b/src/account_x.ts @@ -378,42 +378,6 @@ export class XAccountController { ); } - async getUsernameStart() { - log.info("XAccountController.getUsernameStart"); - const ses = session.fromPartition(`persist:account-${this.account?.id}`); - await ses.clearCache(); - await this.mitmController.startMonitoring(); - await this.mitmController.startMITM(ses, ["api.x.com/1.1/account/settings.json"]); - } - - async getUsernameStop() { - log.info("XAccountController.getUsernameStop"); - await this.mitmController.stopMonitoring(); - const ses = session.fromPartition(`persist:account-${this.account?.id}`); - await this.mitmController.stopMITM(ses); - } - - async getUsername(): Promise { - log.info("XAccountController.getUsername"); - let username = null; - if (!this.account) { - log.error("XAccountController.getUsername: account not found"); - return username; - } - - // See if we got settings.json - for (let i = 0; i < this.mitmController.responseData.length; i++) { - // If URL starts with /1.1/account/settings.json - if (this.mitmController.responseData[i].url.includes("/1.1/account/settings.json") && this.mitmController.responseData[i].status == 200) { - const body = JSON.parse(this.mitmController.responseData[i].body); - username = body.screen_name; - break; - } - } - - return username; - } - async indexStart() { const ses = session.fromPartition(`persist:account-${this.account?.id}`); await ses.clearCache(); @@ -1575,33 +1539,6 @@ export const defineIPCX = () => { } }); - ipcMain.handle('X:getUsernameStart', async (_, accountID: number): Promise => { - try { - const controller = getXAccountController(accountID); - await controller.getUsernameStart(); - } catch (error) { - throw new Error(packageExceptionForReport(error as Error)); - } - }); - - ipcMain.handle('X:getUsernameStop', async (_, accountID: number): Promise => { - try { - const controller = getXAccountController(accountID); - await controller.getUsernameStop(); - } catch (error) { - throw new Error(packageExceptionForReport(error as Error)); - } - }); - - ipcMain.handle('X:getUsername', async (_, accountID: number): Promise => { - try { - const controller = getXAccountController(accountID); - return await controller.getUsername(); - } catch (error) { - throw new Error(packageExceptionForReport(error as Error)); - } - }); - ipcMain.handle('X:indexStart', async (_, accountID: number) => { try { const controller = getXAccountController(accountID); diff --git a/src/preload.ts b/src/preload.ts index d6fd8d03..572ad7bc 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -107,15 +107,6 @@ contextBridge.exposeInMainWorld('electron', { updateJob: (accountID: number, jobJSON: XJob) => { ipcRenderer.invoke('X:updateJob', accountID, jobJSON) }, - getUsernameStart: (accountID: number): Promise => { - return ipcRenderer.invoke('X:getUsernameStart', accountID) - }, - getUsernameStop: (accountID: number) => { - ipcRenderer.invoke('X:getUsernameStop', accountID) - }, - getUsername: (accountID: number): Promise => { - return ipcRenderer.invoke('X:getUsername', accountID) - }, indexStart: (accountID: number) => { ipcRenderer.invoke('X:indexStart', accountID) }, diff --git a/src/renderer/src/main.ts b/src/renderer/src/main.ts index 7eb4abd8..cdc984a1 100644 --- a/src/renderer/src/main.ts +++ b/src/renderer/src/main.ts @@ -56,9 +56,6 @@ declare global { createJobs: (accountID: number, jobTypes: string[]) => Promise; getLastFinishedJob: (accountID: number, jobType: string) => Promise; updateJob: (accountID: number, jobJSON: string) => void; - getUsernameStart: (accountID: number) => Promise; - getUsernameStop: (accountID: number) => void; - getUsername: (accountID: number) => Promise; indexStart: (accountID: number) => void; indexStop: (accountID: number) => void; indexParseTweets: (accountID: number, isFirstRun: boolean) => Promise; diff --git a/src/renderer/src/test_util.ts b/src/renderer/src/test_util.ts index 82946114..814065f4 100644 --- a/src/renderer/src/test_util.ts +++ b/src/renderer/src/test_util.ts @@ -35,9 +35,6 @@ export const stubElectron = () => { createJobs: cy.stub(), getLastFinishedJob: cy.stub(), updateJob: cy.stub(), - getUsernameStart: cy.stub(), - getUsernameStop: cy.stub(), - getUsername: cy.stub(), indexStart: cy.stub(), indexStop: cy.stub(), indexParseTweets: cy.stub(), diff --git a/src/renderer/src/view_models/AccountXViewModel.ts b/src/renderer/src/view_models/AccountXViewModel.ts index b7b9b0d6..a7d768b2 100644 --- a/src/renderer/src/view_models/AccountXViewModel.ts +++ b/src/renderer/src/view_models/AccountXViewModel.ts @@ -361,7 +361,6 @@ export class AccountXViewModel extends BaseViewModel { async login() { const originalUsername = this.account && this.account.xAccount && this.account.xAccount.username ? this.account.xAccount.username : null; - let tries: number, success: boolean; this.showBrowser = true; @@ -400,33 +399,20 @@ export class AccountXViewModel extends BaseViewModel { // Get the username this.log("login", "getting username"); - this.instructions = `You've logged in successfully. Now I'm scraping your username...`; + this.instructions = `You're logged in. Now I'm scraping your username...`; + if (this.webview.getURL() != "https://x.com/home") { + await this.loadURLWithRateLimit("https://x.com/home"); + } - let username: string | null = null; - success = false; - for (tries = 0; tries < 3; tries++) { - await window.electron.X.getUsernameStart(this.account.id); - await this.loadURLWithRateLimit("https://x.com/settings/account"); - await this.waitForSelector('a[href="/settings/your_twitter_data/account"]', "https://x.com/settings/account"); - username = await window.electron.X.getUsername(this.account.id); - await window.electron.X.getUsernameStop(this.account.id); + // Wait for profile button to appear, and click it + await this.waitForSelector('a[aria-label="Profile"]', "https://x.com/home"); + await this.scriptClickElement('a[aria-label="Profile"]'); - if (username) { - success = true; - break; - } else { - this.log("login", `failed to get username, try #${tries}`); - await this.sleep(1000); - } - } - if (!success) { - const latestResponseData = await window.electron.X.getLatestResponseData(this.account.id); - await this.error(AutomationErrorType.X_login_FailedToGetUsername, {}, { - latestResponseData: latestResponseData, - currentURL: this.webview.getURL() - }); - return; - } + // Wait for profile page to load, and get the username from the URL + await this.waitForSelector('div[data-testid="UserName"]'); + const url = this.webview.getURL(); + const urlObj = new URL(url); + const username = urlObj.pathname.substring(1); if (originalUsername !== null && username != originalUsername) { this.log("login", `username changed from ${this.account.xAccount?.username} to ${username}`);