Piped/src/components/HistoryPage.vue

95 lines
3.1 KiB
Vue
Raw Normal View History

<template>
<h1 class="font-bold text-center" v-t="'titles.history'" />
<div class="flex md:items-center gap-2 flex-col md:flex-row">
<button class="btn" v-t="'actions.clear_history'" @click="clearHistory" />
<button class="btn" v-t="'actions.export_to_json'" @click="exportHistory" />
<div class="ml-auto flex gap-1 items-center">
2022-05-19 21:17:58 +00:00
<SortingSelector by-key="watchedAt" @apply="order => videos.sort(order)" />
2022-01-12 11:59:50 +00:00
</div>
2021-09-04 15:39:04 +00:00
</div>
<hr />
2021-12-27 14:46:22 +00:00
<div class="video-grid">
2022-11-01 12:12:54 +00:00
<VideoItem v-for="video in videos" :key="video.url" :item="video" />
</div>
2021-09-04 15:39:04 +00:00
<br />
</template>
<script>
2022-04-08 15:46:49 +00:00
import VideoItem from "./VideoItem.vue";
2022-05-19 21:17:58 +00:00
import SortingSelector from "./SortingSelector.vue";
export default {
components: {
VideoItem,
2022-05-19 21:17:58 +00:00
SortingSelector,
},
data() {
return {
videos: [],
};
},
mounted() {
(async () => {
if (window.db && this.getPreferenceBoolean("watchHistory", false)) {
var tx = window.db.transaction("watch_history", "readonly");
var store = tx.objectStore("watch_history");
2022-10-20 15:59:22 +00:00
const cursorRequest = store.index("watchedAt").openCursor(null, "prev");
cursorRequest.onsuccess = e => {
const cursor = e.target.result;
if (cursor) {
const video = cursor.value;
this.videos.push({
url: "/watch?v=" + video.videoId,
title: video.title,
uploaderName: video.uploaderName,
uploaderUrl: video.uploaderUrl,
duration: video.duration,
thumbnail: video.thumbnail,
watchedAt: video.watchedAt,
watched: true,
currentTime: video.currentTime,
});
if (this.videos.length < 1000) cursor.continue();
}
};
}
})();
},
activated() {
document.title = "Watch History - Piped";
},
methods: {
clearHistory() {
if (window.db) {
var tx = window.db.transaction("watch_history", "readwrite");
var store = tx.objectStore("watch_history");
store.clear();
}
this.videos = [];
},
exportHistory() {
const dateStr = new Date().toISOString().split(".")[0];
let json = {
format: "Piped",
version: 1,
playlists: [
{
name: `Piped History ${dateStr}`,
type: "history",
visibility: "private",
videos: this.videos.map(video => "https://youtube.com" + video.url),
},
],
};
this.download(JSON.stringify(json), `piped_history_${dateStr}.json`, "application/json");
},
},
};
</script>