Skip to content

Commit

Permalink
Merge pull request #201 from JaCzekanski/shorts-block
Browse files Browse the repository at this point in the history
Remove shorts from Subscriptions tab
  • Loading branch information
throwaway96 authored Oct 18, 2024
2 parents cacdf6c + 392afb0 commit 0fc3aff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
44 changes: 44 additions & 0 deletions src/shorts.js
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions src/userScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ document.addEventListener(
);

import './adblock.js';
import './shorts.js';
import './sponsorblock.js';
import './ui.js';

Expand Down

0 comments on commit 0fc3aff

Please sign in to comment.