35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
const REDDITBOT_SESSION_FILENAME = 'redditbot-session.json';
|
|
|
|
function isRedditDomain(domain) {
|
|
const normalised = domain.toLowerCase().replace(/^\./, '');
|
|
return normalised === 'reddit.com' || normalised.endsWith('.reddit.com');
|
|
}
|
|
|
|
function toExportCookie(cookie) {
|
|
return {
|
|
name: cookie.name,
|
|
value: cookie.value,
|
|
domain: cookie.domain,
|
|
path: cookie.path,
|
|
expires: cookie.expirationDate,
|
|
httpOnly: cookie.httpOnly,
|
|
secure: cookie.secure,
|
|
sameSite: cookie.sameSite,
|
|
};
|
|
}
|
|
|
|
export function createSessionExportDownload(cookies, matrix) {
|
|
if (!matrix?.homeserver || !matrix?.accessToken || !matrix?.userId) {
|
|
throw new Error('Open Reddit Chat before exporting so Matrix credentials can be captured.');
|
|
}
|
|
const exportFile = {
|
|
cookies: cookies.filter((cookie) => isRedditDomain(cookie.domain)).map(toExportCookie),
|
|
matrix,
|
|
};
|
|
|
|
return {
|
|
filename: REDDITBOT_SESSION_FILENAME,
|
|
url: `data:application/json;charset=utf-8,${encodeURIComponent(JSON.stringify(exportFile))}`,
|
|
};
|
|
}
|