Greasy Fork is available in English.

淘宝广告屏蔽助手

尝试去除淘宝搜索结果中的动态加载广告以及掌柜热卖。

// ==UserScript==
// @name         淘宝广告屏蔽助手
// @namespace    http://tampermonkey.net/
// @version      1.2.3
// @description  尝试去除淘宝搜索结果中的动态加载广告以及掌柜热卖。
// @author       oldip
// @match        https://s.taobao.com/search*
// @grant        none
// @license      MIT
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const updateMarginLeft = () => {
        // 修正搜寻栏的位置和宽度
        document.querySelectorAll('.PageHeader--suggestWarp--vDSivG5').forEach(el => {
            el.style.marginLeft = 'auto';
        });
    };

    const removeAdElements = () => {
        // 移除搜寻页面内的广告,根据新的元素样式进行更新
        document.querySelectorAll('img.MainPic--mainP4pPic--NLfunBr').forEach(el => {
            let parent = el.closest('a.Card--doubleCardWrapper--L2XFE73').parentNode;
            if (parent && parent.tagName === 'DIV') {
                parent.style.display = 'none';
            }
        });

        // 移除右侧掌柜热卖的广告
        document.querySelectorAll('.RightLay--rightWrap--OxNNeu6').forEach(el => {
            el.style.display = 'none';
        });

        // 移除下方掌柜热卖的广告
        document.querySelectorAll('.BottomLay--bottomWrap--YBah2VM').forEach(el => {
            el.style.display = 'none';
        });

        // 更新页面头部样式
        updateMarginLeft();
    };

    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length) removeAdElements();
        });
    });

    const config = { childList: true, subtree: true };

    observer.observe(document.body, config);

    // 初始调用以立即移除广告和修改样式
    removeAdElements();

    // 监听窗口调整大小事件,动态更新样式
    window.addEventListener('resize', updateMarginLeft);

    // 确保在 DOM 内容加载完毕后也更新样式
    document.addEventListener('DOMContentLoaded', updateMarginLeft);
})();