diff --git a/src/adblock.js b/src/adblock.js index 6176d2c5..572cbe64 100644 --- a/src/adblock.js +++ b/src/adblock.js @@ -22,29 +22,41 @@ JSON.parse = function () { r.adPlacements = []; } - const sectionListRenderer = + // remove ads from home + const homeSectionListRenderer = r?.contents?.tvBrowseRenderer?.content?.tvSurfaceContentRenderer?.content ?.sectionListRenderer; - if (sectionListRenderer?.contents) { + if (homeSectionListRenderer?.contents) { // Drop the full width ad card, usually appears at the top of the page - sectionListRenderer.contents = sectionListRenderer.contents.filter( + homeSectionListRenderer.contents = homeSectionListRenderer.contents.filter( (elm) => !elm.tvMastheadRenderer ); // Drop ad tile from the horizontal shelf - // Target to remove: - // contents.tvBrowseRenderer.content.tvSurfaceContentRenderer.content.sectionListRenderer.contents[0].shelfRenderer.content.horizontalListRenderer.items[0].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 - ); - }); + removeAdSlotRenderer(homeSectionListRenderer); + } + + // remove ad tile from search + const searchSectionListRenderer = r?.contents?.sectionListRenderer; + if (searchSectionListRenderer?.contents) { + removeAdSlotRenderer(searchSectionListRenderer); } return r; }; + +// Drop ad tile from the horizontal shelf +// Target to remove: +// sectionListRenderer.contents[0].shelfRenderer.content.horizontalListRenderer.items[0].adSlotRenderer +function removeAdSlotRenderer(sectionListRenderer) { + 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 + ); + }); +}