From 392afb0a2f3823eb04e6b1f0cae13071cca1bd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Czekan=CC=81ski?= Date: Wed, 18 Sep 2024 13:38:42 +0200 Subject: [PATCH] shorts: Remove shorts from Subscriptions tab --- src/config.js | 1 + src/shorts.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/ui.js | 1 + src/userScript.js | 1 + 4 files changed, 47 insertions(+) create mode 100644 src/shorts.js diff --git a/src/config.js b/src/config.js index 1a7fd149..fb8ed829 100644 --- a/src/config.js +++ b/src/config.js @@ -2,6 +2,7 @@ const CONFIG_KEY = 'ytaf-configuration'; const configOptions = new Map([ ['enableAdBlock', { default: true, desc: 'Enable ad blocking' }], + ['removeShorts', { default: true, desc: 'Remove Shorts from subscriptions' }], ['enableSponsorBlock', { default: true, desc: 'Enable SponsorBlock' }], [ 'enableSponsorBlockSponsor', diff --git a/src/shorts.js b/src/shorts.js new file mode 100644 index 00000000..1bdb2c68 --- /dev/null +++ b/src/shorts.js @@ -0,0 +1,44 @@ +/* eslint no-redeclare: 0 */ +/* global fetch:writable */ +import { configRead } from './config'; + +const origParse = JSON.parse; +JSON.parse = function () { + const r = origParse.apply(this, arguments); + if (!configRead('removeShorts')) { + return r; + } + + // First page of subscriptions tab + const gridRenderer = findFirstObject(r, 'gridRenderer'); + if (gridRenderer?.items) { + removeShorts(gridRenderer); + } + + // Pagination + const gridContinuation = findFirstObject(r, 'gridContinuation'); + if (gridContinuation?.items) { + removeShorts(gridContinuation); + } + + return r; +}; + +function removeShorts(container) { + container.items = container.items.filter( + (elm) => elm?.tileRenderer?.onSelectCommand?.reelWatchEndpoint == null + ); +} + +function findFirstObject(haystack, needle) { + for (const key in haystack) { + if (key === needle) { + return haystack[key]; + } + if (typeof haystack[key] === 'object') { + const result = findFirstObject(haystack[key], needle); + if (result) return result; + } + } + return null; +} diff --git a/src/ui.js b/src/ui.js index 1779406e..0cb85d43 100644 --- a/src/ui.js +++ b/src/ui.js @@ -121,6 +121,7 @@ function createOptionsPanel() { elmContainer.appendChild(createConfigCheckbox('enableAdBlock')); elmContainer.appendChild(createConfigCheckbox('hideLogo')); + elmContainer.appendChild(createConfigCheckbox('removeShorts')); elmContainer.appendChild(createConfigCheckbox('enableSponsorBlock')); const elmBlock = document.createElement('blockquote'); diff --git a/src/userScript.js b/src/userScript.js index cd550219..df0f41c4 100644 --- a/src/userScript.js +++ b/src/userScript.js @@ -13,6 +13,7 @@ document.addEventListener( ); import './adblock.js'; +import './shorts.js'; import './sponsorblock.js'; import './ui.js';