2012-03-14 23:44:15 -04:00
|
|
|
/*
|
|
|
|
* auto-reload.js
|
2012-03-30 20:13:11 -04:00
|
|
|
* https://github.com/savetheinternet/Tinyboard/blob/master/js/auto-reload.js
|
2012-03-14 23:44:15 -04:00
|
|
|
*
|
|
|
|
* Brings AJAX to Tinyboard.
|
|
|
|
*
|
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
|
2014-01-19 07:40:29 -05:00
|
|
|
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
|
2014-01-19 08:27:24 -05:00
|
|
|
* Copyright (c) 2013 undido <firekid109@hotmail.com>
|
2014-09-24 17:29:12 -04:00
|
|
|
* Copyright (c) 2014 Fredrick Brennan <admin@8chan.co>
|
2012-03-14 23:44:15 -04:00
|
|
|
*
|
|
|
|
* Usage:
|
2012-03-15 01:19:26 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
2013-12-28 19:10:35 -05:00
|
|
|
* //$config['additional_javascript'][] = 'js/titlebar-notifications.js';
|
2012-03-15 01:19:26 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/auto-reload.js';
|
2012-03-14 23:44:15 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-12-28 19:10:35 -05:00
|
|
|
auto_reload_enabled = true; // for watch.js to interop
|
|
|
|
|
2012-03-14 23:44:15 -04:00
|
|
|
$(document).ready(function(){
|
|
|
|
if($('div.banner').length == 0)
|
|
|
|
return; // not index
|
2013-03-19 23:56:59 -04:00
|
|
|
|
|
|
|
if($(".post.op").size() != 1)
|
|
|
|
return; //not thread page
|
2012-03-14 23:44:15 -04:00
|
|
|
|
2012-03-15 05:40:52 -04:00
|
|
|
var poll_interval;
|
2013-12-23 10:34:44 -05:00
|
|
|
|
2014-09-24 17:29:12 -04:00
|
|
|
// Add an update link
|
|
|
|
$('.boardlist.bottom').prev().after("<a href='#' id='update_thread' style='padding-left:10px'>["+_("Update thread")+"] (<span id='update_secs'></span>)</a>");
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-05-05 15:51:51 -04:00
|
|
|
// Grab the settings
|
|
|
|
var settings = new script_settings('auto-reload');
|
|
|
|
var poll_interval_mindelay_bottom = settings.get('min_delay_bottom', 3000);
|
|
|
|
var poll_interval_mindelay_top = settings.get('min_delay_top', 10000);
|
|
|
|
var poll_interval_maxdelay = settings.get('max_delay', 600000);
|
|
|
|
var poll_interval_shortdelay = settings.get('quick_delay', 100);
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-05-05 15:51:51 -04:00
|
|
|
// number of ms to wait before reloading
|
|
|
|
var poll_interval_delay = poll_interval_mindelay_bottom;
|
2014-09-24 17:29:12 -04:00
|
|
|
var poll_current_time = poll_interval_delay;
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
var end_of_page = false;
|
2013-12-23 11:31:47 -05:00
|
|
|
|
|
|
|
var new_posts = 0;
|
|
|
|
var first_new_post = null;
|
2013-12-28 19:10:35 -05:00
|
|
|
|
|
|
|
if (typeof update_title == "undefined") {
|
|
|
|
var update_title = function() { };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof add_title_collector != "undefined")
|
|
|
|
add_title_collector(function(){
|
|
|
|
return new_posts;
|
|
|
|
});
|
2013-12-23 11:31:47 -05:00
|
|
|
|
|
|
|
var window_active = true;
|
|
|
|
$(window).focus(function() {
|
|
|
|
window_active = true;
|
|
|
|
recheck_activated();
|
2014-05-05 15:54:40 -04:00
|
|
|
|
|
|
|
// Reset the delay if needed
|
|
|
|
if(settings.get('reset_focus', true)) {
|
|
|
|
poll_interval_delay = end_of_page
|
|
|
|
? poll_interval_mindelay_bottom
|
|
|
|
: poll_interval_mindelay_top;
|
|
|
|
}
|
2013-12-23 11:31:47 -05:00
|
|
|
});
|
|
|
|
$(window).blur(function() {
|
|
|
|
window_active = false;
|
|
|
|
});
|
2012-03-15 05:40:52 -04:00
|
|
|
|
2014-09-24 17:29:12 -04:00
|
|
|
var timer_update = function() {
|
|
|
|
$('#update_secs').text(poll_current_time/1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
var decrement_timer = function() {
|
|
|
|
poll_current_time = poll_current_time - 1000;
|
|
|
|
}
|
|
|
|
|
2013-12-23 11:31:47 -05:00
|
|
|
var recheck_activated = function() {
|
|
|
|
if (new_posts && window_active &&
|
|
|
|
$(window).scrollTop() + $(window).height() >=
|
|
|
|
$(first_new_post).position().top) {
|
|
|
|
|
|
|
|
new_posts = 0;
|
|
|
|
}
|
|
|
|
update_title();
|
|
|
|
};
|
|
|
|
|
2012-03-15 05:40:52 -04:00
|
|
|
var poll = function() {
|
2012-03-14 23:44:15 -04:00
|
|
|
$.ajax({
|
|
|
|
url: document.location,
|
|
|
|
success: function(data) {
|
|
|
|
$(data).find('div.post.reply').each(function() {
|
|
|
|
var id = $(this).attr('id');
|
|
|
|
if($('#' + id).length == 0) {
|
2013-12-23 11:31:47 -05:00
|
|
|
if (!new_posts) {
|
|
|
|
first_new_post = this;
|
|
|
|
}
|
2012-03-15 18:08:03 -04:00
|
|
|
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
|
2013-12-23 11:31:47 -05:00
|
|
|
new_posts++;
|
2012-03-18 08:55:01 -04:00
|
|
|
$(document).trigger('new_post', this);
|
2013-12-23 11:31:47 -05:00
|
|
|
recheck_activated();
|
2012-03-14 23:44:15 -04:00
|
|
|
}
|
|
|
|
});
|
2013-12-28 19:10:35 -05:00
|
|
|
time_loaded = Date.now(); // interop with watch.js
|
2012-03-14 23:44:15 -04:00
|
|
|
}
|
|
|
|
});
|
2012-03-15 05:40:52 -04:00
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
clearTimeout(poll_interval);
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-05-05 14:29:19 -04:00
|
|
|
// If there are no new posts, double the delay. Otherwise set it to the min.
|
|
|
|
if(new_posts == 0) {
|
|
|
|
poll_interval_delay *= 2;
|
2014-05-05 14:30:59 -04:00
|
|
|
|
|
|
|
// Don't increase the delay beyond the maximum
|
|
|
|
if(poll_interval_delay > poll_interval_maxdelay) {
|
|
|
|
poll_interval_delay = poll_interval_maxdelay;
|
|
|
|
}
|
2014-05-05 14:29:19 -04:00
|
|
|
} else {
|
|
|
|
poll_interval_delay = end_of_page
|
|
|
|
? poll_interval_mindelay_bottom
|
|
|
|
: poll_interval_mindelay_top;
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:28:07 -04:00
|
|
|
poll_interval = setTimeout(poll, poll_interval_delay);
|
2014-09-24 17:29:12 -04:00
|
|
|
poll_current_time = poll_interval_delay;
|
2014-09-25 00:31:40 -04:00
|
|
|
|
|
|
|
return false;
|
2012-03-15 05:40:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
$(window).scroll(function() {
|
2013-12-23 11:31:47 -05:00
|
|
|
recheck_activated();
|
|
|
|
|
|
|
|
if($(this).scrollTop() + $(this).height() <
|
|
|
|
$('div.post:last').position().top + $('div.post:last').height()) {
|
2013-12-23 10:34:44 -05:00
|
|
|
end_of_page = false;
|
2012-03-15 05:40:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
clearTimeout(poll_interval);
|
2014-05-05 14:28:07 -04:00
|
|
|
poll_interval = setTimeout(poll, poll_interval_shortdelay);
|
2014-09-24 17:29:12 -04:00
|
|
|
poll_current_time = poll_interval_shortdelay;
|
2013-12-23 10:34:44 -05:00
|
|
|
end_of_page = true;
|
2012-03-15 05:40:52 -04:00
|
|
|
}).trigger('scroll');
|
2013-12-23 10:34:44 -05:00
|
|
|
|
2014-09-24 17:29:12 -04:00
|
|
|
$('#update_thread').on('click', poll);
|
|
|
|
setInterval(timer_update, 1000);
|
|
|
|
setInterval(decrement_timer, 1000);
|
|
|
|
|
2014-09-25 00:32:53 -04:00
|
|
|
poll_interval = setTimeout(poll, poll_interval_delay);
|
2014-09-24 17:29:12 -04:00
|
|
|
timer_update();
|
2012-03-14 23:44:15 -04:00
|
|
|
});
|
|
|
|
|