Skip to content

Instantly share code, notes, and snippets.

@rcbadiale
Last active January 30, 2021 18:45
Show Gist options
  • Select an option

  • Save rcbadiale/e65f2648f5c1744c957c74516719645d to your computer and use it in GitHub Desktop.

Select an option

Save rcbadiale/e65f2648f5c1744c957c74516719645d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GMail Attachments Check
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Alert when unwanted senders or attachments are added to the mail.
// @author rcbadiale
// @match *://mail.google.com/mail/*
// @grant none
// ==/UserScript==
// *********************************************************
// List of names / emails allowed to be used
// Subject pattern: <TEXT> - <COMPANY>
var allowlist = {
// 'COMPANY': ['EMAIL1@EMAIL.com, 'EMAIL2@EMAIL.com']
'ABC': ['test@abc.com',],
'DEF': ['test@def.com',],
}
// *********************************************************
class Email {
constructor(emails, subject, attachments) {
this.emails = emails;
this.subject = subject;
this.attachments = attachments;
}
}
(function() {
var flag = false;
var observer = new MutationObserver(function() {
// var mail_window = document.getElementsByClassName('AD');
var attachments = document.getElementsByClassName('dL');
if (attachments.length > 0 & flag === false) {
console.log('New attachment! :)');
createCheckButton();
document.getElementsByClassName('dC')[0].style.display = 'none';
flag = true;
}
else if (attachments.length === 0 & flag === true) {
console.log('No more attachments! :(');
deleteCheckButton();
document.getElementsByClassName('dC')[0].style.display = '';
flag = false;
}
})
observer.observe(document, {'childList': true, 'subtree': true});
})();
function createCheckButton() {
var bar = document.getElementsByClassName('btC')[0];
var cell = document.createElement('td');
cell.setAttribute('class', 'gU Up');
cell.id = 'user-check-button';
cell.style.paddingRight = '5px';
var check_button = document.createElement('button');
check_button.innerText = 'Check!';
check_button.setAttribute('class', 'T-I-atl');
check_button.style.backgroundColor = '#FBBC04';
check_button.style.color = 'black';
check_button.addEventListener('click', checkData);
check_button.addEventListener('mouseover', function() {
check_button.style.filter = 'contrast(120%)';
});
check_button.addEventListener('mouseout', function() {
check_button.style.filter = '';
});
cell.append(check_button);
bar.prepend(cell);
}
function deleteCheckButton() {
var cell = document.getElementById('user-check-button');
cell.remove();
}
function checkData() {
// Show send button
document.getElementsByClassName('dC')[0].style.display = '';
// Read data
var email = new Email(getEmails(), getSubject(), getAttachments());
console.log(email);
// Get status
let allow = true;
let allowed = '';
let keys = Object.keys(allowlist);
let text = 'Algo errado não está certo! :(\n'
// Define which allowlist to use
keys.forEach(function(key) {
if (email.subject.includes(' - ' + key)) allowed = key;
});
// Check attachments for unwanted names
email.attachments.forEach(function(el) {
if (!el.includes(allowed)) {
allow = false;
text += 'Anexo: ' + el + '\n'
}
console.log(el, allow)
});
// Check if emails are not on the allowlist
email.emails.forEach(function(el) {
if (!allowlist[allowed].includes(el)) {
allow = false;
text += 'Email: ' + el + '\n'
}
console.log(el, allow)
});
// Treatment
let cell = document.getElementById('user-check-button')
let button = cell.getElementsByTagName('button')[0];
if (allow) button.style.backgroundColor = '#34A853';
else {
button.style.backgroundColor = '#EA4335';
alert(text);
}
}
function getEmails() {
var emails = Array();
var el_emails = Array.from(document.getElementsByClassName('vR'));
el_emails.forEach(function(el){
emails.push(el.children[0].attributes.email.value);
});
return emails;
}
function getSubject() {
return document.getElementsByClassName('aoT')[0].value
}
function getAttachments() {
var attachments = Array();
let attachments_bar = Array.from(document.getElementsByClassName('dL'));
attachments_bar.forEach(function(element){
if (!attachments.includes(element.textContent)) {
attachments.push(element.textContent);
}
});
return attachments;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment