Google Play Books Webreader Centering

Fix the centering of Google Play Books webreader content on monitors > 1760px width.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Google Play Books Webreader Centering
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Fix the centering of Google Play Books webreader content on monitors > 1760px width.
// @author       Geoffrey Parker <[email protected]>
// @match        *://books.googleusercontent.com/*
// @grant        none
// ==/UserScript==

(async function() {
    'use strict';

    async function getElem(selector, maxRetries, retryInterval=100) {
        var elem = document.querySelector(selector);
        var retries = 0;
        while (elem == null && !(retries >= maxRetries)) {
            await new Promise(r => setTimeout(r, retryInterval)); // sleep (default 100ms)
            elem = document.querySelector(selector); // try again
            retries++;
        }
        return elem != null ? Promise.resolve(elem) : Promise.reject(new Error("Timeout waiting for element"));
    }

    try {
        let elem = await getElem("body > div.gb-reader-container > div.gb-text-reader > div > table", 50);
        elem.removeAttribute("style");
        let elemObserver = new MutationObserver((mutationsList, observer) => elem.removeAttribute("style"));
        elemObserver.observe(elem, {attributes: true, attributeFilter: ["style"]});
    } catch (err) {
        console.error("Could not center:", err);
    }
})();