AO3 Remove gaps between paragraphs

Remove empty paragraphs and leading/trailing breaks in AO3 works.

目前为 2025-04-30 提交的版本。查看 最新版本

// ==UserScript==
// @name        AO3 Remove gaps between paragraphs
// @description Remove empty paragraphs and leading/trailing breaks in AO3 works.
// @author      C89sd
// @version     1.0
// @match       https://archiveofourown.org/works/*
// @namespace    https://greasyfork.org/users/1376767
// ==/UserScript==

// Testing:
// p.innerHTML = " \t\n "
// https://archiveofourown.org/works/63851347                     empty <p>
// https://archiveofourown.org/works/13439460                     leading <br>
// https://archiveofourown.org/works/5191202?view_full_work=true  formatting

const isEmpty = /^\s*$/;

const ps = document.querySelectorAll('div#workskin p');
for (const p of ps) {
  if (isEmpty.test(p.textContent)) p.remove();
  while (p.firstChild && p.firstChild.nodeName === 'BR') p.removeChild(p.firstChild);
  while (p.lastChild  && p.lastChild.nodeName  === 'BR') p.removeChild(p.lastChild);
}