ResetEra Live Thread

Update threads without refreshing

La data de 27-10-2017. Vezi ultima versiune.

// ==UserScript==
// @name         ResetEra Live Thread
// @namespace    http://madjoki.com
// @version      0.3
// @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;
    var lastUrl = window.location;

    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
        {
            timeout = setInterval(timerTick, 1000);
            autoUpdateEnabled = true;
        }

        updateStatus();
    }

    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 (current === undefined)
            return window.location;

        if (last > current)
            current++;

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

    function updateMessages()
    {
        if (updating)
            return;

        updating = true;
        updateStatus();

        countNewLast = 0;

        var thisUrl = getCurrentURL();

        if (thisUrl != lastUrl)
        {
            window.history.pushState(null, null, thisUrl);
            lastUrl = thisUrl;
        }

        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());

            });

            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.html() != oldMessage.html())
                        oldMessage.replaceWith(newMessage).xfActivate();
                }
                else
                {
                    $el.xfInsert('appendTo', $("#messageList"));

                    countNew++;
                    countNewLast++;
                }

            });

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

    $('.quickReply.message').before('<div class="secondaryContent">\
<div id="livethreadStatus"></div>\
<button id="livethreadAutoUpdate" class="button">Auto Update On/Off</button>\
<button id="livethreadUpdate" class="button">Update</button>\
<select id="updateTime">\
<option value="15">Fast</option>\
<option value="30" selected>Normal</option>\
<option value="60">Slow</option>\
</select>\
</div>');

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

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

    updateStatus();
    
})();