Fix Gmail Email Search

Improved version of the old "Emails" quick search functionality in Gmail, one click to view all emails you've sent or received from any address

  1. // ==UserScript==
  2. // @name Fix Gmail Email Search
  3. // @namespace https://github.com/gserafini/fix-gmail-email-search-userscript/
  4. // @version 1.4.5
  5. // @description Improved version of the old "Emails" quick search functionality in Gmail, one click to view all emails you've sent or received from any address
  6. // @author Gabriel Serafini
  7. // @license MIT
  8. // @donate If you like this, PayPal a little love to gserafini@gmail.com (or https://paypal.me/SerafiniStudios )
  9. // @screenshot https://raw.githubusercontent.com/gserafini/fix-gmail-email-search-userscript/master/fix-gmail-email-search-screenshot.png
  10. // @icon https://raw.githubusercontent.com/gserafini/fix-gmail-email-search-userscript/master/SearchEmails_icon.png
  11. // @match *://mail.google.com/*
  12. // @match *://contacts.google.com/widget/hovercard/*
  13. // @match *://gmail.com/*
  14. // @match *gmail.com/*
  15. // @match *mail.google.com/*
  16. // @match *contacts.google.com/widget/hovercard/*
  17. // @grant none
  18. // @require https://code.jquery.com/jquery-3.5.1.min.js#sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=
  19. // ==/UserScript==
  20.  
  21. var $ = window.jQuery;
  22.  
  23. (function() {
  24. 'use strict';
  25.  
  26. $(function() {
  27. // Handler for .ready() called.
  28.  
  29. $('body').prepend('<style>.email_search_icon {outline: none; ' +
  30. 'position: relative; z-index: 0; padding: 0 4px; margin: 0; top: 2px;} ' +
  31. '.email_search_icon svg {fill: #600;} .email_search_icon svg:hover {fill: #f00;} ' +
  32. '.email_search_icon {cursor: pointer;} </style>');
  33.  
  34. function get_email_search_icon_link(email_address) {
  35.  
  36. var icon = '<span style=""' +
  37. 'class="email_search_icon" title="Click to search for all emails with ' + email_address + '" email="' + email_address + '">' +
  38. '<svg focusable="false" height="1em" viewBox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg">' +
  39. '<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 ' +
  40. '4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></span>';
  41.  
  42. return icon;
  43. }
  44.  
  45. function insert_email_search_icon() {
  46.  
  47. // This may change over time, so will need to update it periodically most likely
  48. $('.gD').not('.email_search_icon+.gD')
  49. .each(
  50. function (index) {
  51. // console.log("Found email for possible searching: " + $(this).attr('email'));
  52. $(this)
  53. .before(get_email_search_icon_link($(this).attr('email')))
  54. }
  55. );
  56.  
  57. // Find all emails in pop-up contact cards and also inside emails
  58. $('a[href^="mailto:"]').not('.email_search_icon+a[href^="mailto:"]').not('div[contenteditable="true"] a[href^="mailto:"]')
  59. .each(
  60. function (index) {
  61. var email_address = $(this).attr('href').replace("mailto:","");
  62. // console.log("Found email for possible searching: " + email_address);
  63. $(this)
  64. .before(get_email_search_icon_link(email_address));
  65. }
  66. );
  67. }
  68.  
  69. // Poll periodically for newly created divs
  70. setInterval(insert_email_search_icon, 300);
  71.  
  72. $('body').on('click', '.email_search_icon', function() {
  73. event.stopPropagation();
  74.  
  75. if (window.self==window.top) {
  76. $('input[name="q"]').val($(this).attr('email'));
  77. $('button[aria-label="Search mail"]').click();
  78. // console.log("Search icon clicked! Searching for " + $(this).attr('email') + "...");
  79. }
  80. else {
  81. // console.log("Search email for " + $(this).attr('email') + " link clicked in hovercard");
  82. window.parent.postMessage("searchForEmail="+$(this).attr('email'),"*");
  83. }
  84.  
  85. });
  86.  
  87. if (window.self==window.top) {
  88. $(window).on("message onmessage", function(e) {
  89. var event = e.originalEvent;
  90. if (event.origin === "https://contacts.google.com" && event.data.indexOf("searchForEmail=") != -1) {
  91.  
  92. // console.log(event.data);
  93. var email_address = event.data;
  94. email_address = email_address.replace("searchForEmail=","");
  95. // console.log("Found email to search for: " + email_address);
  96.  
  97. $('input[name="q"]').val(email_address);
  98. $('button[aria-label="Search mail"]').click();
  99. // console.log("Search icon clicked in hovercard! Searching for " + email_address + "...");
  100. }
  101. });
  102. }
  103.  
  104. });
  105.  
  106. })();