From 2a38e0cecc635df2122b142f2d0c9e13ac894772 Mon Sep 17 00:00:00 2001
From: Kelvin K <kelvin@futo.org>
Date: Thu, 13 Jun 2024 13:44:26 +0200
Subject: [PATCH] Playlist paging support

---
 YoutubeConfig.json |  2 +-
 YoutubeScript.js   | 51 +++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/YoutubeConfig.json b/YoutubeConfig.json
index 37c8daf..1c85234 100644
--- a/YoutubeConfig.json
+++ b/YoutubeConfig.json
@@ -7,7 +7,7 @@
 	"sourceUrl": "https://plugins.grayjay.app/Youtube/YoutubeConfig.json",
 	"repositoryUrl": "https://futo.org",
 	"scriptUrl": "./YoutubeScript.js",
-	"version": 179,
+	"version": 180,
 	"iconUrl": "./youtube.png",
 	"id": "35ae969a-a7db-11ed-afa1-0242ac120002",
 
diff --git a/YoutubeScript.js b/YoutubeScript.js
index 2044085..adbca06 100644
--- a/YoutubeScript.js
+++ b/YoutubeScript.js
@@ -1161,6 +1161,7 @@ source.getPlaylist = function (url) {
 		}
 
 		//TODO: Make a proper pager
+		/*
 		while (continuationToken) {
 			const newData = validateContinuation(()=>requestBrowse({
 				continuation: continuationToken
@@ -1183,7 +1184,7 @@ source.getPlaylist = function (url) {
 					}
 				});
 			}
-		}
+		}*/
 
 		let thumbnail = null;
 		if(videos && videos.length > 0 && 
@@ -1198,12 +1199,56 @@ source.getPlaylist = function (url) {
 			author: extractRuns_AuthorLink(playlistHeaderRenderer?.ownerText?.runs),
             name: title,
             thumbnail: thumbnail,
-            videoCount: videos.length,
-            contents: new VideoPager(videos)
+            videoCount: extractFirstNumber_Integer(extractText_String(playlistHeaderRenderer?.numVideosText)),
+            contents: new PlaylistVideoPager(videos, continuationToken)
         });
     }
     return null;
 };
+
+class PlaylistVideoPager extends VideoPager {
+	constructor(videos, continuation, useMobile = false, useAuth = false) {
+		super(videos, !!continuation);
+		this.continuation = continuation;
+		this.useMobile = useMobile;
+		this.useAuth = useAuth;
+	}
+	
+	nextPage() {
+		if(!this.continuation) {
+			this.hasMore = false;
+			this.results = [];
+			return this;
+		}
+		const newData = validateContinuation(()=>requestBrowse({
+			continuation: this.continuation
+		}, USE_MOBILE_PAGES, true));
+
+		if (newData.length < 1) {
+			this.hasMore = false;
+			this.results = [];
+			return this;
+		}
+		this.continuation = null;
+		let me = this;
+		let videos = [];
+		for(let playlistRenderer of newData) {
+			switchKey(playlistRenderer, {
+				playlistVideoRenderer(renderer) {
+					const video = extractPlaylistVideoRenderer_Video(renderer);
+					if(video)
+						videos.push(video);
+				},
+				continuationItemRenderer(continueRenderer) {
+					me.continuation = continueRenderer?.continuationEndpoint?.continuationCommand?.token;
+				}
+			});
+		}
+		this.results = videos;
+		this.hasMore = !!this.continuation;
+		return this;
+	}
+}
 source.getUserPlaylists = function() {
 	if (!bridge.isLoggedIn()) {
 		bridge.log("Failed to retrieve subscriptions page because not logged in.");
-- 
GitLab