From 128adfd999f99ad039da0605373be8ada401741a Mon Sep 17 00:00:00 2001 From: topkek Date: Sun, 28 Sep 2014 00:16:16 +0100 Subject: [PATCH 1/4] Update auto-reload.js --- js/auto-reload.js | 136 ++++++++++++++++++++++++++++++---------------- 1 file changed, 90 insertions(+), 46 deletions(-) diff --git a/js/auto-reload.js b/js/auto-reload.js index 663b495e..e46fce9a 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -32,31 +32,37 @@ $(document).ready(function(){ return; // not index if($(".post.op").size() != 1) - return; //not thread page + return; //not thread page - var poll_interval; + var countdown_interval; // Add an update link - $('.boardlist.bottom').prev().after("["+_("Update thread")+"] ()"); + $('.boardlist.bottom').prev().after("["+_("Update")+"] ( Auto) "); // 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_mindelay_bottom = settings.get('min_delay_bottom', 5000); 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 - var poll_interval_delay = poll_interval_mindelay_bottom; + var poll_interval_delay = poll_interval_mindelay; var poll_current_time = poll_interval_delay; var end_of_page = false; var new_posts = 0; var first_new_post = null; + + var title = document.title; if (typeof update_title == "undefined") { - var update_title = function() { }; + var update_title = function() { + if (new_posts) { + document.title = "("+new_posts+") "+title; + } else { + document.title = title; + } + }; } if (typeof add_title_collector != "undefined") @@ -71,37 +77,67 @@ $(document).ready(function(){ // 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; + poll_interval_delay = poll_interval_mindelay; } }); $(window).blur(function() { window_active = false; }); - var timer_update = function() { - $('#update_secs').text(poll_current_time/1000); - } + + $('#auto_update_status').click(function() { + if($("#auto_update_status").is(':checked')) { + auto_update(poll_interval_mindelay); + } else { + stop_auto_update(); + $('#update_secs').text(""); + } + + }); + 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 recheck_activated = function() { if (new_posts && window_active && $(window).scrollTop() + $(window).height() >= - $(first_new_post).position().top) { + $('div.boardlist.bottom').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); - var poll = function() { + 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 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.post.reply').each(function() { var id = $(this).attr('id'); if($('#' + id).length == 0) { @@ -111,56 +147,64 @@ $(document).ready(function(){ } $(this).insertAfter($('div.post:last').next()).after('
'); new_posts++; + loaded_posts++; $(document).trigger('new_post', this); recheck_activated(); } }); time_loaded = Date.now(); // interop with watch.js + + + 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; + + // Don't increase the delay beyond the maximum + if(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 { + // Decide the message to show if auto update is disabled + if (loaded_posts > 0) + $('#update_secs').text("Thread updated with "+loaded_posts+" new post(s)"); + else + $('#update_secs').text("No new posts found"); + } } }); - clearTimeout(poll_interval); - - // If there are no new posts, double the delay. Otherwise set it to the min. - if(new_posts == 0) { - poll_interval_delay *= 2; - - // Don't increase the delay beyond the maximum - if(poll_interval_delay > poll_interval_maxdelay) { - poll_interval_delay = poll_interval_maxdelay; - } - } else { - poll_interval_delay = end_of_page - ? poll_interval_mindelay_bottom - : poll_interval_mindelay_top; - } - - poll_interval = setTimeout(poll, poll_interval_delay); - poll_current_time = 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')) { + poll(manualUpdate = true); + } + end_of_page = true; } - - clearTimeout(poll_interval); - poll_interval = setTimeout(poll, poll_interval_shortdelay); - poll_current_time = poll_interval_shortdelay; - end_of_page = true; }).trigger('scroll'); - $('#update_thread').on('click', poll); - setInterval(timer_update, 1000); - setInterval(decrement_timer, 1000); + $('#update_thread').on('click', function() { poll(manualUpdate = true); return false; }); - poll_interval = setTimeout(poll, poll_interval_delay); - timer_update(); + if($("#auto_update_status").is(':checked')) { + auto_update(poll_interval_delay); + } }); From e4c68b83f5c2c19953e156b06fa692e9cafc80c9 Mon Sep 17 00:00:00 2001 From: topkek Date: Sun, 28 Sep 2014 00:17:16 +0100 Subject: [PATCH 2/4] Update settings.js --- js/settings.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/js/settings.js b/js/settings.js index 02d5658d..64de9624 100644 --- a/js/settings.js +++ b/js/settings.js @@ -56,12 +56,8 @@ tb_settings['wpaint'] = { tb_settings['auto-reload'] = { // Minimum delay before reloading the page when at the bottom min_delay_bottom: 3000, - // Minimum delay before reloading the page when not at the bottom - min_delay_top: 10000, // Maximum delay before reloading the page max_delay: 600000, - //Delay to wait before reloading when the user scrolls to the bottom - quick_delay: 100, // Reset the delay to the minimum upon focussing the window. reset_focus: true }; From ea318be4e8b80f0618aa397ec617ff93d61fcee3 Mon Sep 17 00:00:00 2001 From: topkek Date: Sun, 28 Sep 2014 00:27:28 +0100 Subject: [PATCH 3/4] Update settings.js --- js/settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/settings.js b/js/settings.js index 64de9624..fcc8f508 100644 --- a/js/settings.js +++ b/js/settings.js @@ -55,7 +55,7 @@ tb_settings['wpaint'] = { // auto-reload.js tb_settings['auto-reload'] = { // Minimum delay before reloading the page when at the bottom - min_delay_bottom: 3000, + min_delay_bottom: 5000, // Maximum delay before reloading the page max_delay: 600000, // Reset the delay to the minimum upon focussing the window. From cd227d6509405462ed646f8603587e47bf454316 Mon Sep 17 00:00:00 2001 From: topkek Date: Sun, 28 Sep 2014 00:40:00 +0100 Subject: [PATCH 4/4] Update auto-reload.js --- js/auto-reload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/auto-reload.js b/js/auto-reload.js index e46fce9a..de4326a4 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -41,7 +41,7 @@ $(document).ready(function(){ // Grab the settings var settings = new script_settings('auto-reload'); - var poll_interval_mindelay_bottom = 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); // number of ms to wait before reloading