// ==UserScript== // @name 网易邮箱去除广告 // @namespace 126 Mail AdRemover // @version 2021.01.27.1 // @description 126、163和yeah邮箱去除顶部"应用中心"、"网易严选"和"半个电台",去除登陆页与首页广告 // @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.yeah.net/js6/main.jsp* // @include http*://mail.126.com/ // @include http*://mail.163.com/ // @include http*://mail.yeah.net/ // @include http*://mail.126.com/index.htm* // @include http*://mail.163.com/index.htm* // @include http*://mail.yeah.net/index.htm* // @include http*://mail.126.com/?* // @include http*://mail.163.com/?* // @include http*://mail.yeah.net/?* // @include http*://mail.126.com/#* // @include http*://mail.163.com/#* // @include http*://mail.yeah.net/#* // @grant none // @downloadURL none // ==/UserScript== (function () { /** DoLog相关函数取自 Ocrosoft 的 Pixiv Previewer * [GitHub] Ocrosoft: https://github.com/Ocrosoft/ * [GreasyFork] Ocrosoft: https://greasyfork.org/zh-CN/users/63073 * [GreasyFork] Pixiv Previewer: https://greasyfork.org/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|yeah)\.(com|net)\/(index.htm)?(\?.*)?(#.*)?/); 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); (function waitAdsRemove() { if (waitAdsRemove.findCount === undefined) {waitAdsRemove.findCount = 0;}; if (!adsMain.style.backgroundImage.includes('url')) { waitAdsRemove.findCount++; DoLog(waitAdsRemove.findCount > 14 ? LogLevel.Warning : LogLevel.Info, 'adsImage not loaded, keep waiting... Tried for ' + String(waitAdsRemove.findCount) + 'times. '); if (waitAdsRemove.findCount > 40) { DoLog(LogLevel.Error, 'Cannot find ads from backgroundImage. Stop finding now. Tried for ' + String(waitAdsRemove.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'); } // 去除广告标识 (function waitTagRemove() { if (waitTagRemove.adtagFindCount === undefined) {waitTagRemove.adtagFindCount = 0;}; if (waitTagRemove.themeCtrlFindCount === undefined) {waitTagRemove.themeCtrlFindCount = 0;}; if (waitTagRemove.adtagRemoved === undefined) {waitTagRemove.adtagRemoved = false;}; if (waitTagRemove.themeCtrlRemoved === undefined) {waitTagRemove.themeCtrlRemoved = false;}; // 广告标识 if (!waitTagRemove.adtagRemoved) { const adtag = document.querySelector('#adtag'); if (!adtag) { waitTagRemove.adtagFindCount++; DoLog(waitTagRemove.adtagFindCount > 14 ? LogLevel.Warning : LogLevel.Info, 'adTag not loaded, keep waiting... Tried for ' + String(waitTagRemove.adtagFindCount) + 'times. '); } else { adtag.parentElement.removeChild(adtag); waitTagRemove.adtagRemoved = true; } } // 广告翻页键 if (!waitTagRemove.themeCtrlRemoved) { const themeCtrl = document.querySelector('.themeCtrl'); if (!themeCtrl) { waitTagRemove.themeCtrlFindCount++; DoLog(waitTagRemove.themeCtrlFindCount > 14 ? LogLevel.Warning : LogLevel.Info, 'themeCtrl not loaded, keep waiting... Tried for ' + String(waitTagRemove.themeCtrlFindCount) + 'times. '); } else { themeCtrl.parentElement.removeChild(themeCtrl); waitTagRemove.themeCtrlRemoved = true; } } // 如果还没有去除干净,500毫秒后再调用 if (!waitTagRemove.adtagRemoved || !waitTagRemove.themeCtrlRemoved) {setTimeout(waitTagRemove, 500);}; }) () // 登陆板块居中显示(这里有显示bug,以后再找。网易的页面布局让我头痛。。) let loginBlock = document.querySelector('#loginBlock'); (function loginBlockCenter() { window.addEventListener('resize', loginBlockCenter); loginBlock.style.left = String(Math.round((Number(getComputedStyle(document.lastChild).width.replace('px', '')) - Number(getComputedStyle(loginBlock).width.replaceAll('px', ''))) / 2)) + 'px'; }) () 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'); })()