Last active
November 22, 2021 23:06
-
-
Save happiness801/7de3daca19d440d6aebe3be7e8880831 to your computer and use it in GitHub Desktop.
Remove ads, nags from Gmail, Google
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name remove-google-ads-and-nags.user.js | |
// @namespace http://onai.net/ | |
// @version 0.5 | |
// @description Remove ads, nags from Gmail, Google.com | |
// @author Kevin Gwynn | |
// @match https://mail.google.com/* | |
// @match https://www.google.com/* | |
// @match https://calendar.google.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let addMutationObserver = function() { | |
console.log('KAG: Creating mutation observer to watch for Ads...'); | |
// Select the node that will be observed for mutations | |
const adpNodes = document.getElementsByClassName('aDP'); | |
if (adpNodes.length === 0) { | |
console.log('KAG: No aDP nodes found at this time (cannot create mutation observer)'); | |
return; | |
} | |
// As of 2021-09-01, <div class="aDP"> is the main container of the messages list | |
const targetNode = adpNodes[0]; | |
// Options for the observer (which mutations to observe) | |
const config = { attributes: false, childList: true, subtree: true }; | |
// Callback function to execute when mutations are observed | |
const callback = function(mutationsList, observer) { | |
// Use traditional 'for loops' for IE 11 | |
for(const mutation of mutationsList) { | |
if (mutation.type === 'childList') { | |
let adSpans = document.getElementsByClassName('ast'); | |
if (adSpans.length === 0) return; | |
console.log('KAG: DOM structure changed; removing ' + adSpans.length + ' ad(s).'); | |
let removeNode = adSpans[0]?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement; | |
removeNode?.parentNode?.removeChild(removeNode); | |
} | |
} | |
}; | |
// Create an observer instance linked to the callback function | |
const observer = new MutationObserver(callback); | |
// Start observing the target node for configured mutations | |
observer.observe(targetNode, config); | |
// Later, you can stop observing | |
//observer.disconnect(); | |
} | |
let removeNags = function() { | |
let iframes = document.getElementsByTagName('IFRAME'); | |
for (let i = 0; i < iframes.length; i++) { | |
if (iframes[i].getAttribute('role') === 'presentation') { | |
if (iframes[i].src.includes('/callout/')) { | |
console.log('KAG: Removing Chrome nag...'); | |
iframes[i].parentNode.removeChild(iframes[i]); | |
} | |
} | |
} | |
}; | |
setTimeout(addMutationObserver, 5000); | |
setTimeout(removeNags, 400); | |
setTimeout(removeNags, 1500); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment