Browse Source

Initial Implementation of Feature Req: on/off button for auto-reload js #1, configurable in localstorage using options menu

pull/40/head
Benjamin Southall 7 years ago
parent
commit
3a2e2f4f6e
  1. 460
      js/auto-reload.js

460
js/auto-reload.js

@ -8,7 +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> * Copyright (c) 2014-2015 Fredrick Brennan <admin@8chan.co>
* *
* Usage: * Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js'; * $config['additional_javascript'][] = 'js/jquery.min.js';
@ -17,227 +17,305 @@
* *
*/ */
au = false; // From http://stackoverflow.com/a/14035162
auto_reload_enabled = true; // for watch.js to interop $.fn.scrollStopped = function(callback) {
$(this).scroll(function(){
var self = this, $this = $(self);
if ($this.data('scrollTimeout')) {
clearTimeout($this.data('scrollTimeout'));
}
$this.data('scrollTimeout', setTimeout(callback,250,self));
});
};
function makeIcon(){ function makeIcon(mode){
if(au) return; var favicon = $("link[rel='shortcut icon']");
au = true;
$("link[rel='icon']").attr("href", "../static/favicon_au.png");
}
$(document).ready(function(){ if (!favicon.length) {
if($('div.banner').length == 0) var favicon = $('<link rel="shortcut icon"></link>').appendTo('head');
return; // not index }
if($(".post.op").size() != 1) $("link[rel='shortcut icon']").attr("href", configRoot+"static/favicon"+(mode?"-"+mode:"")+".ico");
return; //not thread page }
var countdown_interval; +function(){
var notify = false;
auto_reload_enabled = true; // for watch.js to interop
// Add an update link $(document).ready(function(){
$(".threadlinks span:last-child").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 // Adds Options panel item
var settings = new script_settings('auto-reload'); if (typeof localStorage.auto_thread_update === 'undefined') {
var poll_interval_mindelay = settings.get('min_delay_bottom', 5000); localStorage.auto_thread_update = 'true'; //default value
var poll_interval_maxdelay = settings.get('max_delay', 600000); }
var poll_interval_errordelay = settings.get('error_delay', 30000); if (window.Options && Options.get_tab('general')) {
Options.extend_tab("general", "<fieldset id='auto-update-fs'><legend>"+_("Auto update")+"</legend>"
+ ('<label id="auto-thread-update"><input type="checkbox">' + _('Auto update thread') + '</label>')
+ ('<label id="auto_thread_desktop_notifications"><input type="checkbox">' + _('Show desktop notifications when users quote me') + '</label>')
+ ('<label id="auto_thread_desktop_notifications_all"><input type="checkbox">' + _('Show desktop notifications on all replies') + '</label>')
+ '</fieldset>');
$('#auto-thread-update>input').on('click', function() {
if ($('#auto-thread-update>input').is(':checked')) {
localStorage.auto_thread_update = 'true';
} else {
localStorage.auto_thread_update = 'false';
}
});
// number of ms to wait before reloading $('#auto_thread_desktop_notifications>input,#auto_thread_desktop_notifications_all>input').on('click', function() {
var poll_interval_delay = poll_interval_mindelay; if (!("Notification" in window)) return;
var poll_current_time = poll_interval_delay;
var setting = $(this).parent().attr('id');
if ($(this).is(':checked')) {
Notification.requestPermission(function(permission){
if (permission === "granted") {
localStorage[setting] = 'true';
}
});
if (Notification.permission === "granted") {
localStorage[setting] = 'true';
}
} else {
localStorage[setting] = 'false';
}
});
var end_of_page = false; if (localStorage.auto_thread_update === 'true') {
$('#auto-thread-update>input').prop('checked', true);
}
var new_posts = 0; if (localStorage.auto_thread_desktop_notifications === 'true') {
var first_new_post = null; $('#auto_thread_desktop_notifications>input').prop('checked', true);
notify = "mention";
}
var title = document.title; if (localStorage.auto_thread_desktop_notifications_all === 'true') {
$('#auto_thread_desktop_notifications_all>input').prop('checked', true);
notify = "all";
}
}
if (typeof update_title == "undefined") { // not thread
var update_title = function() { if (active_page != 'thread')
if (new_posts) { return;
document.title = "("+new_posts+") "+title;
} else {
document.title = title;
}
};
}
if (typeof add_title_collector != "undefined") var countdown_interval;
add_title_collector(function(){
return new_posts;
});
var window_active = true; // Add an update link
$(window).focus(function() { $(".threadlinks span:last-child").append("<span id='updater'><a href='#' id='update_thread'>["+_("Update")+"]</a> (<input type='checkbox' id='auto_update_status'> "+_("Auto")+") <span id='update_secs'></span></span>");
window_active = true;
recheck_activated();
// Reset the delay if needed // Set the updater checkbox according to user setting
if(settings.get('reset_focus', true)) { if (localStorage.auto_thread_update === 'true') {
poll_interval_delay = poll_interval_mindelay; $('#auto_update_status').prop('checked', true);
} }
});
$(window).blur(function() {
window_active = false;
});
$('#auto_update_status').click(function() { // Grab the settings
if($("#auto_update_status").is(':checked')) { var settings = new script_settings('auto-reload');
auto_update(poll_interval_mindelay); var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
} else { var poll_interval_maxdelay = settings.get('max_delay', 600000);
stop_auto_update(); var poll_interval_errordelay = settings.get('error_delay', 30000);
$('#update_secs').text("");
// number of ms to wait before reloading
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() {
if (new_posts) {
document.title = "("+new_posts+") "+title;
} else {
document.title = title;
makeIcon(false);
}
};
} }
}); if (typeof add_title_collector != "undefined")
add_title_collector(function(){
return new_posts;
});
var window_active = true;
$(window).focus(function() {
window_active = true;
recheck_activated();
var decrement_timer = function() { // Reset the delay if needed
poll_current_time = poll_current_time - 1000; if(settings.get('reset_focus', true)) {
$('#update_secs').text(poll_current_time/1000); 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("");
}
if (poll_current_time <= 0) { });
poll(manualUpdate = false);
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() { var recheck_activated = function(end_of_page) {
if (new_posts && window_active && if (typeof end_of_page == "undefined") var end_of_page = false;
$(window).scrollTop() + $(window).height() >= if (end_of_page || (new_posts && window_active &&
$('footer').position().top) { $(window).scrollTop() + $(window).height() >=
$('div.boardlist.bottom').position().top)) {
new_posts = 0; 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);
} }
update_title();
first_new_post = null; var epoch = (new Date).getTime();
}; var epochold = epoch;
// automatically updates the thread after a specified delay var timeDiff = function (delay) {
var auto_update = function(delay) { if((epoch-epochold) > delay) {
clearInterval(countdown_interval); epochold = epoch = (new Date).getTime();
return true;
poll_current_time = delay; }else{
countdown_interval = setInterval(decrement_timer, 1000); epoch = (new Date).getTime();
$('#update_secs').text(poll_current_time/1000); return;
} }
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) {
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) { var loaded_posts = 0; // the number of new posts loaded in this update
var loaded_posts = 0; // the number of new posts loaded in this update $(data).find('div.post.reply').each(function() {
$(data).find('div.postcontainer').each(function() { var id = $(this).attr('id');
var id = $(this).attr('id').substring(2); if($('#' + id).length == 0) {
if($('#' + id).length == 0) { if (!new_posts) {
if (!new_posts) { first_new_post = this;
first_new_post = this; makeIcon('reply');
makeIcon(); if (notify === "all" || (notify === "mention" && $(this).find('.own_post').length)) {
var body = $(this).children('.body').html().replace(/<br\s*[\/]?>/gi, "\n");
var n = new Notification("New reply to "+$('title').text(), {body: $('<div/>').html(body).text()});
}
}
$(this).insertAfter($('div.post:not(.post-hover):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
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(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
else
$('#update_secs').text(_("No new posts found"));
}
},
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
return;
} else {
$('#update_secs').text("Error: "+error_text);
}
} else if (status_text) {
$('#update_secs').text(_("Error: ")+status_text);
} else {
$('#update_secs').text(_("Unknown error"));
}
// Keep trying to update
if ($('#auto_update_status').is(':checked')) {
poll_interval_delay = poll_interval_errordelay;
auto_update(poll_interval_delay);
}
} }
$(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
return false;
};
if ($('#auto_update_status').is(':checked')) {
// If there are no new posts, double the delay. Otherwise set it to the min. $(window).scrollStopped(function() {
if(loaded_posts == 0) { // if the newest post is not visible
// if the update was manual, don't increase the delay if($(this).scrollTop() + $(this).height() <
if (manualUpdate == false) { $('div.post:last').position().top + $('div.post:last').height()) {
poll_interval_delay *= 2; end_of_page = false;
// 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(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
else
$('#update_secs').text(_("No new posts found"));
}
},
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
return;
} else {
$('#update_secs').text("Error: "+error_text);
}
} else if (status_text) {
$('#update_secs').text(_("Error: ")+status_text);
} else { } else {
$('#update_secs').text(_("Unknown error")); if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
} poll(manualUpdate = true);
}
// Keep trying to update end_of_page = true;
if ($('#auto_update_status').is(':checked')) {
poll_interval_delay = poll_interval_errordelay;
auto_update(poll_interval_delay);
} }
} recheck_activated(end_of_page);
}); });
return false; $('#update_thread').on('click', function() { poll(manualUpdate = true); 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);
if($("#auto_update_status").is(':checked')) { }
auto_update(poll_interval_delay);
}
}); });
}();

Loading…
Cancel
Save