ResetEra Live Thread

Update threads without refreshing

当前为 2017-10-27 提交的版本,查看 最新版本

// ==UserScript==
// @name         ResetEra Live Thread
// @namespace    http://madjoki.com
// @version      0.1
// @description  Update threads without refreshing
// @author       Madjoki
// @match        https://www.resetera.com/threads/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var countNew = 0;
    var countNewLast = 0;
    var betweenUpdates = 30;
    var timeToNextUpdate = betweenUpdates;
    var timeout;
    var autoUpdateEnabled = false;
    var updating = false;

    function updateStatus()
    {
        var status  = "";

        if (updating)
            status += "Updating - ";
        else if (autoUpdateEnabled)
            status += "Next Update In " + timeToNextUpdate + " seconds - ";
        else
            status += "Disabled - ";

        if (countNewLast > 0)
            status += countNewLast + " new messages!";
        else
            status += "No New Messages";


        $("#livethreadStatus").text(status);
    }

    function enableDisable()
    {
        if (autoUpdateEnabled)
        {
            clearInterval(timeout);
            autoUpdateEnabled = false;
        }
        else
        {
            setInterval(timerTick, 1000);
            autoUpdateEnabled = true;
        }
    }

    function timerTick()
    {
        timeToNextUpdate--;

        if (timeToNextUpdate === 0)
        {
            updateMessages();
            timeToNextUpdate = betweenUpdates
        }

        updateStatus();
    }

    function getCurrentURL()
    {
        var pageNav = $('div.PageNav').first();

        var current = pageNav.data('page');
        var last = pageNav.data('last');

        if (last > current)
            current++;

        return pageNav.data('baseurl').replace('{{sentinel}}', current);
    }

    function updateMessages()
    {
        updating = true;
        updateStatus();

        countNewLast = 0;

        $.get(getCurrentURL(), function (data) {

            var node = $($.parseHTML(data));

            var newNav = node.find('div.PageNav').first();

            $('div.PageNav').each(function (i, el) {

                $(el).replaceWith(newNav.clone());

            });

            node.find('#messageList > li').each(function (i, el) {

                var $el = $(el);

                var id = $el.attr('id');

                var $curr = $('#' + id);

                if ($curr.length)
                {
                    //$curr.replaceWith($el);
                }
                else
                {
                    $el.xfInsert('appendTo', $("#messageList"));

                    countNew++;
                    countNewLast++;
                }

            });

        }).always(function () { updating = false; });
    }

    $('.quickReply.message').before('<div class="secondaryContent"><div id="livethreadStatus"></div><button id="livethreadAutoUpdate">Auto Update On/Off</button><button id="livethreadUpdate">Update</button></div>');

    $('#livethreadAutoUpdate').click(enableDisable);
    $('#livethreadUpdate').click(updateMessages);

    updateStatus();
    
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址