Dynamically swap sender avatar in Gmail compose by current "Send As" FROM address, with full debugging

2 min read Original article ↗

Dynamically swap sender avatar in Gmail compose by current "Send As" FROM address, with full debugging

// ==UserScript==
// @name Gmail Send-As Avatar Swapper (Multicompse, Debug)
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Dynamically swap sender avatar in Gmail compose by current "Send As" FROM address, with full debugging
// @match https://mail.google.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function updateAvatars() {
const avatarMap = {
"xxxxxx@gmail.com": "https://www.yourdomain.com/xxxxxx.png",
"yyyyyy@gmail.com": "https://www.yourdomain.com/yyyyyy.png"
};
document.querySelectorAll('form.bAs').forEach(form => {
const fromInput = form.querySelector('input[name="from"]');
if (!fromInput) return;
const currentFrom = fromInput.value;
const avatarUrl = avatarMap[currentFrom];
if (!avatarUrl) return;
// Try all likely containers (form, parent, grandparent, siblings)
const possibleContainers = [form, form.parentElement, form.parentElement?.parentElement];
let senderAvatar = null;
for (const container of possibleContainers) {
if (!container) continue;
senderAvatar = container.querySelector('img.ajn[jid]');
if (senderAvatar) break;
}
if (!senderAvatar) {
console.log('[AvatarSwap] Sender avatar not found in any container for From:', currentFrom);
return;
}
if (senderAvatar.src !== avatarUrl) {
console.log('[AvatarSwap] Updating avatar src:', senderAvatar.src, '->', avatarUrl);
senderAvatar.src = avatarUrl;
}
});
}
// MutationObserver + polling ensures robustness
const observer = new MutationObserver((mutations) => {
console.log('[AvatarSwap] DOM mutation detected');
updateAvatars();
});
observer.observe(document.body, { childList: true, subtree: true });
// Fallback timed polling (handles edge cases or missed mutations)
setInterval(() => {
console.log('[AvatarSwap] Timed poll running updateAvatars...');
updateAvatars();
}, 1500);
// Initial run on script injection
updateAvatars();
})();