Skip to content

Instantly share code, notes, and snippets.

@ctjlewis
Created July 7, 2022 06:49
Show Gist options
  • Save ctjlewis/8226cae5e79abcd433d2e70b842399db to your computer and use it in GitHub Desktop.
Save ctjlewis/8226cae5e79abcd433d2e70b842399db to your computer and use it in GitHub Desktop.
Scroll up to a given timestamp.
const messageList = document.querySelector("main > div > div > div > div:nth-child(2) > div > div > div > div > div");
/**
* Scroll to the top of the message list.
**/
const scrollToTop = async () => {
messageList.scrollTop = 0;
return await new Promise((resolve) => setTimeout(resolve, 1000));
};
/**
* Get a Date object from a relative timestamp (e.g. "8:12 PM") or
* full date string.
**/
const getDate = (dateString) => {
/**
* If no date prefix, add one.
**/
if (Number(dateString[0])) {
dateString = `${new Date().toDateString()}, ${dateString}`;
} else if (dateString.startsWith("Yesterday")) {
const yesterday = new Date(new Date().setDate(new Date().getDate() - 1));
dateString = dateString.replace("Yesterday", yesterday.toDateString());
}
return new Date(dateString);
}
/**
* Get the earliest timestamp from the loaded messages.
**/
const getEarliestTimestamp = async () => {
const timestamp = messageList.querySelector("div[data-testid='cellInnerDiv'] > div > div:nth-child(2) div[dir='auto'] span:nth-child(3)").textContent;
const earliestTimestamp = getDate(timestamp);
console.log({ earliestTimestamp });
return await new Promise(
(resolve) => setTimeout(() => resolve(earliestTimestamp))
);
};
/**
* Scroll until the earliest timestamp is <= the given date.
**/
const scrollUntilTimestamp = async (dateString) => {
const scrollUntil = getDate(dateString);
console.log(`Scrolling until ${scrollUntil}`);
while (await getEarliestTimestamp() > scrollUntil) {
await scrollToTop();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment