2012-03-15 04:44:15 +01:00
|
|
|
/*
|
|
|
|
* auto-reload.js
|
2012-03-31 02:13:11 +02:00
|
|
|
* https://github.com/savetheinternet/Tinyboard/blob/master/js/auto-reload.js
|
2012-03-15 04:44:15 +01:00
|
|
|
*
|
|
|
|
* Brings AJAX to Tinyboard.
|
|
|
|
*
|
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
|
2014-01-19 13:40:29 +01:00
|
|
|
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
|
2014-01-19 14:27:24 +01:00
|
|
|
* Copyright (c) 2013 undido <firekid109@hotmail.com>
|
2015-03-13 12:18:56 +01:00
|
|
|
* Copyright (c) 2014-2015 Fredrick Brennan <admin@8chan.co>
|
2012-03-15 04:44:15 +01:00
|
|
|
*
|
|
|
|
* Usage:
|
2012-03-15 06:19:26 +01:00
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
2013-12-29 01:10:35 +01:00
|
|
|
* //$config['additional_javascript'][] = 'js/titlebar-notifications.js';
|
2012-03-15 06:19:26 +01:00
|
|
|
* $config['additional_javascript'][] = 'js/auto-reload.js';
|
2012-03-15 04:44:15 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-03-13 12:18:56 +01:00
|
|
|
function makeIcon(mode){
|
|
|
|
var favicon = $("link[rel='shortcut icon']");
|
2014-09-25 03:23:15 +02:00
|
|
|
|
2015-03-13 12:18:56 +01:00
|
|
|
if (!favicon.length) {
|
|
|
|
var favicon = $('<link rel="shortcut icon"></link>').appendTo('head');
|
|
|
|
}
|
2013-12-29 01:10:35 +01:00
|
|
|
|
2015-03-13 12:18:56 +01:00
|
|
|
$("link[rel='shortcut icon']").attr("href", configRoot+"static/favicon"+(mode?"-"+mode:"")+".ico");
|
2014-09-25 03:23:15 +02:00
|
|
|
}
|
|
|
|
|
2015-03-13 12:18:56 +01:00
|
|
|
+function(){
|
|
|
|
var notify = false;
|
|
|
|
auto_reload_enabled = true; // for watch.js to interop
|
|
|
|
|
2012-03-15 04:44:15 +01:00
|
|
|
$(document).ready(function(){
|
2015-01-23 16:26:41 +01:00
|
|
|
|
|
|
|
// Adds Options panel item
|
|
|
|
if (typeof localStorage.auto_thread_update === 'undefined') {
|
|
|
|
localStorage.auto_thread_update = 'true'; //default value
|
|
|
|
}
|
|
|
|
if (window.Options && Options.get_tab('general')) {
|
2015-03-13 12:18:56 +01:00
|
|
|
Options.extend_tab("general", "<fieldset><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>');
|
|
|
|
|
2015-01-23 16:26:41 +01:00
|
|
|
$('#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';
|
|
|
|
}
|
|
|
|
});
|
2015-03-13 12:18:56 +01:00
|
|
|
|
|
|
|
$('#auto_thread_desktop_notifications>input,#auto_thread_desktop_notifications_all>input').on('click', function() {
|
|
|
|
if (!("Notification" in window)) return;
|
|
|
|
|
|
|
|
var setting = $(this).parent().attr('id');
|
|
|
|
|
|
|
|
if ($(this).is(':checked')) {
|
|
|
|
Notification.requestPermission();
|
|
|
|
if (Notification.permission === "granted") {
|
|
|
|
localStorage[setting] = 'true';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
localStorage[setting] = 'false';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-23 16:26:41 +01:00
|
|
|
if (localStorage.auto_thread_update === 'true') {
|
2015-03-08 02:37:29 +01:00
|
|
|
$('#auto-thread-update>input').prop('checked', true);
|
2015-01-23 16:26:41 +01:00
|
|
|
}
|
2015-03-13 12:18:56 +01:00
|
|
|
|
|
|
|
if (localStorage.auto_thread_desktop_notifications === 'true') {
|
|
|
|
$('#auto_thread_desktop_notifications>input').prop('checked', true);
|
|
|
|
notify = "mention";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localStorage.auto_thread_desktop_notifications_all === 'true') {
|
|
|
|
$('#auto_thread_desktop_notifications_all>input').prop('checked', true);
|
|
|
|
notify = "all";
|
|
|
|
}
|
2015-01-23 16:26:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-08 02:57:49 +01:00
|
|
|
// not thread
|
|
|
|
if (active_page != 'thread')
|
|
|
|
return;
|
|
|
|
|
|
|
|
var countdown_interval;
|
|
|
|
|
|
|
|
// 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'> "+_("Auto")+") <span id='update_secs'></span></span>");
|
|
|
|
|
2015-01-23 16:26:41 +01:00
|
|
|
// Set the updater checkbox according to user setting
|
|
|
|
if (localStorage.auto_thread_update === 'true') {
|
|
|
|
$('#auto_update_status').prop('checked', true);
|
|
|
|
}
|
2014-05-05 20:28:07 +02:00
|
|
|
|
2014-05-05 21:51:51 +02:00
|
|
|
// Grab the settings
|
|
|
|
var settings = new script_settings('auto-reload');
|
2014-09-28 01:40:00 +02:00
|
|
|
var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
|
2014-05-05 21:51:51 +02:00
|
|
|
var poll_interval_maxdelay = settings.get('max_delay', 600000);
|
2014-09-28 21:23:30 +02:00
|
|
|
var poll_interval_errordelay = settings.get('error_delay', 30000);
|
2014-05-05 20:28:07 +02:00
|
|
|
|
2014-05-05 21:51:51 +02:00
|
|
|
// number of ms to wait before reloading
|
2014-09-28 01:16:16 +02:00
|
|
|
var poll_interval_delay = poll_interval_mindelay;
|
2014-09-24 23:29:12 +02:00
|
|
|
var poll_current_time = poll_interval_delay;
|
2014-05-05 20:28:07 +02:00
|
|
|
|
2013-12-23 16:34:44 +01:00
|
|
|
var end_of_page = false;
|
2013-12-23 17:31:47 +01:00
|
|
|
|
2015-01-23 16:26:41 +01:00
|
|
|
var new_posts = 0;
|
2013-12-23 17:31:47 +01:00
|
|
|
var first_new_post = null;
|
2014-09-28 01:16:16 +02:00
|
|
|
|
|
|
|
var title = document.title;
|
2013-12-29 01:10:35 +01:00
|
|
|
|
|
|
|
if (typeof update_title == "undefined") {
|
2015-01-23 16:26:41 +01:00
|
|
|
var update_title = function() {
|
|
|
|
if (new_posts) {
|
|
|
|
document.title = "("+new_posts+") "+title;
|
|
|
|
} else {
|
|
|
|
document.title = title;
|
2015-03-13 12:18:56 +01:00
|
|
|
makeIcon(false);
|
2015-01-23 16:26:41 +01:00
|
|
|
}
|
|
|
|
};
|
2013-12-29 01:10:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof add_title_collector != "undefined")
|
|
|
|
add_title_collector(function(){
|
|
|
|
return new_posts;
|
|
|
|
});
|
2013-12-23 17:31:47 +01:00
|
|
|
|
|
|
|
var window_active = true;
|
|
|
|
$(window).focus(function() {
|
|
|
|
window_active = true;
|
|
|
|
recheck_activated();
|
2014-05-05 21:54:40 +02:00
|
|
|
|
|
|
|
// Reset the delay if needed
|
|
|
|
if(settings.get('reset_focus', true)) {
|
2014-09-28 01:16:16 +02:00
|
|
|
poll_interval_delay = poll_interval_mindelay;
|
2014-05-05 21:54:40 +02:00
|
|
|
}
|
2013-12-23 17:31:47 +01:00
|
|
|
});
|
|
|
|
$(window).blur(function() {
|
|
|
|
window_active = false;
|
|
|
|
});
|
2012-03-15 10:40:52 +01:00
|
|
|
|
2014-09-28 01:16:16 +02:00
|
|
|
|
|
|
|
$('#auto_update_status').click(function() {
|
|
|
|
if($("#auto_update_status").is(':checked')) {
|
|
|
|
auto_update(poll_interval_mindelay);
|
|
|
|
} else {
|
|
|
|
stop_auto_update();
|
|
|
|
$('#update_secs').text("");
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-09-24 23:29:12 +02:00
|
|
|
|
|
|
|
var decrement_timer = function() {
|
|
|
|
poll_current_time = poll_current_time - 1000;
|
2014-09-28 01:16:16 +02:00
|
|
|
$('#update_secs').text(poll_current_time/1000);
|
|
|
|
|
|
|
|
if (poll_current_time <= 0) {
|
|
|
|
poll(manualUpdate = false);
|
|
|
|
}
|
2014-09-24 23:29:12 +02:00
|
|
|
}
|
|
|
|
|
2013-12-23 17:31:47 +01:00
|
|
|
var recheck_activated = function() {
|
|
|
|
if (new_posts && window_active &&
|
|
|
|
$(window).scrollTop() + $(window).height() >=
|
2014-09-28 01:16:16 +02:00
|
|
|
$('div.boardlist.bottom').position().top) {
|
2013-12-23 17:31:47 +01:00
|
|
|
|
|
|
|
new_posts = 0;
|
|
|
|
}
|
|
|
|
update_title();
|
2014-09-28 01:16:16 +02:00
|
|
|
first_new_post = null;
|
2013-12-23 17:31:47 +01:00
|
|
|
};
|
2014-09-28 01:16:16 +02:00
|
|
|
|
|
|
|
// automatically updates the thread after a specified delay
|
|
|
|
var auto_update = function(delay) {
|
|
|
|
clearInterval(countdown_interval);
|
2013-12-23 17:31:47 +01:00
|
|
|
|
2014-09-28 01:16:16 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:26:41 +01:00
|
|
|
var epoch = (new Date).getTime();
|
|
|
|
var epochold = epoch;
|
|
|
|
|
2014-09-30 05:02:57 +02:00
|
|
|
var timeDiff = function (delay) {
|
|
|
|
if((epoch-epochold) > delay) {
|
|
|
|
epochold = epoch = (new Date).getTime();
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
epoch = (new Date).getTime();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 01:16:16 +02:00
|
|
|
|
|
|
|
var poll = function(manualUpdate) {
|
|
|
|
stop_auto_update();
|
2014-10-07 05:40:37 +02:00
|
|
|
$('#update_secs').text(_("Updating..."));
|
2014-09-28 01:16:16 +02:00
|
|
|
|
2012-03-15 04:44:15 +01:00
|
|
|
$.ajax({
|
|
|
|
url: document.location,
|
|
|
|
success: function(data) {
|
2014-09-28 01:16:16 +02:00
|
|
|
var loaded_posts = 0; // the number of new posts loaded in this update
|
2012-03-15 04:44:15 +01:00
|
|
|
$(data).find('div.post.reply').each(function() {
|
|
|
|
var id = $(this).attr('id');
|
|
|
|
if($('#' + id).length == 0) {
|
2013-12-23 17:31:47 +01:00
|
|
|
if (!new_posts) {
|
|
|
|
first_new_post = this;
|
2015-03-13 12:18:56 +01:00
|
|
|
makeIcon('reply');
|
|
|
|
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()});
|
|
|
|
}
|
2013-12-23 17:31:47 +01:00
|
|
|
}
|
2014-11-09 01:57:21 +01:00
|
|
|
$(this).insertAfter($('div.post:not(.post-hover):last').next()).after('<br class="clear">');
|
2013-12-23 17:31:47 +01:00
|
|
|
new_posts++;
|
2014-09-28 01:16:16 +02:00
|
|
|
loaded_posts++;
|
2012-03-18 13:55:01 +01:00
|
|
|
$(document).trigger('new_post', this);
|
2013-12-23 17:31:47 +01:00
|
|
|
recheck_activated();
|
2012-03-15 04:44:15 +01:00
|
|
|
}
|
|
|
|
});
|
2013-12-29 01:10:35 +01:00
|
|
|
time_loaded = Date.now(); // interop with watch.js
|
2014-09-28 01:16:16 +02: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;
|
|
|
|
|
|
|
|
// 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)
|
2014-10-07 05:40:37 +02:00
|
|
|
$('#update_secs').text(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
|
2014-09-28 01:16:16 +02:00
|
|
|
else
|
2014-10-07 05:40:37 +02:00
|
|
|
$('#update_secs').text(_("No new posts found"));
|
2014-09-28 01:16:16 +02:00
|
|
|
}
|
2014-09-28 21:00:43 +02:00
|
|
|
},
|
|
|
|
error: function(xhr, status_text, error_text) {
|
|
|
|
if (status_text == "error") {
|
|
|
|
if (error_text == "Not Found") {
|
2014-10-07 05:40:37 +02:00
|
|
|
$('#update_secs').text(_("Thread deleted or pruned"));
|
2014-09-28 21:00:43 +02:00
|
|
|
$('#auto_update_status').prop('checked', false);
|
|
|
|
$('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
|
2014-09-28 21:23:30 +02:00
|
|
|
return;
|
2014-09-28 21:00:43 +02:00
|
|
|
} else {
|
|
|
|
$('#update_secs').text("Error: "+error_text);
|
|
|
|
}
|
|
|
|
} else if (status_text) {
|
2014-10-07 05:40:37 +02:00
|
|
|
$('#update_secs').text(_("Error: ")+status_text);
|
2014-09-28 21:00:43 +02:00
|
|
|
} else {
|
2014-10-07 05:40:37 +02:00
|
|
|
$('#update_secs').text(_("Unknown error"));
|
2014-09-28 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep trying to update
|
|
|
|
if ($('#auto_update_status').is(':checked')) {
|
|
|
|
poll_interval_delay = poll_interval_errordelay;
|
|
|
|
auto_update(poll_interval_delay);
|
|
|
|
}
|
2012-03-15 04:44:15 +01:00
|
|
|
}
|
|
|
|
});
|
2012-03-15 10:40:52 +01:00
|
|
|
|
2014-09-25 06:31:40 +02:00
|
|
|
return false;
|
2012-03-15 10:40:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$(window).scroll(function() {
|
2013-12-23 17:31:47 +01:00
|
|
|
recheck_activated();
|
2014-09-28 01:16:16 +02:00
|
|
|
|
|
|
|
// if the newest post is not visible
|
2013-12-23 17:31:47 +01:00
|
|
|
if($(this).scrollTop() + $(this).height() <
|
|
|
|
$('div.post:last').position().top + $('div.post:last').height()) {
|
2013-12-23 16:34:44 +01:00
|
|
|
end_of_page = false;
|
2012-03-15 10:40:52 +01:00
|
|
|
return;
|
2014-09-28 01:16:16 +02:00
|
|
|
} else {
|
2014-09-30 05:02:57 +02:00
|
|
|
if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
|
2014-09-28 01:16:16 +02:00
|
|
|
poll(manualUpdate = true);
|
|
|
|
}
|
|
|
|
end_of_page = true;
|
2012-03-15 10:40:52 +01:00
|
|
|
}
|
2014-09-30 08:58:23 +02:00
|
|
|
});
|
2013-12-23 16:34:44 +01:00
|
|
|
|
2014-09-28 01:16:16 +02:00
|
|
|
$('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
|
2014-09-24 22:14:02 +02:00
|
|
|
|
2014-09-28 01:16:16 +02:00
|
|
|
if($("#auto_update_status").is(':checked')) {
|
|
|
|
auto_update(poll_interval_delay);
|
|
|
|
}
|
2012-03-15 04:44:15 +01:00
|
|
|
});
|
2015-03-13 12:18:56 +01:00
|
|
|
}();
|