Greasy Fork

AO3 Fix Paragraph Spacing

Removes excessive spacing between paragraphs on AO3.

目前为 2023-06-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         AO3 Fix Paragraph Spacing
// @namespace    ao3-remove-double-spacing
// @version      1.6
// @description  Removes excessive spacing between paragraphs on AO3.
// @author       yuube
// @match        http*://*.archiveofourown.org/*
// @icon         https://www.google.com/s2/favicons?domain=archiveofourown.org
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function hasMedia (el) {
        var mediaWhitelist = [
            'IMG',
            'EMBED',
            'IFRAME',
            'VIDEO'
        ];

        var whitelisted = false;

        mediaWhitelist.forEach(item => {
            if (el.tagName === item || el.querySelector(item)) {
                whitelisted = true;
            };
        });

        return whitelisted
    };

    function naturallyEmpty (el) {
        var emptyWhitelist = [
          'HR'
        ];

        var whitelisted = false;
        var tagName = el.tagName;

        emptyWhitelist.forEach(item => {
          if (tagName === item || el.querySelector(item)) {
              whitelisted = true;
          };
        });

        if (tagName === 'BR') {
            whitelisted = true
        };

        return whitelisted
    };

    function removeEmptyElement (el) {
        var content = el.textContent && el.textContent.trim()

        if (!content) {
            if (hasMedia(el) || naturallyEmpty(el)) {
                return
            }

            el.remove();
        }
    };

    var parent = document.querySelectorAll('.userstuff');

    parent.forEach(userstuff => {
        // Commonly empty elements
        ['p', 'div', 'span', ':empty'].forEach(el => {
            userstuff.querySelectorAll(el).forEach(removeEmptyElement);
        });

        // <br>s with non-text siblings
        userstuff.querySelectorAll('br').forEach(el => {
            var previous = el.previousSibling;
            var next = el.nextSibling;

            if (previous.nodeType !== 3 && next.nodeType !== 3) { el.remove(); };
        });
    });
})();