Ssieth / Google Doc, go to end

// ==UserScript==
// @name         Google Doc, go to end
// @namespace    http://ssieth.co.uk/googledoc-go-to-end
// @version      0.1
// @description  Go to end of google doc automatically when loading
// @author       Ssieth
// @match        https://docs.google.com/document/d/*
// @grant        none
// @copyright    2020, Ssieth (https://openuserjs.org/users/Ssieth)
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const scrollClass = "kix-appview-editor";
    var fired = false;
    var lastSize = 0;
    var tick;

    function doTick() {
        console.log("------- doTick -------");
        var ele;
        var editors = document.getElementsByClassName(scrollClass);
        if (editors.length > 0) {
            ele = editors[0];
        } else {
            ele = null;
        }
        if (fired) {
            if (ele) {
                // Give it a little while to load some stuff
                fired = true;
            } else {
                console.log("------- Checked -------");
            }
        } else {
            if (ele && lastSize < ele.scrollHeight) {
                console.log("------- scrollIt -------");
                ele.scrollTo(0,ele.scrollHeight);
                lastSize = ele.scrollHeight;
            } else {
                console.log("-------- clearing: " + tick + "  -------");
                clearInterval(tick);
            }
        }
    }

    tick = setInterval(doTick, 3000);
})();