Google Docs Zoom Shortcut

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

目前為 2024-08-29 提交的版本,檢視 最新版本

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