ResetEra Live Thread

Update threads without refreshing

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

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

(function() {
    'use strict';

    var countNew = 0;
    var countNewLast = 0;
    var betweenUpdates = 30;
    var errors = 0;
    var timeToNextUpdate = betweenUpdates;
    var timeout;
    var autoUpdateEnabled = false;
    var updating = false;
    var lastUrl = window.location;
    var currentPage = $('div.PageNav').first().data('page') || 1;
    var lastLoadedPage = currentPage;
    var lastPage = $('div.PageNav').first().data('last') || 1;

    GM_addStyle(
"#livethreadControl {\n" +
"    text-align: center; \n" +
"    margin-bottom: 5px; \n" +
"}\n" +
"#livethreadStatus {\n" +
"    text-align: center; \n" +
"}\n" +
"#livethreadSettings {\n" +
"    text-align: center; \n" +
"    margin: auto;\n" +
"}\n" +
        "");

    function updateStatus()
    {
        var status  = "";

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

            if (countNewLast > 0)
                status += countNewLast + " New Messages!";
            else
                status += "No New Messages";
        }
        else
            status += "Auto Update Disabled";

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

    function enableDisable(event)
    {
        event.preventDefault();

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

        updateStatus();
    }

    function timerTick()
    {
        timeToNextUpdate--;

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

        updateStatus();
    }

    function insertNotifi(text)
    {
        var $el = $('<div>', {'class': "newMessagesNotice", 'text': text});

        $("#messageList").append($el);

        return $el;
    }

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

        currentPage = pageNav.data('page') || 1;
        lastPage = pageNav.data('last') || 1;

        if (pageNav.data('baseurl') === undefined)
            return window.location;

        if (lastPage > currentPage)
            currentPage++;

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

    function updateMessagesClick(event)
    {
        event.preventDefault();
        updateMessages();
    }

    function updateMessages()
    {
        if (updating)
            return;

        updating = true;
        updateStatus();

        countNewLast = 0;

        var thisUrl = getCurrentURL();

        if (lastLoadedPage < currentPage)
        {
            window.history.pushState(null, null, thisUrl);
            lastUrl = thisUrl;

            insertNotifi("Page " + currentPage);
        }

        lastUrl = getCurrentURL();

        $.get(lastUrl, function (data) {

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

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

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

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

            });

            $('input[name="last_date"]').val(node.find('input[name="last_date"]').val());
            $('input[name="last_known_date"]').val(node.find('input[name="last_known_date"]').val());

            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);
                    var newMessage = $el.find('article');
                    var oldMessage = $curr.find('article');

                    if (newMessage.text() != oldMessage.text())
                    {
                        oldMessage.replaceWith(newMessage).xfActivate();

                        $curr.xfActivate();
                    }
                }
                else
                {
                    $el.xfInsert('appendTo', $("#messageList"));

                    countNew++;
                    countNewLast++;
                }

            });

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

    $('Div.pageNavLinkGroup').last().before('\
<div id="livethreadPanel" class="DiscussionListOptions secondaryContent">\
   <div id="livethreadStatus"></div>\
   <div id="livethreadControl" class="publicControls">\
       <a id="livethreadAutoUpdate" href="#" class="callToAction"><span>Auto Update On/Off</span></a>\
       <a id="livethreadUpdate" href="#" class="callToAction"><span>Update Now</span></a>\
   </div>\
   <div id="livethreadSettings">\
      <div class="controlGroup">\
         <label for="updateTime">Update Speed:</label>\
         <select id="updateTime" class="textCtrl">\
             <option value="15">Fast</option>\
             <option value="30" selected>Normal</option>\
             <option value="60">Slow</option>\
         </select>\
    </div>\
</div>');

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

    $('#updateTime').change(function () {
        betweenUpdates = parseInt($('#updateTime').val());

        if (betweenUpdates < timeToNextUpdate)
            timeToNextUpdate = betweenUpdates;

        updateStatus();
    });

    updateStatus();
    
})();

QingJ © 2025

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