SpaceBattles Reader Mode

Some minor tweaks to help enable and preserve reader mode on Space Battles.

目前為 2025-08-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         SpaceBattles Reader Mode
// @namespace    ultrabenosaurus.SpaceBattles
// @version      1.2
// @description  Some minor tweaks to help enable and preserve reader mode on Space Battles.
// @author       Ultrabenosaurus
// @license      GNU AGPLv3
// @source       https://greasyfork.org/en/users/437117-ultrabenosaurus?sort=name
// @match        https://forums.spacebattles.com/threads/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=spacebattles.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var _chapterPosts = document.querySelectorAll('article.hasThreadmark');

    if( 0 != _chapterPosts.length ) {

        _chapterPosts.forEach( function( _post, _i, _chaps ) {

            var _postID = _post.getAttribute('data-content');

            if( "reader" == document.location.href.split('/')[5] || "reader" == document.location.href.split('/')[6] ) {
                // if in reader mode, fix post number links

                UBfixReaderPostLinks( _postID, _post.querySelectorAll( 'div.message-inner header.message-attribution ul.message-attribution-opposite li:has(a[aria-label="Share"])+li > a' ) );

            } else {
                // if not in reader mode, bring button up to top of chapter where it's actually useful

                var _btn = document.querySelector('article#js-' + _postID + ' div.message-cell li.reader a.threadmark-control');
                document.querySelector( 'article#js-' + _postID + ' div.message-cell span.primary:has(span.threadmarkLabel)' ).insertAdjacentHTML( "afterend", _btn.outerHTML.replace( _btn.href.split('#')[1], _postID ) );

            }
        });

        window.addEventListener('scroll', UBupdateLocationAnchor);

    }
})();

function UBfixReaderPostLinks( _postID, _postLinks ) {
    _postLinks.forEach( function( _link, _i, _posts ) {

        _link.setAttribute( 'href', document.location.href.split('#')[0] + '#' + _postID );

    });
}

function UBupdateLocationAnchor(){
    if( history.pushState ) {

        var _chapterPosts = document.querySelectorAll('article.hasThreadmark');

        if( 0 != _chapterPosts.length ) {
            // if browser has 'history.pushState' and page has chapters, loop them to check position

            _chapterPosts.forEach( function( _post, _i, _chaps ) {

                if( 50 > _post.getBoundingClientRect().top && 50 < _post.getBoundingClientRect().bottom ){

                    //console.log( _post.getBoundingClientRect() );

                    if( _post.getAttribute( 'id' ) != 'js-' + document.location.hash.substr( 1 ) ) {
                        // if current chapter is at or past the top of the screen, update page title and history state

                        var _title = document.querySelector( 'h1.p-title-value' ).innerHTML.trim() + " - " + _post.querySelector( 'div.message-cell span.primary span.threadmarkLabel' ).innerHTML.trim();
                        //console.log( _post.getAttribute( 'id' ).replace( 'js-', '' ), _title );

                        document.title = _title;
                        history.pushState( {}, _title, '#' + _post.getAttribute( 'id' ).replace( 'js-', '' ) );

                    } else {

                        return;

                    }
                }
            });
        }
    }
}