push
This commit is contained in:
1 parent
bcf05e8063
commit
f37985b4c8
109 files changed
+22810
-6143
No files matched your search
+150
-71
@@ -1,91 +1,170 @@
|
||||
/*
|
||||
STORAGE — resume points, kept in localStorage (survives restarts, no size
|
||||
worries like cookies, no per-request overhead).
|
||||
storage.js — resume points, My List and preferences in localStorage.
|
||||
|
||||
A "record" looks like:
|
||||
{
|
||||
key: "m:603" | "t:1396:s1:e2",
|
||||
type: "movie" | "tv",
|
||||
tmdbId: 1396,
|
||||
season: 1, // tv only
|
||||
episode: 2, // tv only
|
||||
show: "Show name",
|
||||
title: "Episode name", // tv only
|
||||
poster: "https://…/w342/…",
|
||||
t: 934.2, // seconds played
|
||||
d: 2710.5, // duration seconds
|
||||
at: 1712345678901,
|
||||
provider: "cineby"
|
||||
}
|
||||
Resume record (key bug-tv.resume.v1, unchanged from v4):
|
||||
{ key: "m:603" | "t:1396:s1:e2", type: "movie"|"tv", tmdbId, season?, episode?,
|
||||
show: "Show name", title?: "Episode name", poster: "https://…/w342/…",
|
||||
backdrop?: "https://…/w780/…", still?: "https://…/w300/…",
|
||||
t: seconds played, d: duration seconds, at: epoch ms, provider? }
|
||||
|
||||
One record per (movie) or (show, season, episode). The home page collapses
|
||||
to the most recent record per show.
|
||||
My List item (key bug-tv.mylist.v1):
|
||||
{ type, id, title, poster_path, backdrop_path, year, rating, addedAt }
|
||||
|
||||
Prefs (key bug-tv.prefs.v1): free-form { name: value }. Names used by the UI:
|
||||
subtitleLang ("" = off, "en", …), autoplayNext (bool, default true), volume (0..1), muted.
|
||||
|
||||
Every mutation dispatches a window event so views can refresh:
|
||||
"bugtv:resume", "bugtv:mylist", "bugtv:prefs" (detail = { key? / item? / name? })
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
import { safe } from './util.js';
|
||||
|
||||
const KEY = 'bug-tv.resume.v1';
|
||||
const MAX_RECORDS = 200;
|
||||
const RESUME_KEY = 'bug-tv.resume.v1';
|
||||
const LIST_KEY = 'bug-tv.mylist.v1';
|
||||
const PREFS_KEY = 'bug-tv.prefs.v1';
|
||||
const MAX_RECORDS = 200;
|
||||
|
||||
function readAll() {
|
||||
try {
|
||||
const parsed = JSON.parse(localStorage.getItem(KEY) || '{}');
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
function readObj(key) {
|
||||
const parsed = safe(() => JSON.parse(localStorage.getItem(key) || '{}'), {});
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
|
||||
}
|
||||
|
||||
function writeAll(all) {
|
||||
try {
|
||||
localStorage.setItem(KEY, JSON.stringify(all));
|
||||
} catch (e) {
|
||||
/* storage full/blocked — resume just won't persist */
|
||||
}
|
||||
}
|
||||
function writeObj(key, obj) {
|
||||
safe(() => localStorage.setItem(key, JSON.stringify(obj)));
|
||||
}
|
||||
|
||||
function keyFor({ type, tmdbId, season, episode }) {
|
||||
return type === 'tv' ? `t:${tmdbId}:s${season}:e${episode}` : `m:${tmdbId}`;
|
||||
}
|
||||
function emit(name, detail) {
|
||||
safe(() => window.dispatchEvent(new CustomEvent(name, { detail })));
|
||||
}
|
||||
|
||||
function get(key) {
|
||||
return readAll()[key] || null;
|
||||
}
|
||||
export function keyFor({ type, tmdbId, season, episode }) {
|
||||
return type === 'tv' ? `t:${tmdbId}:s${season}:e${episode}` : `m:${tmdbId}`;
|
||||
}
|
||||
|
||||
function save(record) {
|
||||
const all = readAll();
|
||||
all[record.key] = record;
|
||||
export const resume = {
|
||||
KEY: RESUME_KEY,
|
||||
keyFor,
|
||||
get(key) {
|
||||
return readObj(RESUME_KEY)[key] || null;
|
||||
},
|
||||
save(record) {
|
||||
if (!record || !record.key) return;
|
||||
const all = readObj(RESUME_KEY);
|
||||
all[record.key] = { ...record, at: record.at || Date.now() };
|
||||
const keys = Object.keys(all).sort((a, b) => (all[a].at || 0) - (all[b].at || 0));
|
||||
while (keys.length > MAX_RECORDS) delete all[keys.shift()];
|
||||
writeAll(all);
|
||||
}
|
||||
|
||||
function remove(key) {
|
||||
const all = readAll();
|
||||
writeObj(RESUME_KEY, all);
|
||||
emit('bugtv:resume', { key: record.key });
|
||||
},
|
||||
remove(key) {
|
||||
const all = readObj(RESUME_KEY);
|
||||
if (all[key]) {
|
||||
delete all[key];
|
||||
writeAll(all);
|
||||
writeObj(RESUME_KEY, all);
|
||||
emit('bugtv:resume', { key });
|
||||
}
|
||||
}
|
||||
|
||||
function clear() {
|
||||
writeAll({});
|
||||
}
|
||||
|
||||
function list() {
|
||||
return Object.values(readAll());
|
||||
}
|
||||
|
||||
},
|
||||
// remove every record of a movie/show
|
||||
removeShow(type, tmdbId) {
|
||||
const all = readObj(RESUME_KEY);
|
||||
let changed = false;
|
||||
for (const [k, r] of Object.entries(all)) {
|
||||
if (r && r.type === type && String(r.tmdbId) === String(tmdbId)) {
|
||||
delete all[k];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
writeObj(RESUME_KEY, all);
|
||||
emit('bugtv:resume', {});
|
||||
}
|
||||
},
|
||||
clear() {
|
||||
writeObj(RESUME_KEY, {});
|
||||
emit('bugtv:resume', {});
|
||||
},
|
||||
list() {
|
||||
return Object.values(readObj(RESUME_KEY)).filter((r) => r && r.key);
|
||||
},
|
||||
// records of one movie/show, newest first
|
||||
forShow(type, tmdbId) {
|
||||
return resume
|
||||
.list()
|
||||
.filter((r) => r.type === type && String(r.tmdbId) === String(tmdbId))
|
||||
.sort((a, b) => (b.at || 0) - (a.at || 0));
|
||||
},
|
||||
// one entry per movie/show: the most recently watched record wins
|
||||
function latestPerShow() {
|
||||
latestPerShow() {
|
||||
const byShow = new Map();
|
||||
for (const rec of list().sort((a, b) => (a.at || 0) - (b.at || 0))) {
|
||||
const showKey = rec.type === 'tv' ? `t:${rec.tmdbId}` : `m:${rec.tmdbId}`;
|
||||
byShow.set(showKey, rec);
|
||||
for (const rec of resume.list().sort((a, b) => (a.at || 0) - (b.at || 0))) {
|
||||
byShow.set(`${rec.type === 'tv' ? 't' : 'm'}:${rec.tmdbId}`, rec);
|
||||
}
|
||||
return [...byShow.values()].sort((a, b) => (b.at || 0) - (a.at || 0));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
window.SR = window.SR || {};
|
||||
SR.storage = { KEY, keyFor, get, save, remove, clear, list, latestPerShow };
|
||||
})();
|
||||
// fraction watched 0..1 for a record (0 when unknown)
|
||||
export function progressOf(rec) {
|
||||
return rec && rec.d > 0 ? Math.min(1, Math.max(0, rec.t / rec.d)) : 0;
|
||||
}
|
||||
|
||||
export const myList = {
|
||||
KEY: LIST_KEY,
|
||||
_all() {
|
||||
return readObj(LIST_KEY);
|
||||
},
|
||||
has(type, id) {
|
||||
return !!myList._all()[`${type}:${id}`];
|
||||
},
|
||||
// item: { type, id, title, poster_path?, backdrop_path?, year?, rating? } → returns new state
|
||||
toggle(item) {
|
||||
const all = myList._all();
|
||||
const k = `${item.type}:${item.id}`;
|
||||
let state;
|
||||
if (all[k]) {
|
||||
delete all[k];
|
||||
state = false;
|
||||
} else {
|
||||
all[k] = {
|
||||
type: item.type,
|
||||
id: item.id,
|
||||
title: item.title || item.name || '',
|
||||
poster_path: item.poster_path || null,
|
||||
backdrop_path: item.backdrop_path || null,
|
||||
year: item.year || '',
|
||||
rating: item.rating || item.vote_average || 0,
|
||||
addedAt: Date.now(),
|
||||
};
|
||||
state = true;
|
||||
}
|
||||
writeObj(LIST_KEY, all);
|
||||
emit('bugtv:mylist', { type: item.type, id: item.id, state });
|
||||
return state;
|
||||
},
|
||||
remove(type, id) {
|
||||
const all = myList._all();
|
||||
if (all[`${type}:${id}`]) {
|
||||
delete all[`${type}:${id}`];
|
||||
writeObj(LIST_KEY, all);
|
||||
emit('bugtv:mylist', { type, id, state: false });
|
||||
}
|
||||
},
|
||||
// newest first
|
||||
all() {
|
||||
return Object.values(myList._all()).sort((a, b) => (b.addedAt || 0) - (a.addedAt || 0));
|
||||
},
|
||||
};
|
||||
|
||||
export const prefs = {
|
||||
KEY: PREFS_KEY,
|
||||
get(name, fallback) {
|
||||
const all = readObj(PREFS_KEY);
|
||||
return Object.prototype.hasOwnProperty.call(all, name) ? all[name] : fallback;
|
||||
},
|
||||
set(name, value) {
|
||||
const all = readObj(PREFS_KEY);
|
||||
if (value === undefined) delete all[name];
|
||||
else all[name] = value;
|
||||
writeObj(PREFS_KEY, all);
|
||||
emit('bugtv:prefs', { name, value });
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user