Google Play Books Webreader Centering

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);
    }
})();