Browse Source

Use 8chan's auto-reload.js

pull/40/head
Michael Walker 10 years ago
parent
commit
4f0be0d3c2
  1. 285
      js/auto-reload.js

285
js/auto-reload.js

@ -8,6 +8,7 @@
* Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org> * Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net> * Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
* Copyright (c) 2013 undido <firekid109@hotmail.com> * Copyright (c) 2013 undido <firekid109@hotmail.com>
* Copyright (c) 2014 Fredrick Brennan <admin@8chan.co>
* *
* Usage: * Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/jquery.min.js';
@ -16,121 +17,227 @@
* *
*/ */
au = false;
auto_reload_enabled = true; // for watch.js to interop auto_reload_enabled = true; // for watch.js to interop
function makeIcon(){
if(au) return;
au = true;
$("link[rel='icon']").attr("href", "../static/favicon_au.png");
}
$(document).ready(function(){ $(document).ready(function(){
if($('div.banner').length == 0) if($('div.banner').length == 0)
return; // not index return; // not index
if($(".post.op").size() != 1) if($(".post.op").size() != 1)
return; //not thread page return; //not thread page
var poll_interval;
var countdown_interval;
// Grab the settings // Add an update link
var settings = new script_settings('auto-reload'); $("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>");
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);
// number of ms to wait before reloading // Grab the settings
var poll_interval_delay = poll_interval_mindelay_bottom; 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);
var end_of_page = false; // number of ms to wait before reloading
var poll_interval_delay = poll_interval_mindelay;
var poll_current_time = poll_interval_delay;
var new_posts = 0; var end_of_page = false;
var first_new_post = null;
if (typeof update_title == "undefined") { var new_posts = 0;
var update_title = function() { }; var first_new_post = null;
}
if (typeof add_title_collector != "undefined") 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")
add_title_collector(function(){ add_title_collector(function(){
return new_posts; return new_posts;
}); });
var window_active = true; var window_active = true;
$(window).focus(function() { $(window).focus(function() {
window_active = true; window_active = true;
recheck_activated(); recheck_activated();
// Reset the delay if needed // Reset the delay if needed
if(settings.get('reset_focus', true)) { if(settings.get('reset_focus', true)) {
poll_interval_delay = end_of_page poll_interval_delay = poll_interval_mindelay;
? poll_interval_mindelay_bottom }
: poll_interval_mindelay_top; });
} $(window).blur(function() {
}); window_active = false;
$(window).blur(function() { });
window_active = false;
});
$('#auto_update_status').click(function() {
var recheck_activated = function() { if($("#auto_update_status").is(':checked')) {
if (new_posts && window_active && auto_update(poll_interval_mindelay);
$(window).scrollTop() + $(window).height() >= } else {
$(first_new_post).position().top) { stop_auto_update();
$('#update_secs').text("");
}
new_posts = 0; });
}
update_title();
}; 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);
}
}
var poll = function() { var recheck_activated = function() {
$.ajax({ if (new_posts && window_active &&
url: document.location, $(window).scrollTop() + $(window).height() >=
success: function(data) { $('footer').position().top) {
$(data).find('div.post.reply').each(function() {
var id = $(this).attr('id'); new_posts = 0;
if($('#' + id).length == 0) { }
if (!new_posts) { update_title();
first_new_post = this; first_new_post = null;
} };
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
new_posts++; // automatically updates the thread after a specified delay
$(document).trigger('new_post', this); var auto_update = function(delay) {
recheck_activated(); clearInterval(countdown_interval);
}
}); poll_current_time = delay;
time_loaded = Date.now(); // interop with watch.js 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();
} }
$(this).insertAfter($('div.postcontainer:last').next()).after('<br class="clear">');
new_posts++;
loaded_posts++;
$(document).trigger('new_post', this);
recheck_activated();
}
}); });
time_loaded = Date.now(); // interop with watch.js
clearTimeout(poll_interval);
// If there are no new posts, double the delay. Otherwise set it to the min. if ($('#auto_update_status').is(':checked')) {
if(new_posts == 0) { // If there are no new posts, double the delay. Otherwise set it to the min.
poll_interval_delay *= 2; if(loaded_posts == 0) {
// if the update was manual, don't increase the delay
if (manualUpdate == false) {
poll_interval_delay *= 2;
// Don't increase the delay beyond the maximum // Don't increase the delay beyond the maximum
if(poll_interval_delay > poll_interval_maxdelay) { if(poll_interval_delay > poll_interval_maxdelay) {
poll_interval_delay = poll_interval_maxdelay; poll_interval_delay = poll_interval_maxdelay;
}
} }
} else {
poll_interval_delay = poll_interval_mindelay;
}
auto_update(poll_interval_delay);
} else { } else {
poll_interval_delay = end_of_page // Decide the message to show if auto update is disabled
? poll_interval_mindelay_bottom if (loaded_posts > 0)
: poll_interval_mindelay_top; $('#update_secs').text(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
else
$('#update_secs').text(_("No new posts found"));
} }
},
poll_interval = setTimeout(poll, poll_interval_delay); error: function(xhr, status_text, error_text) {
}; if (status_text == "error") {
if (error_text == "Not Found") {
$(window).scroll(function() { $('#update_secs').text(_("Thread deleted or pruned"));
recheck_activated(); $('#auto_update_status').prop('checked', false);
$('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
if($(this).scrollTop() + $(this).height() <
$('div.post:last').position().top + $('div.post:last').height()) {
end_of_page = false;
return; return;
} else {
$('#update_secs').text("Error: "+error_text);
}
} else if (status_text) {
$('#update_secs').text(_("Error: ")+status_text);
} else {
$('#update_secs').text(_("Unknown error"));
} }
clearTimeout(poll_interval);
poll_interval = setTimeout(poll, poll_interval_shortdelay);
end_of_page = true;
}).trigger('scroll');
poll_interval = setTimeout(poll, poll_interval_delay); // Keep trying to update
}); if ($('#auto_update_status').is(':checked')) {
poll_interval_delay = poll_interval_errordelay;
auto_update(poll_interval_delay);
}
}
});
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);
}
});

Loading…
Cancel
Save