CarGurus Sponsored Remover

Removes sponsored content from CarGurus search results.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         CarGurus Sponsored Remover
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Removes sponsored content from CarGurus search results.
// @match        https://www.cargurus.ca/*
// @match        https://www.cargurus.com/*
// @match        https://www.cargurus.co.uk/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=cargurus.ca
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to scan and remove sponsored tiles
    function removeSponsoredListings() {
        // The specific element that contains the "Sponsored" text
        const sponsoredIndicators = document.querySelectorAll('[data-testid="sponsored-text"]');

        sponsoredIndicators.forEach(indicator => {
            const textContent = indicator.textContent.trim().toLowerCase();

            // Check if it actually says "Sponsored" (sometimes this element exists but is empty/whitespace)
            if (textContent.includes('sponsored')) {
                // We need to remove the main container for this specific car.
                // Based on the HTML structure: p -> div -> div -> div(class*="_tileFrame")
                // We use .closest() to find the nearest parent wrapper that acts as the tile frame.
                // We use a partial selector for "tileFrame" in case the random hash suffix changes in updates.
                const tile = indicator.closest('div[class*="_tileFrame"]');

                if (tile) {
                    tile.style.display = 'none'; // Hide it visually
                    // Optional: tile.remove(); // Completely remove from DOM
                    console.log('CarGurus Sponsored Remover: Hid a sponsored listing.');
                }
            }
        });
    }

    // Run the removal function immediately on load
    removeSponsoredListings();

    // Set up a MutationObserver to catch listings loaded dynamically (infinite scroll/pagination)
    const observer = new MutationObserver((mutations) => {
        let shouldRun = false;
        for (const mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                shouldRun = true;
                break;
            }
        }
        if (shouldRun) {
            removeSponsoredListings();
        }
    });

    // Start observing the document body for changes
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

})();