Google Docs Zoom Shortcut

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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 });
})();