fix: use session cookie for progress
This commit is contained in:
@@ -16,6 +16,8 @@ const sendBeacon = (payload: string) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
const hasSessionCookie = (): boolean => document.cookie.includes('session_id=');
|
||||
|
||||
let saveProgressInFlight: Promise<void> | null = null;
|
||||
|
||||
/**
|
||||
@@ -28,7 +30,7 @@ export const saveProgress = async (): Promise<void> => {
|
||||
const request = (async (): Promise<void> => {
|
||||
if (state.transitionEpisode !== null || !state.malID || state.video.currentTime < 1) return;
|
||||
// progress is user-scoped; avoid spamming 401s for anonymous sessions
|
||||
if (!document.cookie.includes('mal_session=')) return;
|
||||
if (!hasSessionCookie()) return;
|
||||
const episode = Number.parseInt(state.currentEpisode, 10);
|
||||
if (!episode) return;
|
||||
|
||||
@@ -79,7 +81,7 @@ const scheduleProgressSave = (): void => {
|
||||
export const markEpisodeTransition = (episodeNumber: number): void => {
|
||||
if (!state.malID || !episodeNumber) return;
|
||||
// progress is user-scoped; avoid sending beacons for anonymous sessions
|
||||
if (!document.cookie.includes('mal_session=')) return;
|
||||
if (!hasSessionCookie()) return;
|
||||
if (state.progressSaveTimer !== undefined) {
|
||||
window.clearTimeout(state.progressSaveTimer);
|
||||
state.progressSaveTimer = undefined;
|
||||
@@ -122,7 +124,7 @@ export const setupProgress = (): void => {
|
||||
// save on page close
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if (state.transitionEpisode !== null || state.completionSent || !state.malID) return;
|
||||
if (!document.cookie.includes('mal_session=')) return;
|
||||
if (!hasSessionCookie()) return;
|
||||
const episode = Number.parseInt(state.currentEpisode, 10);
|
||||
if (!episode) return;
|
||||
sendBeacon(buildPayload(episode, displayTimeFromAbsolute(state.video.currentTime)));
|
||||
|
||||
Reference in New Issue
Block a user