Greasy Fork

Greasy Fork is available in English.

网易邮箱去除广告

126和163邮箱去除顶部"应用中心"、"网易严选"和"半个电台",去除登陆页与首页广告

目前为 2021-01-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         网易邮箱去除广告
// @namespace    126 Mail AdRemover
// @version      2021.01.17.1
// @description  126和163邮箱去除顶部"应用中心"、"网易严选"和"半个电台",去除登陆页与首页广告
// @author       PY-DNG
// @icon         https://mail.126.com/favicon.ico
// @include      http*://mail.126.com/js6/main.jsp*
// @include      http*://mail.163.com/js6/main.jsp*
// @include      http*://mail.126.com/
// @include      http*://mail.163.com/
// @include      http*://mail.126.com/?*
// @include      http*://mail.163.com/?*
// @grant        none
// ==/UserScript==

(function () {
    /** DoLog相关函数取自 Ocrosoft 的 Pixiv Previewer
     *  [GitHub]     Ocrosoft: https://github.com/Ocrosoft/
     *  [GreasyFork] Ocrosoft: http://greasyfork.icu/zh-CN/users/63073
     *  [GreasyFork] Pixiv Previewer: http://greasyfork.icu/zh-CN/scripts/30766
     *  [GitHub]     Pixiv Previewer: https://github.com/Ocrosoft/PixivPreviewer
     **/
    let LogLevel = {
        None: 0,
        Error: 1,
        Warning: 2,
        Info: 3,
        Elements: 4,
    };
    let g_logCount = 0;
    let g_logLevel = LogLevel.Warning;

    function DoLog(level, msgOrElement) {
        if (level <= g_logLevel) {
            let prefix = '%c';
            let param = '';

            if (level == LogLevel.Error) {
                prefix += '[Error]';
                param = 'color:#ff0000';
            } else if (level == LogLevel.Warning) {
                prefix += '[Warning]';
                param = 'color:#ffa500';
            } else if (level == LogLevel.Info) {
                prefix += '[Info]';
                param = 'color:#888888';
            } else if (level == LogLevel.Elements) {
                prefix += 'Elements';
                param = 'color:#000000';
            }

            if (level != LogLevel.Elements) {
                console.log(prefix + msgOrElement, param);
            } else {
                console.log(msgOrElement);
            }

            if (++g_logCount > 512) {
                //console.clear();
                g_logCount = 0;
            }
        }
    }

    // 去除登陆页面广告
    const loginPageMatch = location.href.match(/https:\/\/mail\.(126|163)\.com\/(\?.*)?/);
    if (loginPageMatch && loginPageMatch[0] === location.href) {
        DoLog(LogLevel.Info, 'This is ' + loginPageMatch[1] + ' login page. ');
        const adsMain = document.querySelector('#mainCnt');
        if (adsMain) {
            DoLog(LogLevel.Info, '#mainCnt found');
            DoLog(LogLevel.Elements, adsMain);
            let findCount = 0;
            (function waitAdsRemove() {
                if (!adsMain.style.backgroundImage.includes('url')) {
                    findCount++;
                    DoLog(findCount > 14 ? LogLevel.Warning : LogLevel.Info, 'adsImage not loaded, keep waiting... Tried for ' + String(findCount) + 'times. ');
                    if (findCount > 40) {
                        DoLog(LogLevel.Error, 'Cannot find ads from backgroundImage. Stop finding now. Tried for ' + String(findCount) + 'times. ');
                        return false;
                    }
                    setTimeout(waitAdsRemove, 500);
                    return false;
                }
                DoLog(LogLevel.Info, 'adsImage loaded, remove it.');
                adsMain.style.backgroundImage = 'none';
                return true;
            })()
        } else {
            DoLog(LogLevel.Error, 'Cannot find #mainCnt');
        }
        return;
    }

    DoLog(LogLevel.Info, 'This is mail page. ');

    // 去广函数
    let removeAds = function () {
        DoLog(LogLevel.Info, 'Searching for ads...');
        let advertisement = document.getElementsByClassName('js-component-tab gWel-recommend-title nui-tabs nui-tabs-common  ')[0]
        if (advertisement) {
            DoLog(LogLevel.Info, 'Ads found. Remove it. ');
            advertisement.parentElement.parentElement.remove();
            return true;
        } else {
            DoLog(LogLevel.Info, 'No ads here. ');
            return false;
        }
    }

    // 去除顶部"应用中心"、"网易严选"和"半个电台",挂接首页自动去广函数
    let p = document.getElementById('_mail_tab_0_115'); //p - parentNote
    if (p) {
        let cs = p.children; //cs- childs:)
        let i, j = 0,
            note, targetNotes = new Array();
        for (i = 0; i < cs.length; i++) {
            if (cs[i].title === '应用中心' || cs[i].title === '网易严选' || cs[i].title === '半个电台') {
                targetNotes[j] = cs[i];
                j += 1;
            }
        }
        targetNotes.forEach(function (item, index, array) {
            p.removeChild(item);
        })
    }

    // 尝试现在就去除首页广告区域(如果在首页并且广告已经加载)
    removeAds();

    // 循环执行去广函数
    setInterval(removeAds, '1000');
})()