minimeh / SF Chronicle in Full Screen

// ==UserScript==
// @name         SF Chronicle in Full Screen
// @namespace    com.minimeh.www
// @version      0.6
// @description  Open the Chronicle in full screen mode by pressing the space-bar for fit-to-width, or shift+space-bar for fit-to-height
// @author       minimeh
// @copyright    2021, minimeh (https://openuserjs.org/users/minimeh)
// @license      GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @supportURL   mailto://minimeh@outlook.com
// @match        https://digital.olivesoftware.com/olive/odn/sanfranciscochronicle/default.aspx*
// @icon         https://www.google.com/s2/favicons?domain=sfchronicle.com
// @grant        none
// @run-at       document-idle
// ==/UserScript==

var interval = 1000;

// ------ Editable element types to ignore hotkeys when focused
var inputTypes = [
    "text",
    "password",
    "number",
    "email",
    "tel",
    "url",
    "search",
    "date",
    "datetime",
    "datetime-local",
    "time",
    "month",
    "week"
];

// Checks the focused element of the page to see if it is an
// editable element. Hotkeys will not be in effect when the
// focused element is editable.
function isEditableTextBox(element) {
    'use strict';
    // the following returns false inappropriately and us unreliable
    // if(!element.isContentEditable) return false;
    var tagName = element.tagName.toLowerCase();
    if (tagName === 'textarea') return true;
    if (tagName !== 'input') return false;
    var type = element.getAttribute('type').toLowerCase();
    return inputTypes.indexOf(type) >= 0;
}

function fitToScreen(fitTo) {
    var elem = document.getElementsByClassName(fitTo)[0];
    if(elem){
        elem.click();
    }
}

function closePopUps() {
    //console.log("close popups");
    // Close the various popup windows hogging screen space
    var elems = document.getElementsByClassName('close_button');
    if(elems){
        for (var itemUl = 0; itemUl < elems.length; ++itemUl) {
            elems[itemUl].click();
        }
    }
}

// Note: the full screen api requires a user interaction to function. Therefore, there
// is no way to automate going to fullscreen beyond assigning a hot key. :-(
function goFullScreen(fitTo){
    // Close the various popup windows hogging screen space
    closePopUps();
    // Set the specified fit to screen
    fitToScreen(fitTo);
    // View in fullscreen
    var elem = document.getElementsByClassName('fullScreen')[0];
    if(elem){
        elem.click();
    }
}

function key_event(event){
    'use strict';
    // console.log("keydown: " + event.keyCode + " ctrlKey: " + event.ctrlKey);
    // space 32, event.ctrlKey && ctrl+/ 191
    if(event.keyCode==32 && !(event.ctrlKey || event.altKey || event.metaKey) &&
       !isEditableTextBox(document.activeElement)) {
        if (event.shiftKey) {
            goFullScreen("fitToHeight");
        } else {
            goFullScreen("fitToWidth");
        }
    }
}

(function() {
    'use strict';
    document.addEventListener("keydown", key_event, true);
    // Close the various popup windows hogging screen space
    setTimeout(closePopUps, interval);
})();