1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-02 12:57:35 +01:00

auto-reload.js: format

This commit is contained in:
Zankaria 2025-01-04 21:42:01 +01:00
parent 8d56da9108
commit 209c334667

View File

@ -17,62 +17,65 @@
* *
* You must have boardlinks or else this script will not load. * You must have boardlinks or else this script will not load.
* Search for "$config['boards'] = array(" within your inc/config.php and add something similar to your instance-config.php. * Search for "$config['boards'] = array(" within your inc/config.php and add something similar to your instance-config.php.
*
*/ */
auto_reload_enabled = true; // For watch.js to interop.
auto_reload_enabled = true; // for watch.js to interop
$(document).ready(function(){ $(document).ready(function(){
if($('div.banner').length == 0) if($('div.banner').length == 0) {
return; // not index // Not index.
return;
if($(".post.op").size() != 1) }
return; //not thread page
if($(".post.op").size() != 1) {
// Not thread page.
return;
}
var countdown_interval; var countdown_interval;
// Add an update link // Add an update link.
$('.boardlist.bottom').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>"); $('.boardlist.bottom').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>");
// Grab the settings // Grab the settings.
var settings = new script_settings('auto-reload'); var settings = new script_settings('auto-reload');
var poll_interval_mindelay = settings.get('min_delay_bottom', 5000); var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
var poll_interval_maxdelay = settings.get('max_delay', 600000); var poll_interval_maxdelay = settings.get('max_delay', 600000);
var poll_interval_errordelay = settings.get('error_delay', 30000); var poll_interval_errordelay = settings.get('error_delay', 30000);
// number of ms to wait before reloading // number of ms to wait before reloading.
var poll_interval_delay = poll_interval_mindelay; var poll_interval_delay = poll_interval_mindelay;
var poll_current_time = poll_interval_delay; var poll_current_time = poll_interval_delay;
var end_of_page = false; var end_of_page = false;
var new_posts = 0; var new_posts = 0;
var first_new_post = null; var first_new_post = null;
var title = document.title; var title = document.title;
if (typeof update_title == "undefined") { if (typeof update_title == "undefined") {
var update_title = function() { var update_title = function() {
if (new_posts) { if (new_posts) {
document.title = "("+new_posts+") "+title; document.title = "("+new_posts+") "+title;
} else { } else {
document.title = title; document.title = title;
} }
}; };
} }
if (typeof add_title_collector != "undefined") 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 = poll_interval_mindelay; poll_interval_delay = poll_interval_mindelay;
} }
@ -80,7 +83,7 @@ $(document).ready(function(){
$(window).blur(function() { $(window).blur(function() {
window_active = false; window_active = false;
}); });
$('#auto_update_status').click(function() { $('#auto_update_status').click(function() {
if($("#auto_update_status").is(':checked')) { if($("#auto_update_status").is(':checked')) {
@ -89,14 +92,13 @@ $(document).ready(function(){
stop_auto_update(); stop_auto_update();
$('#update_secs').text(""); $('#update_secs').text("");
} }
}); });
var decrement_timer = function() { var decrement_timer = function() {
poll_current_time = poll_current_time - 1000; poll_current_time = poll_current_time - 1000;
$('#update_secs').text(poll_current_time/1000); $('#update_secs').text(poll_current_time/1000);
if (poll_current_time <= 0) { if (poll_current_time <= 0) {
poll(manualUpdate = false); poll(manualUpdate = false);
} }
@ -112,37 +114,37 @@ $(document).ready(function(){
update_title(); update_title();
first_new_post = null; first_new_post = null;
}; };
// automatically updates the thread after a specified delay // automatically updates the thread after a specified delay
var auto_update = function(delay) { var auto_update = function(delay) {
clearInterval(countdown_interval); clearInterval(countdown_interval);
poll_current_time = delay; poll_current_time = delay;
countdown_interval = setInterval(decrement_timer, 1000); countdown_interval = setInterval(decrement_timer, 1000);
$('#update_secs').text(poll_current_time/1000); $('#update_secs').text(poll_current_time/1000);
} }
var stop_auto_update = function() { var stop_auto_update = function() {
clearInterval(countdown_interval); clearInterval(countdown_interval);
} }
var epoch = (new Date).getTime(); var epoch = (new Date).getTime();
var epochold = epoch; var epochold = epoch;
var timeDiff = function (delay) { var timeDiff = function (delay) {
if((epoch-epochold) > delay) { if ((epoch-epochold) > delay) {
epochold = epoch = (new Date).getTime(); epochold = epoch = (new Date).getTime();
return true; return true;
}else{ } else {
epoch = (new Date).getTime(); epoch = (new Date).getTime();
return; return;
} }
} }
var poll = function(manualUpdate) { var poll = function(manualUpdate) {
stop_auto_update(); stop_auto_update();
$('#update_secs').text(_("Updating...")); $('#update_secs').text(_("Updating..."));
$.ajax({ $.ajax({
url: document.location, url: document.location,
success: function(data) { success: function(data) {
@ -168,15 +170,15 @@ $(document).ready(function(){
$(document).trigger('new_post', ele); $(document).trigger('new_post', ele);
}); });
time_loaded = Date.now(); // interop with watch.js time_loaded = Date.now(); // interop with watch.js
if ($('#auto_update_status').is(':checked')) { if ($('#auto_update_status').is(':checked')) {
// If there are no new posts, double the delay. Otherwise set it to the min. // If there are no new posts, double the delay. Otherwise set it to the min.
if(loaded_posts == 0) { if(loaded_posts == 0) {
// if the update was manual, don't increase the delay // if the update was manual, don't increase the delay
if (manualUpdate == false) { if (manualUpdate == false) {
poll_interval_delay *= 2; 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;
@ -185,7 +187,7 @@ $(document).ready(function(){
} else { } else {
poll_interval_delay = poll_interval_mindelay; poll_interval_delay = poll_interval_mindelay;
} }
auto_update(poll_interval_delay); auto_update(poll_interval_delay);
} else { } else {
// Decide the message to show if auto update is disabled // Decide the message to show if auto update is disabled
@ -210,7 +212,7 @@ $(document).ready(function(){
} else { } else {
$('#update_secs').text(_("Unknown error")); $('#update_secs').text(_("Unknown error"));
} }
// Keep trying to update // Keep trying to update
if ($('#auto_update_status').is(':checked')) { if ($('#auto_update_status').is(':checked')) {
poll_interval_delay = poll_interval_errordelay; poll_interval_delay = poll_interval_errordelay;
@ -218,14 +220,14 @@ $(document).ready(function(){
} }
} }
}); });
return false; return false;
}; };
$(window).scroll(function() { $(window).scroll(function() {
recheck_activated(); recheck_activated();
// if the newest post is not visible // If the newest post is not visible.
if($(this).scrollTop() + $(this).height() < if($(this).scrollTop() + $(this).height() <
$('div.post:last').position().top + $('div.post:last').height()) { $('div.post:last').position().top + $('div.post:last').height()) {
end_of_page = false; end_of_page = false;