I collaborate on a large Google Spreadsheet and a zoom of 65% happens to be optimal for displaying all relevant content on my screen. Since manually setting the zoom every time is tedious, and since Google does not seem to offer a way of storing a given zoom for me, I am trying to use Greasemonkey and jQuery to zoom upon page load.
With the following code, I only get to the point of entering the value (waiting for the element to appear using the famous waitForKeyElements function and then changing its val), but I still need to manually hit Enter to confirm. I was so far unable to get the page to accept the entered value programmatically.
// ==UserScript==
// @name Google Table Auto Zoom
// @include https://docs.google.com/spreadsheets/d/1SjL_dgsxzj9y7E3JRBYR9gRxiM0ieMkbrGdybAstyt8/*
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @version 2020
// ==/UserScript==
var done = false;
function zoomout(jNode) {
if (done == false) {
done = true;
jNode.val("65%").focus();
}
}
waitForKeyElements ('.goog-toolbar-combo-button-input[aria-label="Zoom"]', zoomout);
I have tried numerous approaches to get the page to accept the value, e.g.:
- jNode.submit(); or jNode[0].submit(); However, since there is no wrapping form element, I was not lucky with this approach. I guess the page listens for changes using AJAX?
- All variations of simulating Enter using the various stackoverflow results on how to simulate keypresses in jQuery (see below for examples). However, the page does not react to any of them even though InspectElement shows that the element listens to keypress and keydown events, which I just cannot get to trigger.
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;
jNode.trigger(e);
or
const ke = new KeyboardEvent("keydown", {
bubbles: true, cancelable: true, keyCode: 13
});
document.body.dispatchEvent(ke); // doesnt work
jNode.dispatchEvent(ke) // doesnt work either
None of the above seems to work. Using the code provided and the public spreadsheet example therein, it should be easy to reproduce what I'm trying to do. I would be very grateful for any input!
Thank you very much, Chris