Skip to content

Commit

Permalink
remove ad tiles from horizontal shelf
Browse files Browse the repository at this point in the history
  • Loading branch information
alyyousuf7 authored and 0xBADEAFFE committed Nov 9, 2023
1 parent d5aea5f commit e780cbc
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions src/adblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,32 @@ const origParse = JSON.parse;
JSON.parse = function () {
const r = origParse.apply(this, arguments);
try {
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)
.map((elm) => {
// Drop recommended video tiles
if (elm?.shelfRenderer?.content?.horizontalListRenderer?.items) {
elm.shelfRenderer.content.horizontalListRenderer.items =
elm.shelfRenderer.content.horizontalListRenderer.items.filter(
(item) =>
!item?.tileRenderer?.metadata?.tileMetadataRenderer
?.lines ||
!item?.tileRenderer?.metadata?.tileMetadataRenderer
?.lines[0]?.lineRenderer?.items ||
item?.tileRenderer?.metadata?.tileMetadataRenderer?.lines[0]
?.lineRenderer?.items[0]?.lineItemRenderer?.badge
?.metadataBadgeRenderer?.style !== 'BADGE_STYLE_TYPE_AD'
);
}

return elm;
});
?.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);
}
} catch (err) {
console.warn(
Expand All @@ -56,3 +51,26 @@ JSON.parse = function () {

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 e780cbc

Please sign in to comment.