Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Gmail Protected Recipients
- // @namespace local.gmail.protected-recipients
- // @version 1.3
- // @description Warn before emailing protected domains in Gmail
- // @match https://mail.google.com/*
- // @run-at document-start
- // ==/UserScript==
- (() => {
- 'use strict';
- const PROTECTED_DOMAINS = [
- 'example.com'
- ];
- const LOG_PREFIX = '[Gmail Protected Recipients]';
- function log(...args) {
- console.log(LOG_PREFIX, ...args);
- }
- function isProtectedEmail(email) {
- const normalized = email.trim().toLowerCase();
- return PROTECTED_DOMAINS.some(domain =>
- normalized.endsWith(`@${domain.toLowerCase()}`)
- );
- }
- function extractEmails(root) {
- const found = new Set();
- if (!root) return [];
- const emailRegex =
- /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/gi;
- root.querySelectorAll('*').forEach(el => {
- const attrs = [
- 'email',
- 'data-hovercard-id',
- 'data-email',
- 'aria-label',
- 'title',
- 'name',
- ];
- for (const attr of attrs) {
- const value = el.getAttribute?.(attr);
- if (!value) continue;
- value.match(emailRegex)?.forEach(email => {
- found.add(email.toLowerCase());
- });
- }
- if (
- el instanceof HTMLInputElement ||
- el instanceof HTMLTextAreaElement
- ) {
- el.value.match(emailRegex)?.forEach(email => {
- found.add(email.toLowerCase());
- });
- }
- });
- return [...found];
- }
- function findComposeArea(sendButton) {
- let node = sendButton;
- while (node && node !== document.body) {
- const emails = extractEmails(node);
- log(
- 'Checking ancestor:',
- node,
- 'emails:',
- emails
- );
- if (emails.length > 0) {
- log('Using compose area:', node);
- return node;
- }
- node = node.parentElement;
- }
- log('Could not find recipient-containing ancestor');
- return null;
- }
- function getProtectedRecipients(root) {
- const emails = extractEmails(root);
- log('Detected emails:', emails);
- const protectedRecipients =
- emails.filter(isProtectedEmail);
- log(
- 'Protected recipients:',
- protectedRecipients
- );
- return protectedRecipients;
- }
- function confirmProtectedSend(root) {
- const recipients =
- getProtectedRecipients(root);
- if (!recipients.length) {
- return true;
- }
- return window.confirm(
- '⚠️ WARNING: PROTECTED RECIPIENT ⚠️\n\n' +
- 'You are about to send email to:\n\n' +
- recipients.map(x => ` ${x}`).join('\n') +
- '\n\nAre you ABSOLUTELY SURE?'
- );
- }
- function getSendButton(element) {
- const button = element.closest(
- '[role="button"], button, [data-tooltip], [aria-label]'
- );
- if (!button) return null;
- const labels = [
- button.textContent,
- button.getAttribute('data-tooltip'),
- button.getAttribute('aria-label'),
- button.getAttribute('title'),
- ].filter(Boolean);
- const isSend = labels.some(value =>
- /^\s*send(?:\s|$)/i.test(value)
- );
- return isSend ? button : null;
- }
- document.addEventListener(
- 'click',
- event => {
- if (!(event.target instanceof Element)) return;
- const sendButton =
- getSendButton(event.target);
- if (!sendButton) return;
- log('Send clicked:', sendButton);
- const composeArea =
- findComposeArea(sendButton);
- if (!composeArea) {
- log(
- 'No compose area found — allowing send'
- );
- return;
- }
- if (!confirmProtectedSend(composeArea)) {
- log('Send cancelled');
- event.preventDefault();
- event.stopPropagation();
- event.stopImmediatePropagation();
- }
- },
- true
- );
- document.addEventListener(
- 'keydown',
- event => {
- if (
- event.key !== 'Enter' ||
- (!event.metaKey && !event.ctrlKey)
- ) {
- return;
- }
- if (!(event.target instanceof Element)) {
- return;
- }
- log('Cmd/Ctrl+Enter detected');
- // For keyboard sends, start walking upward
- // from the currently focused compose element.
- let node = event.target;
- let composeArea = null;
- while (node && node !== document.body) {
- const emails = extractEmails(node);
- if (emails.length > 0) {
- composeArea = node;
- break;
- }
- node = node.parentElement;
- }
- if (!composeArea) {
- log(
- 'Could not find compose area for keyboard send'
- );
- return;
- }
- if (!confirmProtectedSend(composeArea)) {
- log('Keyboard send cancelled');
- event.preventDefault();
- event.stopPropagation();
- event.stopImmediatePropagation();
- }
- },
- true
- );
- log('Userscript loaded');
- })();
Advertisement
Add Comment
Please, Sign In to add comment