Greasy Fork

Greasy Fork is available in English.

Links to Next and Previous Posts

Add links to next and previous posts to each post

当前为 2019-11-23 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Links to Next and Previous Posts
// @namespace    http://greasyfork.icu/en/users/163551-vannius
// @version      1.5
// @license      MIT
// @description  Add links to next and previous posts to each post
// @author       Vannius
// @match        https://www.alternatehistory.com/forum/threads/*
// @match        https://forums.spacebattles.com/threads/*
// @grant        none
// ==/UserScript==

(function () {
    if (window.location.host === "www.alternatehistory.com") {
        // Get hash links from divTags and store in hashLinks
        const divTags = document.getElementsByClassName('messageBarNumber');
        const hashLinks = Array.from(divTags).map((element) => element.children[0].href);
        // Very first post of thread's divTag.children[0].href don't have #.
        if (window.location.href.split('/')[6] === '') hashLinks[0] += '#' + divTags[0].parentNode.parentNode.id;

        for (let i = 0; i < divTags.length; i++) {
            // Add a link to next post
            if (i !== divTags.length - 1) {
                const nextPost = document.createElement('a');
                nextPost.title = "Next post";
                nextPost.href = hashLinks[i + 1];
                nextPost.appendChild(document.createTextNode('▼'));
                divTags[i].appendChild(nextPost);
            }
            // Add a link to prev post
            if (i !== 0) {
                const prevPost = document.createElement('a');
                prevPost.title = "Prev post";
                prevPost.href = hashLinks[i - 1];
                prevPost.appendChild(document.createTextNode('▲'));
                const fragment = document.createDocumentFragment();
                fragment.appendChild(prevPost);
                fragment.appendChild(document.createTextNode('\n'));
                divTags[i].insertBefore(fragment, divTags[i].children[0]);
            }
            // Add a link to current post
            if (i === 0 || i === divTags.length - 1) {
                const currentPost = document.createElement('a');
                currentPost.title = "Current post";
                currentPost.href = hashLinks[i];
                currentPost.appendChild(document.createTextNode('◈'));
                if (i === 0) {
                    const fragment = document.createDocumentFragment();
                    fragment.appendChild(currentPost);
                    fragment.appendChild(document.createTextNode('\n'));
                    divTags[i].insertBefore(fragment, divTags[i].children[0]);
                } else if (i === divTags.length - 1) {
                    divTags[i].appendChild(currentPost);
                }
            }
        }
    } else if (window.location.host === "forums.spacebattles.com") {
        // Get message ids from messageTags and make in hashLinks
        const messageTags = document.querySelectorAll('.message-attribution-opposite--list > li:nth-child(3)');
        const hashLinks = Array.from(messageTags).map(element => '#' + element.closest('article').id);

        for (let i = 0; i < messageTags.length; i++) {
            // Add a link to next post
            if (i !== messageTags.length - 1) {
                const nextPost = document.createElement('a');
                nextPost.title = "Next post";
                nextPost.href = hashLinks[i + 1];
                nextPost.appendChild(document.createTextNode('▼'));
                const liTag = document.createElement('li');
                liTag.appendChild(nextPost);
                messageTags[i].parentElement.appendChild(liTag);
            }

            // Add a link to prev post
            if (i !== 0) {
                const prevPost = document.createElement('a');
                prevPost.title = "Prev post";
                prevPost.href = hashLinks[i - 1];
                prevPost.appendChild(document.createTextNode('▲'));
                const liTag = document.createElement('li');
                liTag.appendChild(prevPost);
                messageTags[i].parentElement.insertBefore(liTag, messageTags[i]);
            }

            // Add a link to current post
            if (i === 0 || i === messageTags.length - 1) {
                const currentPost = document.createElement('a');
                currentPost.title = "Current post";
                currentPost.href = hashLinks[i];
                currentPost.appendChild(document.createTextNode('◈'));
                const liTag = document.createElement('li');
                liTag.appendChild(currentPost);
                if (i === 0) {
                    messageTags[i].parentElement.insertBefore(liTag, messageTags[i]);
                } else if (i === messageTags.length - 1) {
                    messageTags[i].parentElement.appendChild(liTag);
                }
            }
        }
    }
})();