Greasy Fork

Greasy Fork is available in English.

YouTube Normal Thumbnails

Restores normal thumbnails size

目前为 2023-05-29 提交的版本。查看 最新版本

"use strict";
// ==UserScript==
// @name         YouTube Normal Thumbnails
// @namespace    http://greasyfork.org
// @version      0.7.0
// @description  Restores normal thumbnails size
// @author       NeoCortex
// @match        *://www.youtube.com/*
// @match        *://youtube.com/*
// @run-at       document-end
// @grant        none
// ==/UserScript==
(function () {
    var perRowProperty = '--ytd-rich-grid-items-per-row';
    var styleContent = "\n    ytd-rich-grid-video-renderer[mini-mode] #video-title.ytd-rich-grid-video-renderer {\n        font-size: 1.4rem;\n        font-weight: 500;\n        line-height: 1.6rem;\n    }\n\n    #avatar-link.ytd-rich-grid-video-renderer {\n        display: none !important;\n    }\n    ".trim();
    var commonStyles = "\n    ytd-video-renderer[use-prominent-thumbs] ytd-thumbnail.ytd-video-renderer {\n        min-width: 120px !important;\n        max-width: 240px !important;\n    }\n    ".trim();
    var YoutubeThumbnailsFixer = /** @class */ (function () {
        function YoutubeThumbnailsFixer() {
            this.videoObserverConfig = {
                attributes: true,
                childList: false,
                subtree: false
            };
            this.installContentObserver();
            this.installStyle(commonStyles);
        }
        YoutubeThumbnailsFixer.prototype.installContentObserver = function () {
            var _this = this;
            this.observer = new MutationObserver(function (mutationsList, observer) {
                var _a;
                _this.target = document.querySelector('ytd-rich-grid-renderer');
                if (null !== _this.target) {
                    (_a = _this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
                    _this.installStyle(styleContent);
                    _this.updateRowsCount();
                    _this.installVideoGridObserver();
                }
            });
            this.observer.observe(document.getElementById('content'), {
                attributes: false,
                childList: true,
                subtree: true
            });
        };
        YoutubeThumbnailsFixer.prototype.installVideoGridObserver = function () {
            var _this = this;
            var _a;
            if (this.isNil(this.target)) {
                return;
            }
            this.observer = new MutationObserver(function (mutationsList, observer) {
                var _a, _b;
                for (var _i = 0, mutationsList_1 = mutationsList; _i < mutationsList_1.length; _i++) {
                    var mutation = mutationsList_1[_i];
                    if (mutation.attributeName == 'style') {
                        (_a = _this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
                        _this.updateRowsCount();
                        (_b = _this.observer) === null || _b === void 0 ? void 0 : _b.observe(_this.target, _this.videoObserverConfig);
                    }
                }
            });
            (_a = this.observer) === null || _a === void 0 ? void 0 : _a.observe(this.target, this.videoObserverConfig);
            console.info("[YouTube Normal Thumbnails] Changed to " + this.currentPerRow + " thumbnails per row instead of " + this.oldPerRow + ".");
        };
        YoutubeThumbnailsFixer.prototype.updateRowsCount = function () {
            if (this.isNil(this.target)) {
                return;
            }
            try {
                if (this.oldPerRow === undefined || this.oldPerRow == this.currentPerRow || this.oldPerRow === 0) {
                    this.oldPerRow = this.currentPerRow;
                    this.currentPerRow += 1;
                }
            }
            catch (e) {
                console.warn("[YouTube Normal Thumbnails] Cannot update thumbnails count: " + e);
            }
        };
        YoutubeThumbnailsFixer.prototype.installStyle = function (contents) {
            var style = document.createElement('style');
            style.innerHTML = contents;
            document.body.appendChild(style);
        };
        YoutubeThumbnailsFixer.prototype.isNil = function (item) {
            return null === item || undefined === item;
        };
        Object.defineProperty(YoutubeThumbnailsFixer.prototype, "currentPerRow", {
            get: function () {
                if (this.isNil(this.target)) {
                    return 0;
                }
                return +this.target.style.getPropertyValue(perRowProperty);
            },
            set: function (value) {
                var _a;
                (_a = this.target) === null || _a === void 0 ? void 0 : _a.style.setProperty(perRowProperty, String(value));
            },
            enumerable: false,
            configurable: true
        });
        return YoutubeThumbnailsFixer;
    }());
    new YoutubeThumbnailsFixer();
})();