Advertisement
Guest User

tgchan autoupdate

a guest
Mar 14th, 2018
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         tgchan autoupdate
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.0
  5. // @description  tgchan autoupdate
  6. // @author       Rex
  7. // @include      http*://tgchan.org/kusaba/quest/res/*
  8. // @include      http*://tgchan.org/kusaba/questdis/res/*
  9. // @grant        none
  10. // @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
  11. // @run-at       document-start
  12. // ==/UserScript==
  13. jQuery.noConflict();
  14.  
  15. jQuery(document).ready(function($) {
  16.     var deadline = null;
  17.     setInterval(function(){
  18.         if(!deadline || jQuery('.spanUpdating').html() == 'Updating...')
  19.             return;
  20.         var t = deadline - new Date();
  21.         jQuery('.spanUpdating').html((t - t % 1000) / 1000);
  22.     },1000);
  23.     function updateReplies() {
  24.         jQuery.get(window.location.href, function( data ) {
  25.             var n = data.indexOf('<div style="display: none;" class="unicorn">');
  26.             var d = data.substring(n);
  27.             d = d.substring(0, d.indexOf('</form>'));
  28.             var pagedata = jQuery('<div>').append(d);
  29.             jQuery('table:has(.reply)').last().after(pagedata.find('table:has(.reply)').toArray().filter(function(x) { return jQuery('#' + jQuery(x).find('.reply').get(0).id).length == 0; }));
  30.             var updateUrl = window.location.href.split('#')[0] + '#' + pagedata.find('.reply').last().get(0).id.replace('reply', '');
  31.             if(updateUrl && window.location.href.split('#').pop() != updateUrl.split('#').pop())
  32.                 window.location.replace(updateUrl);
  33.             jQuery('.spanUpdating').html('');
  34.             if(jQuery('.chkAuto').is(':checked')) {
  35.                 deadline = new Date(new Date().getTime() + 5000);
  36.                 setTimeout(function() {
  37.                     checkWTForUpdates(5000);
  38.                 }, 5000);
  39.             }
  40.         });
  41.     }
  42.     function checkWTForUpdates(timeout) {
  43.         jQuery('.spanUpdating').html('Updating...');
  44.         var urlparts = window.location.href.split('/');
  45.         new Ajax.Request(ku_boardspath + '/threadwatch.php?board=' + urlparts[4] + '&threadid=' + /*urlparts.pop().split('.')[0]*/'0',
  46.         {
  47.             method:'get',
  48.             onSuccess: function(transport){
  49.                 var response = transport.responseText;
  50.                 if(!response)
  51.                     location.reload();
  52.                 console.log(transport.responseText, timeout);
  53.                 var resObject = jQuery('<div>').append(transport.responseText).find('strong');
  54.                 if(resObject.html() != '0')
  55.                     updateReplies();
  56.                     //updateReplies(resObject.parent().attr('href'));
  57.                 else {
  58.                     jQuery('.spanUpdating').html('');
  59.                     var newTimeout = timeout >= 30000 ? timeout : timeout + 5000;
  60.                     deadline = new Date(new Date().getTime() + newTimeout);
  61.                     setTimeout(function() {
  62.                         if(jQuery('.chkAuto').is(':checked'))
  63.                             checkWTForUpdates(newTimeout);
  64.                     }, newTimeout);
  65.                 }
  66.             },
  67.             onFailure: function(){ location.reload(); }
  68.         });
  69.     }
  70.     var navbar = jQuery('.navbar');
  71.     navbar.append(' [');
  72.     navbar.append(jQuery('<a class="aUpdate" href="#">Update</a>'));
  73.     jQuery('.aUpdate').click(function() { event.preventDefault(); updateReplies(); });
  74.     navbar.append('] [');
  75.     navbar.append(jQuery('<label><input class="chkAuto" type="checkbox" title="Fetch new replies automatically">Auto</label>'));
  76.     jQuery('.chkAuto').mouseup(function() {
  77.         var chk = jQuery(this).is(':checked');
  78.         jQuery('.chkAuto').prop('checked', !chk);
  79.         checkWTForUpdates(0);
  80.     });
  81.     navbar.append('] ');
  82.     navbar.append(jQuery('<span class="spanUpdating"></span>'));
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement