Outlook live ad-remover

Remove ad's from mirosofts web mail services

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Outlook live ad-remover
// @namespace    http://monkeyr.com/
// @version      1.6.1
// @description  Remove ad's from mirosofts web mail services
// @author       mh
// @license      MIT
// @match        https://outlook.live.com/owa/*
// @match        https://outlook.live.com/mail/*
// @icon         https://outlook.live.com/favicon.ico
// @grant        none
// ==/UserScript==

const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
let adPanel = false;
let o365Button = false;
let emailAd = false;
let emailAdSeenEles = [];
if (MutationObserver) console.log('Outlook live ad-remover is enabled.');
window.addEventListener('resize', function() {adPanel = false; o365Button = false; emailAd = false; console.log('resize event')});
const observer = new MutationObserver(MutationRecords => {
    MutationRecords.forEach(function(MutationRecord){
        let msFZs = {};
        let emailAdEles;
        let target = MutationRecord.target;
        // Find and remove the right/lower ad panel
        if(!adPanel && target.querySelectorAll('[aria-label="advertisement"]').length && target.tagName === 'DIV'){
            target.remove();
            //adPanel = true;
            console.log('adPanel removal', target, parent);
        }
        // find and remove the upgrade to o365 button
        else if(!o365Button && (msFZs = target.querySelectorAll('.ms-FocusZone')) && msFZs.length){
            msFZs.forEach(msFZ => {
                let nextSibling = msFZ.nextSibling;
                if(nextSibling && nextSibling.firstChild && nextSibling.firstChild.tagName == 'BUTTON' ){
                    nextSibling.remove();
                    o365Button = true;
                    console.log('o365Button removal', target, MutationRecord, nextSibling, nextSibling.childElementCount);
                    return false;
                }
            });
        }
        // hide the ad from the top of the inbox, removing it will sometimes crash the outlook "app"
        if(!emailAd && (emailAdEles = target.querySelectorAll('[role="listbox"] .customScrollBar > div > div')) && emailAdEles.length && target.tagName === 'DIV'){
            emailAdEles.forEach(ele => {
                if(!ele.querySelector('[draggable]')){
                    if(ele.style.display != 'none'){
                        ele.style.display = 'none';
                        console.log('owaContainer removal', ele);
                        //emailAd = true;
                    }
                    return false;
                }
                //console.log(itm, itm.querySelector('[draggable]'))
            });
        }
    });
    //console.log(MutationRecords);
});
observer.observe(document.body, {childList: true, subtree: true});