Greasy Fork

Greasy Fork is available in English.

AO3 Remove Double-Spacing

Removes awkward double spaces between paragraphs on AO3.

当前为 2020-06-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

function removeDoubleSpacing (doc) {
  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)
  }

  function supposedToBeEmpty (el) {
    var hr = el.tagName === 'HR' || el.querySelector('hr')
    var br = el.tagName === 'BR' || el.querySelector('br')

    return !!(hr || br)
  }

  // Hide the given element if it has no text content.
  function hideEmptyElement (el) {
    var content = el.textContent && el.textContent.replace(/ /g, '').trim()
    if (!content) {
        // But, if it has no text because it contains media,
        // or if it's an element that's meant to be empty, don't hide.
        if (hasMedia(el) || supposedToBeEmpty(el)) {
          return
        }

        el.style.display = 'none'
    }
  }

  var chapters = doc.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 unneeded <br>s between paragraphs
  chapters.innerHTML = chapters.innerHTML.replace(/<\/\s*p>\s*(<br\s*\/?>\s*)+\s*<p/gi, '</p><p')

  // Remove unneeded <br>s between divs
  chapters.innerHTML = chapters.innerHTML.replace(/<\/\s*div>\s*(<br\s*\/?>\s*)+\s*<div/gi, '</div><div')

  // Cut down size of paragraphs with only linebreaks in them
  chapters.innerHTML = chapters.innerHTML.replace(/<p>\s*(<br\s*\/?>\s*)+\s*<\/\s*p>/gi, '<p><br /></p>')

  // Cut down size of divs with only linebreaks in them
  chapters.innerHTML = chapters.innerHTML.replace(/<div>\s*(<br\s*\/?>\s*)+<\/\s*div>/gi, '<div><br /></div>')

  // Remove pointless <br>s at beginning of paragraphs
  chapters.innerHTML = chapters.innerHTML.replace(/<p>\s*<br\s*\/?>/gi, '<p>')

  // Remove <br/>s between spans
  chapters.innerHTML = chapters.innerHTML.replace(/<span>\s*(<br\s*\/*>\s*)+<\/\s*span>/gi, '')

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

removeDoubleSpacing(document);