微信读书 宽度自适应

2024/2/18 17:08:40

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        微信读书 宽度自适应
// @namespace   Weread Width Adaptation
// @match       *://weread.qq.com/web/reader/*
// @grant       none
// @version     2024.09.03.01
// @author      James Wood
// @description 2024/2/18 17:08:40
// @license MIT
// ==/UserScript==




(function () {
    'use strict';

    // 基础方法
    function getCurrentMaxWidth(element) {
        let currentValue = window.getComputedStyle(element).maxWidth;
        currentValue = currentValue.substring(0, currentValue.indexOf('px'));
        currentValue = parseInt(currentValue);
        return currentValue;
    }

    function changeWidth() {
        const items = document.querySelectorAll(".readerContent .app_content, .readerTopBar");
        const targetValue = window.innerWidth * 0.9;
        items.forEach(item => {
            const currentValue = getCurrentMaxWidth(item);
            if (currentValue !== targetValue) {
                item.style['max-width'] = targetValue + 'px';
            }
        });
        const myEvent = new Event('resize');
        window.dispatchEvent(myEvent)
    }

    // 调整宽度
    changeWidth();

    // 将.readerControls元素距离右边10%
    const readerControls = document.querySelector('.readerControls');
    readerControls.style.right = '6%';
    readerControls.style.left = 'unset';
    readerControls.style.marginLeft = 'unset';
})();