Add Esc key on Google Maps

Add Esc key on Google Maps for better UX.

  1. // ==UserScript==
  2. // @name Add Esc key on Google Maps
  3. // @namespace https://github.com/gslin/add-esc-key-on-google-maps
  4. // @match https://www.google.com/maps*
  5. // @grant none
  6. // @version 0.20220228.0
  7. // @author Gea-Suan Lin <gslin@gslin.org>
  8. // @description Add Esc key on Google Maps for better UX.
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (() => {
  13. 'use strict';
  14.  
  15. window.addEventListener('keydown', ev => {
  16. // Esc only
  17. if (27 !== ev.keyCode) {
  18. return;
  19. }
  20. console.debug('Application "Add Esc key on Google Maps" esc key detected.');
  21.  
  22. // User page.
  23. let el = document.querySelector('button[aria-label="Close"], button[aria-label="關閉"]');
  24. if (el && el.offsetHeight > 0 && el.offsetWidth > 0) {
  25. el.click();
  26. return;
  27. }
  28.  
  29. // Comment page.
  30. el = document.querySelector('button[aria-label="Back"], button[aria-label="返回"]');
  31. if (el && el.offsetHeight > 0 && el.offsetWidth > 0) {
  32. el.click();
  33. return;
  34. }
  35.  
  36. // Store page.
  37. el = document.querySelector('a[aria-label="Clear search"], a[guidedhelpid="clear_search"]');
  38. if (el && el.offsetHeight > 0 && el.offsetWidth > 0) {
  39. el.click();
  40. return;
  41. }
  42. }, true);
  43.  
  44. console.debug('Application "Add Esc key on Google Maps" installed.');
  45. })();