Google Docs Zoom Shortcut

Lets you zoom with Ctrl + Mouse Wheel on Google Docs and Google Sheets

  1. // ==UserScript==
  2. // @name Google Docs Zoom Shortcut
  3. // @version 1.0.4
  4. // @description Lets you zoom with Ctrl + Mouse Wheel on Google Docs and Google Sheets
  5. // @author Lucas Bürgy
  6. // @homepageURL https://github.com/burgyl/google-docs-zoom-shortcut
  7. // @match https://docs.google.com/document/*
  8. // @match https://docs.google.com/spreadsheets/*
  9. // @icon https://www.google.com/favicon.ico
  10. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  11. // @namespace https://greasyfork.org/users/1359841
  12. // ==/UserScript==
  13.  
  14. const ZOOM_DELTA = 10;
  15. const INPUT_SELECTOR = [
  16. "#t-zoom input.goog-toolbar-combo-button-input",
  17. "#zoomSelect input.goog-toolbar-combo-button-input",
  18. ];
  19.  
  20. async function changeZoom(delta) {
  21. var input = null;
  22. for (let i = 0; input === null && i < INPUT_SELECTOR.length; i++) {
  23. input = document.querySelector(INPUT_SELECTOR[i]);
  24. }
  25. if (input === null) {
  26. console.error('Zoom input not found');
  27. return;
  28. }
  29.  
  30. const sanitizedValue = Number(input.value.replace(/%/, ""));
  31. const newValue = sanitizedValue + delta;
  32. input.value = (newValue < 0 ? 0 : newValue) + '%';
  33.  
  34. await input.focus();
  35.  
  36. input.dispatchEvent( new KeyboardEvent('keypress', {
  37. keyCode: 13,
  38. }));
  39. }
  40.  
  41. (function () {
  42. window.addEventListener("wheel", function (event) {
  43. if (event.ctrlKey) {
  44. event.preventDefault();
  45. if (event.deltaY < 0) {
  46. changeZoom(ZOOM_DELTA);
  47. } else {
  48. changeZoom(-ZOOM_DELTA);
  49. }
  50. }
  51. }, { passive: false });
  52. })();