From eba1bb5d2ccf438a2f025a204d2d443e4b76b144 Mon Sep 17 00:00:00 2001
From: Kelvin <kelvin@futo.org>
Date: Wed, 18 Oct 2023 20:09:42 +0200
Subject: [PATCH] Add more captcha checks

---
 YoutubeConfig.json |  2 +-
 YoutubeScript.js   | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/YoutubeConfig.json b/YoutubeConfig.json
index a4d5cb8..42924d9 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": 125,
+	"version": 126,
 	"iconUrl": "./youtube.png",
 	"id": "35ae969a-a7db-11ed-afa1-0242ac120002",
 
diff --git a/YoutubeScript.js b/YoutubeScript.js
index a8421fc..ceef8ad 100644
--- a/YoutubeScript.js
+++ b/YoutubeScript.js
@@ -145,13 +145,10 @@ source.enable = (conf, settings, saveStateStr) => {
         const batchResp = batchReq.execute();
 
 		console.log("batchResp", batchResp);
-		if (!batchResp[0].isOk) {
-			if (batchResp[0].code === 429 && batchResp[0].body.includes("captcha")) {
-				throw new CaptchaRequiredException(batchResp[0].url, batchResp[0].body);
-			}
-
+		throwIfCaptcha(batchResp[0])
+		if (!batchResp[0].isOk)
 			throw new ScriptException("Failed to request context enable !batchResp[0].isOk");
-		}
+
         if(isLoggedIn && !batchResp[1].isOk) throw new ScriptException("Failed to request context enable isLoggedIn && !batchResp[1].isOk");
 
         _clientContext = getClientConfig(batchResp[0].body)//requestClientConfig(false);
@@ -332,6 +329,7 @@ source.getContentDetails = (url, useAuth) => {
 		batch.GET(URL_YOUTUBE_DISLIKES + videoId, {}, false);
 	const resps = batch.execute();
 
+    throwIfCaptcha(resps[0]);
 	if(!resps[0].isOk)
 		throw new ScriptException("Failed to request page [" + resps[0].code + "]");
 
@@ -990,6 +988,13 @@ source.getUserSubscriptions = function() {
 
 //#endregion
 
+function throwIfCaptcha(resp) {
+    if (resp != null && resp.code === 429 && resp.body != null && resp.body.includes("captcha")) {
+        throw new CaptchaRequiredException(resp.url, resp.body);
+    }
+}
+
+
 function extractVideoIDFromUrl(url) {
 	let match = url.match(REGEX_VIDEO_URL_DESKTOP);
 	if(match)
@@ -1564,6 +1569,7 @@ function requestInitialData(url, useMobile = false, useAuth = false) {
 		headers["User-Agent"] = USER_AGENT_TABLET;
 
 	const resp = http.GET(url, headers, useAuth);
+	throwIfCaptcha(resp);
 	if(resp.isOk) {
 		const html = resp.body;
 		const initialData = getInitialData(html);
-- 
GitLab