NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name Gmail From/Domain Search Shortcuts
// @description Ctrl+Shift+F / Ctrl+Shift+G shortcuts to search by sender or sender's domain
// @author Rami S Ejailat
// @copyright RamiEjailat (https://openuserjs.org/users/RamiEjailat)
// @updateURL https://openuserjs.org/meta/RamiEjailat/Gmail_FromDomain_Search_Shortcuts.meta.js
// @downloadURL https://openuserjs.org/install/RamiEjailat/Gmail_FromDomain_Search_Shortcuts.user.js
// @license MIT
// @version 20251122
// @namespace http://tampermonkey.net/
// @match https://mail.google.com/*
// @grant none
// @icon https://ssl.gstatic.com/images/branding/product/2x/hh_gmail_16dp.png
// @run-at document-start
// ==/UserScript==
/* jshint esversion: 6 */
(function() {
'use strict';
function findSingleSelectedEmail() {
// List view: only proceed if exactly one email is selected
const selectedRows = document.querySelectorAll('[role="main"] tr[aria-selected="true"]');
if (selectedRows.length === 1) {
const emailEl = selectedRows[0].querySelector('.yX .gD');
if (emailEl) return emailEl.getAttribute('email');
}
// Open conversation view: check for a single visible message
const openMsgEl = document.querySelector('.ii.gt .gD, .adn .gD');
if (openMsgEl) return openMsgEl.getAttribute('email');
// Otherwise, don't act (multiple selected or none)
return null;
}
function getBaseDomain(domain) {
if (!domain) return null;
const parts = domain.toLowerCase().split('.');
return parts.length > 1 ? parts.slice(-2).join('.') : domain;
}
function doSearch(query) {
const url = `/mail/u/0/#search/${encodeURIComponent(query)}`;
window.location.href = url;
}
function searchFromSender() {
const email = findSingleSelectedEmail();
if (email) doSearch(`from:${email}`);
}
function searchFromDomain() {
const email = findSingleSelectedEmail();
if (!email) return;
const parts = email.split('@');
if (parts.length < 2) return;
const baseDomain = getBaseDomain(parts[1]);
if (baseDomain) doSearch(`from:*@*${baseDomain}`);
}
document.addEventListener('keydown', (e) => {
// Check for Ctrl or Command key pressed
const isCtrlCmd = e.ctrlKey || e.metaKey;
if (isCtrlCmd && e.shiftKey && e.key.toLowerCase() === 'f') {
e.preventDefault();
searchFromSender();
}
if (isCtrlCmd && e.shiftKey && e.key.toLowerCase() === 'g') {
e.preventDefault();
searchFromDomain();
}
}, true);
})();