rexvivat / tgchan autoupdate 1.2

// ==UserScript==
// @name         tgchan autoupdate 1.2
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  tgchan autoupdate
// @author       Rex
// @include      http*://tgchan.org/kusaba/quest/res/*
// @include      http*://tgchan.org/kusaba/questdis/res/*
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @run-at       document-start
// @license      OSL-3.0
// ==/UserScript==
jQuery.noConflict();

jQuery(document).ready(function($) {
    var deadline = null;
    setInterval(function(){
        if(!deadline || jQuery('.spanUpdating').html() == 'Updating...')
            return;
        var t = deadline - new Date();
        jQuery('.spanUpdating').html((t - t % 1000) / 1000);
    },1000);
    function updateReplies(timeout) {
        jQuery('.spanUpdating').html('Updating...');
        if(!timeout)
            timeout = 0;
        jQuery.get(window.location.href, function( data ) {
            var n = data.indexOf('<div style="display: none;" class="unicorn">');
            var d = data.substring(n);
            d = d.substring(0, d.indexOf('</form>'));
            var pagedata = jQuery('<div>').append(d);
            var newReplies = pagedata.find('table:has(.reply,.highlight)').toArray().filter(function(x) { return jQuery('#' + jQuery(x).find('.reply,.highlight').get(0).id).length == 0; });
            if(newReplies.length > 0) {
                jQuery('table:has(.reply,.highlight)').last().after(newReplies);
                timeout = 0;
            }
            var updateUrl = window.location.href.split('#')[0] + '#' + pagedata.find('.reply,.highlight').last().get(0).id.replace('reply', '');
            if(updateUrl && window.location.href.split('#').pop() != updateUrl.split('#').pop()) {
                //window.location.href = updateUrl;
                highlight(updateUrl.split('#').pop());
            }
            jQuery('.spanUpdating').html('');
            if(jQuery('.chkAuto').is(':checked')) {
                var newTimeout = timeout >= 30000 ? timeout : timeout + 5000;
                deadline = new Date(new Date().getTime() + newTimeout);
                setTimeout(function() {
                    updateReplies(newTimeout);
                }, newTimeout);
            }
        });
    }
    var navbar = jQuery('.navbar');
    navbar.append(' [');
    navbar.append(jQuery('<a class="aUpdate" href="#">Update</a>'));
    jQuery('.aUpdate').click(function() { event.preventDefault(); updateReplies(); });
    navbar.append('] [');
    navbar.append(jQuery('<label><input class="chkAuto" type="checkbox" title="Fetch new replies automatically">Auto</label>'));
    jQuery('.chkAuto').mouseup(function() {
        var chk = jQuery(this).is(':checked');
        jQuery('.chkAuto').prop('checked', !chk);
        updateReplies();
    });
    navbar.append('] ');
    navbar.append(jQuery('<span class="spanUpdating"></span>'));
});