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-11-12 19:03:02 -05: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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
au = false;
|
2013-12-28 19:10:35 -05:00
|
|
|
auto_reload_enabled = true; // for watch.js to interop
|
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
function makeIcon(){
|
|
|
|
if(au) return;
|
|
|
|
au = true;
|
|
|
|
$("link[rel='icon']").attr("href", "../static/favicon_au.png");
|
|
|
|
}
|
|
|
|
|
2012-03-14 23:44:15 -04:00
|
|
|
$(document).ready(function(){
|
2014-11-12 19:03:02 -05:00
|
|
|
if($('div.banner').length == 0)
|
|
|
|
return; // not index
|
|
|
|
|
|
|
|
if($(".post.op").size() != 1)
|
2013-03-19 23:56:59 -04:00
|
|
|
return; //not thread page
|
2013-12-23 10:34:44 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
var countdown_interval;
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
// Add an update link
|
|
|
|
$("footer").prev().after("<span id='updater'><a href='#' id='update_thread' style='padding-left:10px'>["+_("Update")+"]</a> (<input type='checkbox' id='auto_update_status' checked> "+_("Auto")+") <span id='update_secs'></span></span>");
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
// Grab the settings
|
|
|
|
var settings = new script_settings('auto-reload');
|
|
|
|
var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
|
|
|
|
var poll_interval_maxdelay = settings.get('max_delay', 600000);
|
|
|
|
var poll_interval_errordelay = settings.get('error_delay', 30000);
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
// number of ms to wait before reloading
|
|
|
|
var poll_interval_delay = poll_interval_mindelay;
|
|
|
|
var poll_current_time = poll_interval_delay;
|
2013-12-23 11:31:47 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
var end_of_page = false;
|
2013-12-28 19:10:35 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
var new_posts = 0;
|
|
|
|
var first_new_post = null;
|
2013-12-28 19:10:35 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
var title = document.title;
|
|
|
|
|
|
|
|
if (typeof update_title == "undefined") {
|
|
|
|
var update_title = function() {
|
|
|
|
if (new_posts) {
|
|
|
|
document.title = "("+new_posts+") "+title;
|
|
|
|
} else {
|
|
|
|
document.title = title;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof add_title_collector != "undefined")
|
2013-12-28 19:10:35 -05:00
|
|
|
add_title_collector(function(){
|
2014-11-12 19:03:02 -05:00
|
|
|
return new_posts;
|
2013-12-28 19:10:35 -05:00
|
|
|
});
|
2013-12-23 11:31:47 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
var window_active = true;
|
|
|
|
$(window).focus(function() {
|
|
|
|
window_active = true;
|
|
|
|
recheck_activated();
|
2014-05-05 15:54:40 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
// Reset the delay if needed
|
|
|
|
if(settings.get('reset_focus', true)) {
|
|
|
|
poll_interval_delay = poll_interval_mindelay;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$(window).blur(function() {
|
|
|
|
window_active = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$('#auto_update_status').click(function() {
|
|
|
|
if($("#auto_update_status").is(':checked')) {
|
|
|
|
auto_update(poll_interval_mindelay);
|
|
|
|
} else {
|
|
|
|
stop_auto_update();
|
|
|
|
$('#update_secs').text("");
|
|
|
|
}
|
2013-12-23 11:31:47 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var decrement_timer = function() {
|
|
|
|
poll_current_time = poll_current_time - 1000;
|
|
|
|
$('#update_secs').text(poll_current_time/1000);
|
|
|
|
|
|
|
|
if (poll_current_time <= 0) {
|
|
|
|
poll(manualUpdate = false);
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 11:31:47 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
var recheck_activated = function() {
|
|
|
|
if (new_posts && window_active &&
|
|
|
|
$(window).scrollTop() + $(window).height() >=
|
|
|
|
$('footer').position().top) {
|
|
|
|
|
|
|
|
new_posts = 0;
|
|
|
|
}
|
|
|
|
update_title();
|
|
|
|
first_new_post = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// automatically updates the thread after a specified delay
|
|
|
|
var auto_update = function(delay) {
|
|
|
|
clearInterval(countdown_interval);
|
|
|
|
|
|
|
|
poll_current_time = delay;
|
|
|
|
countdown_interval = setInterval(decrement_timer, 1000);
|
|
|
|
$('#update_secs').text(poll_current_time/1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stop_auto_update = function() {
|
|
|
|
clearInterval(countdown_interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
var epoch = (new Date).getTime();
|
|
|
|
var epochold = epoch;
|
|
|
|
|
|
|
|
var timeDiff = function (delay) {
|
|
|
|
if((epoch-epochold) > delay) {
|
|
|
|
epochold = epoch = (new Date).getTime();
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
epoch = (new Date).getTime();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var poll = function(manualUpdate) {
|
|
|
|
stop_auto_update();
|
|
|
|
$('#update_secs').text(_("Updating..."));
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: document.location,
|
|
|
|
success: function(data) {
|
|
|
|
var loaded_posts = 0; // the number of new posts loaded in this update
|
|
|
|
$(data).find('div.postcontainer').each(function() {
|
|
|
|
var id = $(this).attr('id').substring(2);
|
|
|
|
if($('#' + id).length == 0) {
|
|
|
|
if (!new_posts) {
|
|
|
|
first_new_post = this;
|
|
|
|
makeIcon();
|
2012-03-14 23:44:15 -04:00
|
|
|
}
|
2014-11-12 19:03:02 -05:00
|
|
|
$(this).insertAfter($('div.postcontainer:last').next()).after('<br class="clear">');
|
|
|
|
new_posts++;
|
|
|
|
loaded_posts++;
|
|
|
|
$(document).trigger('new_post', this);
|
|
|
|
recheck_activated();
|
|
|
|
}
|
2012-03-14 23:44:15 -04:00
|
|
|
});
|
2014-11-12 19:03:02 -05:00
|
|
|
time_loaded = Date.now(); // interop with watch.js
|
|
|
|
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
if ($('#auto_update_status').is(':checked')) {
|
|
|
|
// If there are no new posts, double the delay. Otherwise set it to the min.
|
|
|
|
if(loaded_posts == 0) {
|
|
|
|
// if the update was manual, don't increase the delay
|
|
|
|
if (manualUpdate == false) {
|
|
|
|
poll_interval_delay *= 2;
|
2014-05-05 14:30:59 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
// Don't increase the delay beyond the maximum
|
|
|
|
if(poll_interval_delay > poll_interval_maxdelay) {
|
2014-05-05 14:30:59 -04:00
|
|
|
poll_interval_delay = poll_interval_maxdelay;
|
2014-11-12 19:03:02 -05:00
|
|
|
}
|
2014-05-05 14:30:59 -04:00
|
|
|
}
|
2014-11-12 19:03:02 -05:00
|
|
|
} else {
|
|
|
|
poll_interval_delay = poll_interval_mindelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_update(poll_interval_delay);
|
2014-05-05 14:29:19 -04:00
|
|
|
} else {
|
2014-11-12 19:03:02 -05:00
|
|
|
// Decide the message to show if auto update is disabled
|
|
|
|
if (loaded_posts > 0)
|
|
|
|
$('#update_secs').text(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
|
|
|
|
else
|
|
|
|
$('#update_secs').text(_("No new posts found"));
|
2014-05-05 14:29:19 -04:00
|
|
|
}
|
2014-11-12 19:03:02 -05:00
|
|
|
},
|
|
|
|
error: function(xhr, status_text, error_text) {
|
|
|
|
if (status_text == "error") {
|
|
|
|
if (error_text == "Not Found") {
|
|
|
|
$('#update_secs').text(_("Thread deleted or pruned"));
|
|
|
|
$('#auto_update_status').prop('checked', false);
|
|
|
|
$('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
|
2012-03-15 05:40:52 -04:00
|
|
|
return;
|
2014-11-12 19:03:02 -05:00
|
|
|
} else {
|
|
|
|
$('#update_secs').text("Error: "+error_text);
|
|
|
|
}
|
|
|
|
} else if (status_text) {
|
|
|
|
$('#update_secs').text(_("Error: ")+status_text);
|
|
|
|
} else {
|
|
|
|
$('#update_secs').text(_("Unknown error"));
|
2012-03-15 05:40:52 -04:00
|
|
|
}
|
2013-12-23 10:34:44 -05:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
// Keep trying to update
|
|
|
|
if ($('#auto_update_status').is(':checked')) {
|
|
|
|
poll_interval_delay = poll_interval_errordelay;
|
|
|
|
auto_update(poll_interval_delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-03-14 23:44:15 -04:00
|
|
|
|
2014-11-12 19:03:02 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$(window).scroll(function() {
|
|
|
|
recheck_activated();
|
|
|
|
|
|
|
|
// if the newest post is not visible
|
|
|
|
if($(this).scrollTop() + $(this).height() <
|
|
|
|
$('div.post:last').position().top + $('div.post:last').height()) {
|
|
|
|
end_of_page = false;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
|
|
|
|
poll(manualUpdate = true);
|
|
|
|
}
|
|
|
|
end_of_page = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
|
|
|
|
|
|
|
|
if($("#auto_update_status").is(':checked')) {
|
|
|
|
auto_update(poll_interval_delay);
|
|
|
|
}
|
|
|
|
});
|