Google Docs Zoom Shortcut

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Docs Zoom Shortcut
// @version      1.0.4
// @description  Lets you zoom with Ctrl + Mouse Wheel on Google Docs and Google Sheets
// @author       Lucas Bürgy
// @homepageURL  https://github.com/burgyl/google-docs-zoom-shortcut
// @match        https://docs.google.com/document/*
// @match        https://docs.google.com/spreadsheets/*
// @icon         https://www.google.com/favicon.ico
// @license      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @namespace https://greasyfork.org/users/1359841
// ==/UserScript==

const ZOOM_DELTA = 10;
const INPUT_SELECTOR = [
  "#t-zoom input.goog-toolbar-combo-button-input",
  "#zoomSelect input.goog-toolbar-combo-button-input",
];

async function changeZoom(delta) {
  var input = null;
  for (let i = 0; input === null && i < INPUT_SELECTOR.length; i++) {
    input = document.querySelector(INPUT_SELECTOR[i]);
  }
  if (input === null) {
    console.error('Zoom input not found');
    return;
  }

  const sanitizedValue = Number(input.value.replace(/%/, ""));
  const newValue = sanitizedValue + delta;
  input.value = (newValue < 0 ? 0 : newValue) + '%';

  await input.focus();

  input.dispatchEvent( new KeyboardEvent('keypress', {
    keyCode: 13,
  }));
}

(function () {
  window.addEventListener("wheel", function (event) {
    if (event.ctrlKey) {
      event.preventDefault();
      if (event.deltaY < 0) {
        changeZoom(ZOOM_DELTA);
      } else {
        changeZoom(-ZOOM_DELTA);
      }
    }
  }, { passive: false });
})();