Skip to content

Instantly share code, notes, and snippets.

@nilslindemann
Last active April 6, 2021 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilslindemann/e50cb02ea4afc48cc97e50c0a9d87f8a to your computer and use it in GitHub Desktop.
Save nilslindemann/e50cb02ea4afc48cc97e50c0a9d87f8a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Fedora Forum Tweaks
// @match https://fedoraforum.org/*
// @match https://forums.fedoraforum.org/*
// @grant GM_addStyle
// @version 2
// ==/UserScript==
{
'use strict';
// ---------------- USER CONFIGURATION ----------------
// Possible values are: true, false
// Example: https://forums.fedoraforum.org/showthread.php?286110-remap-specific-keyboard-keys#post1615714
let FIX_CODEBLOCK_LAYOUT = true
let REMOVE_TEXT_ABOVE_CODEBLOCKS = true
// Example: https://forums.fedoraforum.org/showthread.php?316568-How-do-I-subscribe-to-a-thread-and-find-it-again#post1800021
// But: https://forums.fedoraforum.org/showthread.php?325913-Grub2-error-message#post1846439
let FIX_LIGHTBOX_LAYOUT = true
// Affects all pages. Expands the content area so that the blue blackground disappears.
let EXPAND_CONTENT = true
// ---------------- CODE SECTION (Experts only) ----------------
// Shortcuts for the script author
const log = console.log.bind(console)
const create = document.createElement.bind(document)
const get_all = document.querySelectorAll.bind(document)
const ELEM = Node.ELEMENT_NODE
const TEXT = Node.TEXT_NODE
const COMMENT = Node.COMMENT_NODE
function copy_attribs(fromelem, toelem) {
Array.from(fromelem.attributes).forEach(attr => {
toelem.setAttribute(attr.name, attr.value);
})
}
function not_just_whitespace(string) {
return !/\S/.test(string)
}
function is_forum_thread() {
return /[/]showthread[.]php[?]/.test(location.href)
}
// Cleanup functions
// Inspired by https://www.sitepoint.com/removing-useless-nodes-from-the-dom/
function cleanup_whitespace(node, recursive=false) {
for(let curpos = 0; curpos < node.childNodes.length; curpos++) {
let child = node.childNodes[curpos]
let childtype = child.nodeType
if ( childtype === COMMENT || (childtype === TEXT && not_just_whitespace(child.nodeValue)) ) {
curpos --
node.removeChild(child)
}
else if(recursive && childtype === ELEM) {
clean(child)
}
}
}
function fix_paragraphs(old){
let parent = old.parentNode
let next = old.nextSibling
old.remove()
cleanup_whitespace(old)
let fixed = create('div')
copy_attribs(old, fixed)
let brs = []
let p = create('p')
while (old.hasChildNodes()) {
const child = old.firstChild
const childtype = child.nodeType
if (childtype === ELEM && child.tagName === 'BR') {
brs.push(child)
old.removeChild(child)
}
else if (childtype === ELEM && child.tagName === 'DIV') {
if (p.hasChildNodes()) {
fixed.appendChild(p)
}
p = create('p')
fixed.appendChild(child)
brs = []
}
else if ( (brs.length > 1) && (p.hasChildNodes()) ) {
fixed.appendChild(p)
p = create('p')
p.appendChild(child)
brs = []
}
else if (brs.length === 1) {
p.appendChild(brs[0])
p.appendChild(child)
brs = []
}
else {
p.appendChild(child)
brs = []
}
}
if (p.hasChildNodes()) {
fixed.appendChild(p)
}
parent.insertBefore(fixed, next)
}
// Main program
console.clear()
if ( is_forum_thread() ) {
get_all('.postcontent.restore').forEach(fix_paragraphs)
get_all('.postcontent.restore .bbcode_quote .message').forEach(fix_paragraphs)
}
if (EXPAND_CONTENT) {
GM_addStyle(`
body {
margin:0!important;
padding:0!important;
}
.below_body {
margin-bottom:0!important;
border-radius:0!important;
}
.above_body {
border-radius:0!important;
}
`)
}
if (FIX_CODEBLOCK_LAYOUT) {
GM_addStyle(`
div.bbcode_container {
margin-bottom:1em!important;
}
div.bbcode_container>pre.bbcode_code {
overflow:visible!important;
height:auto!important;
}
`)
}
if (REMOVE_TEXT_ABOVE_CODEBLOCKS) {
get_all('.bbcode_container > .bbcode_description').forEach(function(codelabel){
codelabel.remove()
})
}
if (FIX_LIGHTBOX_LAYOUT) {
GM_addStyle(`
a[rel*="Lightbox_"] {
display:block;
margin-top:1em;
}
`)
}
// Version history:
// v1 Initial commit
// v2 `fix_paragraphs()` now runs faster. Cosmetic cleanup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment