2021-03-26 23:53:30 +01:00
|
|
|
/* global $, CSS */
|
2020-12-11 03:08:45 +01:00
|
|
|
|
2024-11-13 00:59:12 +01:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import path from "node:path";
|
|
|
|
import {parseArgs} from "node:util";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2024-11-13 00:59:12 +01:00
|
|
|
import "css.escape";
|
|
|
|
import puppeteer from "puppeteer";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
const usage = "Usage: message-screenshot.js <message_id> <image_path> <realm_url>";
|
|
|
|
const {
|
|
|
|
values: {help},
|
|
|
|
positionals: [messageId, imagePath, realmUrl],
|
|
|
|
} = parseArgs({options: {help: {type: "boolean"}}, allowPositionals: true});
|
2020-04-10 19:49:18 +02:00
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
if (help) {
|
|
|
|
console.log(usage);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
if (realmUrl === undefined) {
|
|
|
|
console.error(usage);
|
2020-04-10 19:49:18 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2024-11-11 00:10:00 +01:00
|
|
|
console.log(`Capturing screenshot for message ${messageId} to ${imagePath}`);
|
|
|
|
|
2023-02-22 23:04:11 +01:00
|
|
|
// TODO: Refactor to share code with web/e2e-tests/realm-creation.test.ts
|
2020-04-10 19:49:18 +02:00
|
|
|
async function run() {
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
args: [
|
2020-07-15 01:29:15 +02:00
|
|
|
"--window-size=1400,1024",
|
2020-07-15 00:34:28 +02:00
|
|
|
"--no-sandbox",
|
|
|
|
"--disable-setuid-sandbox",
|
2020-04-10 19:49:18 +02:00
|
|
|
// Helps render fonts correctly on Ubuntu: https://github.com/puppeteer/puppeteer/issues/661
|
2020-07-15 01:29:15 +02:00
|
|
|
"--font-render-hinting=none",
|
2020-04-10 19:49:18 +02:00
|
|
|
],
|
|
|
|
defaultViewport: null,
|
2023-07-15 02:21:50 +02:00
|
|
|
headless: "new",
|
2020-04-10 19:49:18 +02:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
const page = await browser.newPage();
|
|
|
|
// deviceScaleFactor:2 gives better quality screenshots (higher pixel density)
|
2020-07-16 22:40:18 +02:00
|
|
|
await page.setViewport({width: 1280, height: 1024, deviceScaleFactor: 2});
|
2024-11-11 00:10:00 +01:00
|
|
|
await page.goto(`${realmUrl}/devlogin`);
|
2020-06-13 12:40:21 +02:00
|
|
|
// wait for Iago devlogin button and click on it.
|
|
|
|
await page.waitForSelector('[value="iago@zulip.com"]');
|
|
|
|
|
|
|
|
// By waiting till DOMContentLoaded we're confirming that Iago is logged in.
|
|
|
|
await Promise.all([
|
2020-07-16 22:40:18 +02:00
|
|
|
page.waitForNavigation({waitUntil: "domcontentloaded"}),
|
2020-06-13 12:40:21 +02:00
|
|
|
page.click('[value="iago@zulip.com"]'),
|
|
|
|
]);
|
2020-04-10 19:49:18 +02:00
|
|
|
|
|
|
|
// Navigate to message and capture screenshot
|
2024-11-11 00:10:00 +01:00
|
|
|
await page.goto(`${realmUrl}/#narrow/id/${messageId}`, {
|
2023-07-15 02:21:50 +02:00
|
|
|
waitUntil: "networkidle2",
|
|
|
|
});
|
2024-01-17 07:53:40 +01:00
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
const message_list_id = await page.evaluate(() => zulip_test.current_msg_list.id);
|
2024-11-11 00:10:00 +01:00
|
|
|
const messageSelector = `#message-row-${message_list_id}-${CSS.escape(messageId)}`;
|
2020-04-10 19:49:18 +02:00
|
|
|
await page.waitForSelector(messageSelector);
|
|
|
|
// remove unread marker and don't select message
|
2024-11-11 00:10:00 +01:00
|
|
|
const marker = `#message-row-${message_list_id}-${CSS.escape(messageId)} .unread_marker`;
|
2020-12-11 03:08:45 +01:00
|
|
|
await page.evaluate((sel) => $(sel).remove(), marker);
|
2020-04-10 19:49:18 +02:00
|
|
|
const messageBox = await page.$(messageSelector);
|
2021-03-29 21:33:19 +02:00
|
|
|
await page.evaluate((msg) => $(msg).removeClass("selected_message"), messageSelector);
|
2023-07-15 02:21:50 +02:00
|
|
|
const messageGroup = await messageBox.$("xpath/..");
|
2020-04-10 19:49:18 +02:00
|
|
|
// Compute screenshot area, with some padding around the message group
|
2020-12-22 00:37:36 +01:00
|
|
|
const clip = {...(await messageGroup.boundingBox())};
|
2020-04-10 19:49:18 +02:00
|
|
|
clip.x -= 5;
|
|
|
|
clip.width += 10;
|
2023-07-15 02:21:50 +02:00
|
|
|
clip.y += 5;
|
2020-04-24 14:48:28 +02:00
|
|
|
const imageDir = path.dirname(imagePath);
|
2024-11-10 22:34:32 +01:00
|
|
|
await fs.promises.mkdir(imageDir, {recursive: true});
|
2020-07-20 22:18:43 +02:00
|
|
|
await page.screenshot({path: imagePath, clip});
|
2020-10-07 10:20:41 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2020-04-10 19:49:18 +02:00
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
|
|
|
await browser.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|