AO3 Remove Double-Spacing

Removes awkward double spaces between paragraphs on AO3.

Verzia zo dňa 30.08.2019. Pozri najnovšiu verziu.

// ==UserScript==
// @name         AO3 Remove Double-Spacing
// @namespace    ao3-remove-double-spacing
// @version      1.0
// @description  Removes awkward double spaces between paragraphs on AO3.
// @author       yuube
// @match        http*://*.archiveofourown.org/works/*
// @grant        none
// ==/UserScript==

function hasMedia (el) {
  var hasImg = el.tagName === 'IMG' || el.querySelector('img')
  var hasEmbed = el.tagName === 'EMBED' || el.querySelector('embed')
  var hasIframe = el.tagName === 'IFRAME' || el.querySelector('iframe')
  var hasVideo = el.tagName === 'VIDEO' || el.querySelector('video')

  return !!(hasImg || hasEmbed || hasIframe || hasVideo)
}

// Hide the given element if it has no text content.
function hideEmptyElement (el) {
  var content = el.textContent && el.textContent.trim().replace(' ', '')
  if (!content) {
      // But, if it has no text because it contains media, don't hide.
      if (hasMedia(el)) {
        return
      }

      el.style.display = 'none'
  }
}

var chapters = document.querySelector('#chapters')

// Remove empty paragraphs
chapters.querySelectorAll('p').forEach(hideEmptyElement);

// Remove empty divs
chapters.querySelectorAll('div').forEach(hideEmptyElement);

// Remove empty spans
chapters.querySelectorAll('span').forEach(hideEmptyElement);

// Remove any other empty elements (only catches elements that are completely
// empty, with no children)
chapters.querySelectorAll(':empty').forEach(hideEmptyElement);

// Remove excessive line breaks (replaces 2 or more <br />s with one)
chapters.innerHTML = chapters.innerHTML.replace(/(<br\s*\/?>){3,}/gi, '<br>');