查看最新创建帖子的油猴代码

基于下边这个佬的帖子修改
https://linux.do/t/topic/31420
新版本上线后选中ui样式有问题。
简单修改了下,要不看着不得劲儿。
效果:

下边是油猴代码:

// ==UserScript==
// @name         Linux.do按帖子创建时间排序
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  在 Linux.do 网页上修改链接和增加新链接
// @author       马克思
// @match        https://linux.do/*
// @grant        none
// @icon         https://www.google.com/s2/favicons?sz=64&domain=linux.do
// ==/UserScript==

(function () {
    'use strict';
    var count = 0

    function modifyNavigationBar() {

        var navigationBar = document.querySelector('ul#navigation-bar');
        if (navigationBar) {
            var navigationItems = navigationBar.querySelectorAll('li');
            navigationItems.forEach(function (item) {
                var anchor = item.querySelector('a');
                if (anchor && anchor.textContent.trim() === "最新") {
                    anchor.textContent = "最新回复";
                    var newestCreatedElement = document.createElement('li');
                    newestCreatedElement.title = "最新创建的帖子";
                    newestCreatedElement.id = "ember999";
                    item.insertAdjacentElement('afterend', newestCreatedElement);
                    console.log(location.href);
                    if (location.href.indexOf("order=created") !== -1) {
                        newestCreatedElement.className = "active latest_created ember-view nav-item_latest_created";
                        newestCreatedElement.innerHTML = '<a  class="active" href="' + anchor.getAttribute('href') + '?order=created" >最新创建</a>';
                        anchor.className = ""
                    } else {
                        newestCreatedElement.className = "latest_created ember-view nav-item_latest_created";
                        newestCreatedElement.innerHTML = '<a  href="' + anchor.getAttribute('href') + '?order=created" class="" >最新创建</a>';
                    }

                    return true;
                }
            });
        }
    }


    // 使用 MutationObserver 来监视 DOM 变化
    const observer = new MutationObserver((mutations) => {
        for (let mutation of mutations) {
            if (mutation.type === 'childList') {
                const navBar = document.getElementById('navigation-bar');
                if (navBar) {
                    modifyNavigationBar()
                    break;
                }
            }
        }
    });

    // 开始观察 document.body 的变化
    observer.observe(document.body, {childList: true, subtree: true});

    // 如果页面已经加载完成,立即尝试添加新的导航项
    if (document.readyState === 'complete' || document.readyState === 'interactive') {
        console.log("complete")
        modifyNavigationBar()
    }
})();


5 个赞

感谢佬分享。(最新回复是不是就包括了更新话题)

感谢大佬分享!

从新更新了下代码,之前的子类有点问题。

1 个赞

最新回复是按照最新评论的帖子排序,最新创建是按照创建时间排序

感谢大佬,如果点击左上角的论坛logo也能回到按照创建时间排序的页面就更好了 :grinning: