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.2
  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. // @namespace https://greasyfork.org/users/1359841
  10. // ==/UserScript==
  11.  
  12. const ZOOM_DELTA = 10;
  13. const INPUT_SELECTOR = [
  14. "#t-zoom input.goog-toolbar-combo-button-input",
  15. "#zoomSelect input.goog-toolbar-combo-button-input",
  16. ];
  17.  
  18. async function changeZoom(delta) {
  19. var input = null;
  20. for (let i = 0; input === null && i < INPUT_SELECTOR.length; i++) {
  21. input = document.querySelector(INPUT_SELECTOR[i]);
  22. }
  23. if (input === null) {
  24. console.error('Zoom input not found');
  25. return;
  26. }
  27.  
  28. const sanitizedValue = Number(input.value.replace(/%/, ""));
  29. const newValue = sanitizedValue + delta;
  30. input.value = (newValue < 0 ? 0 : newValue) + '%';
  31.  
  32. await input.focus();
  33.  
  34. input.dispatchEvent( new KeyboardEvent('keypress', {
  35. keyCode: 13,
  36. }));
  37. }
  38.  
  39. (function () {
  40. window.addEventListener("wheel", function (event) {
  41. if (event.ctrlKey) {
  42. event.preventDefault();
  43. if (event.deltaY < 0) {
  44. changeZoom(ZOOM_DELTA);
  45. } else {
  46. changeZoom(-ZOOM_DELTA);
  47. }
  48. }
  49. }, { passive: false });
  50. })();