Autotrader UK - remove ads from search results

Removes all "Ad" and "You may also like" items from autotrader.co.uk search results

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Autotrader UK - remove ads from search results
// @namespace    https://autotrader.co.uk
// @version      0.2
// @description  Removes all "Ad" and "You may also like" items from autotrader.co.uk search results
// @author       Steve Chambers
// @license      MIT
// @match        https://www.autotrader.co.uk/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=autotrader.co.uk
// ==/UserScript==
 
(function() {
    'use strict';
 
    // Function to remove the targeted <li> elements
    function removeTargetedListItems() {
        document.querySelectorAll('li').forEach(li => {
            const spanText = li.querySelector('span')?.textContent.trim();
            if (spanText === 'Ad' || spanText === 'You may also like') {
                li.remove();
                return;
            }
            
            if (li.querySelector('span[data-testid="FEATURED_LISTING"]')) {
                li.remove();
                return;
            }
            
            const adSpans = li.querySelectorAll('span');
            for (const span of adSpans) {
                if (span.textContent.trim().toLowerCase() === 'ad') {
                    li.remove();
                    return;
                }
            }
        });
    }
 
    // Run the function on page load
    removeTargetedListItems();
 
    // Observe the DOM for dynamically loaded content
    const observer = new MutationObserver(() => {
        removeTargetedListItems();
    });
 
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();