From d20e5750300ccb7e72d09d114a30537fd8c28bab Mon Sep 17 00:00:00 2001 From: Shivam Date: Mon, 8 Mar 2021 00:25:37 +0530 Subject: [PATCH] Added pagination in getInbox. getInbox function now accepts two arguments: start and limit (both integers) start - Specifies the starting index of the results. (index starts from 0) limit - Specifies how many elements we want after the start value. -1 for infinite. For example: start = 10 and limit = 4 will return the 11th, 12th,13th and 14th elements. --- lib/service.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/service.ts b/lib/service.ts index 2fa04f0..324a875 100644 --- a/lib/service.ts +++ b/lib/service.ts @@ -13,12 +13,11 @@ class MessageService { this.page = page } - async getInbox() { - // TODO: add pagination + async getInbox(start=0, limit=-1) { await this.page.waitForNavigation({ waitUntil: 'load' }) await this.page.waitForSelector('body > mw-app > mw-bootstrap > div > main > mw-main-container > div > mw-main-nav > mws-conversations-list > nav > div.conv-container.ng-star-inserted > mws-conversation-list-item') - const inbox = await this.page.evaluate(() => { + const inbox = await this.page.evaluate((start, limit) => { function evalConvoElement (conversation: Element) { const props: Conversation = { unread: false, // querySelector find .unread class @@ -49,13 +48,20 @@ class MessageService { const conversations = document.querySelectorAll("body > mw-app > mw-bootstrap > div > main > mw-main-container > div > mw-main-nav > mws-conversations-list > nav > div.conv-container.ng-star-inserted > mws-conversation-list-item") const msgs = [] + var i =0 for (const conversation of conversations) { if (conversation) { - msgs.push(evalConvoElement(conversation)) + if(limit >= 0 && i>limit+start) { + break + } + if(i>=start){ + msgs.push(evalConvoElement(conversation)) + } + i++ } } return msgs - }) + }, start, limit) return inbox }