puppeteer_lib: Remove jQuery dependency from get_rendered_messages.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-03-12 17:38:44 -08:00 committed by Steve Howell
parent 73ca1afc53
commit 812adba5a3
1 changed files with 28 additions and 20 deletions

View File

@ -405,13 +405,17 @@ class CommonUtils {
* The messages are sorted chronologically. * The messages are sorted chronologically.
*/ */
async get_rendered_messages(page: Page, table = "zhome"): Promise<[string, string[]][]> { async get_rendered_messages(page: Page, table = "zhome"): Promise<[string, string[]][]> {
return await page.evaluate((table: string) => { const recipient_rows = await page.$$(`#${CSS.escape(table)} .recipient_row`);
const $recipient_rows = $(`#${CSS.escape(table)}`).find(".recipient_row"); return Promise.all(
return $recipient_rows.toArray().map((element): [string, string[]] => { recipient_rows.map(
const $el = $(element); async (element): Promise<[string, string[]]> => {
const stream_name = $el.find(".stream_label").text().trim(); const stream_label = await element.$(".stream_label");
const topic_name = $el.find(".stream_topic a").text().trim(); const stream_name = (await this.get_element_text(stream_label!)).trim();
const topic_label = await element.$(".stream_topic a");
const topic_name =
topic_label === null
? ""
: (await this.get_element_text(topic_label)).trim();
let key = stream_name; let key = stream_name;
if (topic_name !== "") { if (topic_name !== "") {
// If topic_name is '' then this is PMs, so only // If topic_name is '' then this is PMs, so only
@ -419,14 +423,18 @@ class CommonUtils {
key = `${stream_name} > ${topic_name}`; key = `${stream_name} > ${topic_name}`;
} }
const messages = $el const messages = await Promise.all(
.find(".message_row .message_content") (
.toArray() await element.$$(".message_row .message_content")
.map((message_row) => message_row.textContent!.trim()); ).map(async (message_row) =>
(await this.get_element_text(message_row)).trim(),
),
);
return [key, messages]; return [key, messages];
}); },
}, table); ),
);
} }
// This method takes in page, table to fetch the messages // This method takes in page, table to fetch the messages