Skip to content

Commit

Permalink
Merge pull request #100 from alyyousuf7/remove-ad-tiles
Browse files Browse the repository at this point in the history
remove sponsored tiles from horizontal shelf
  • Loading branch information
throwaway96 authored Dec 3, 2023
2 parents aebb41e + ba5d771 commit 72eb528
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/adblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,56 @@ import { configRead } from './config';
const origParse = JSON.parse;
JSON.parse = function () {
const r = origParse.apply(this, arguments);
if (r.adPlacements && configRead('enableAdBlock')) {
if (!configRead('enableAdBlock')) {
return r;
}

if (r.adPlacements) {
r.adPlacements = [];
}

// Drop "masthead" ad from home screen
if (
// remove ads from home
const homeSectionListRenderer =
r?.contents?.tvBrowseRenderer?.content?.tvSurfaceContentRenderer?.content
?.sectionListRenderer?.contents &&
configRead('enableAdBlock')
) {
r.contents.tvBrowseRenderer.content.tvSurfaceContentRenderer.content.sectionListRenderer.contents =
r.contents.tvBrowseRenderer.content.tvSurfaceContentRenderer.content.sectionListRenderer.contents.filter(
(elm) => !elm.tvMastheadRenderer
);
?.sectionListRenderer;
if (homeSectionListRenderer?.contents) {
// Drop the full width ad card, usually appears at the top of the page
homeSectionListRenderer.contents = homeSectionListRenderer.contents.filter(
(elm) => !elm.tvMastheadRenderer
);

// Drop ad tile from the horizontal shelf
removeAdSlotRenderer(homeSectionListRenderer);
}

// remove ad tile from search
const searchSectionListRenderer = r?.contents?.sectionListRenderer;
if (searchSectionListRenderer?.contents) {
removeAdSlotRenderer(searchSectionListRenderer);
}

return r;
};

// Drop `adSlotRenderer`
// `adSlotRenderer` can occur as,
// - sectionListRenderer.contents[*].adSlotRenderer
// - sectionListRenderer.contents[*].shelfRenderer.content.horizontalListRenderer.items[*].adSlotRenderer
function removeAdSlotRenderer(sectionListRenderer) {
// sectionListRenderer.contents[*].adSlotRenderer
sectionListRenderer.contents = sectionListRenderer.contents.filter(
(elm) => !elm.adSlotRenderer
);

// sectionListRenderer.contents[*].shelfRenderer.content.horizontalListRenderer.items[*].adSlotRenderer
const contentsWithShelfRenderer = sectionListRenderer.contents.filter(
(elm) => elm.shelfRenderer
);
contentsWithShelfRenderer.forEach((content) => {
const horizontalRenderer =
content.shelfRenderer.content.horizontalListRenderer;
horizontalRenderer.items = horizontalRenderer.items.filter(
(elm) => !elm.adSlotRenderer
);
});
}

0 comments on commit 72eb528

Please sign in to comment.