The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
6.4KB

  1. /*
  2. * auto-reload.js
  3. * https://github.com/savetheinternet/Tinyboard/blob/master/js/auto-reload.js
  4. *
  5. * Brings AJAX to Tinyboard.
  6. *
  7. * Released under the MIT license
  8. * Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
  9. * Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
  10. * Copyright (c) 2013 undido <firekid109@hotmail.com>
  11. * Copyright (c) 2014 Fredrick Brennan <admin@8chan.co>
  12. *
  13. * Usage:
  14. * $config['additional_javascript'][] = 'js/jquery.min.js';
  15. * //$config['additional_javascript'][] = 'js/titlebar-notifications.js';
  16. * $config['additional_javascript'][] = 'js/auto-reload.js';
  17. *
  18. */
  19. auto_reload_enabled = true; // for watch.js to interop
  20. $(document).ready(function(){
  21. if($('div.banner').length == 0)
  22. return; // not index
  23. if($(".post.op").size() != 1)
  24. return; //not thread page
  25. var countdown_interval;
  26. // Add an update link
  27. $('.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>");
  28. // Grab the settings
  29. var settings = new script_settings('auto-reload');
  30. var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
  31. var poll_interval_maxdelay = settings.get('max_delay', 600000);
  32. var poll_interval_errordelay = settings.get('error_delay', 30000);
  33. // number of ms to wait before reloading
  34. var poll_interval_delay = poll_interval_mindelay;
  35. var poll_current_time = poll_interval_delay;
  36. var end_of_page = false;
  37. var new_posts = 0;
  38. var first_new_post = null;
  39. var title = document.title;
  40. if (typeof update_title == "undefined") {
  41. var update_title = function() {
  42. if (new_posts) {
  43. document.title = "("+new_posts+") "+title;
  44. } else {
  45. document.title = title;
  46. }
  47. };
  48. }
  49. if (typeof add_title_collector != "undefined")
  50. add_title_collector(function(){
  51. return new_posts;
  52. });
  53. var window_active = true;
  54. $(window).focus(function() {
  55. window_active = true;
  56. recheck_activated();
  57. // Reset the delay if needed
  58. if(settings.get('reset_focus', true)) {
  59. poll_interval_delay = poll_interval_mindelay;
  60. }
  61. });
  62. $(window).blur(function() {
  63. window_active = false;
  64. });
  65. $('#auto_update_status').click(function() {
  66. if($("#auto_update_status").is(':checked')) {
  67. auto_update(poll_interval_mindelay);
  68. } else {
  69. stop_auto_update();
  70. $('#update_secs').text("");
  71. }
  72. });
  73. var decrement_timer = function() {
  74. poll_current_time = poll_current_time - 1000;
  75. $('#update_secs').text(poll_current_time/1000);
  76. if (poll_current_time <= 0) {
  77. poll(manualUpdate = false);
  78. }
  79. }
  80. var recheck_activated = function() {
  81. if (new_posts && window_active &&
  82. $(window).scrollTop() + $(window).height() >=
  83. $('div.boardlist.bottom').position().top) {
  84. new_posts = 0;
  85. }
  86. update_title();
  87. first_new_post = null;
  88. };
  89. // automatically updates the thread after a specified delay
  90. var auto_update = function(delay) {
  91. clearInterval(countdown_interval);
  92. poll_current_time = delay;
  93. countdown_interval = setInterval(decrement_timer, 1000);
  94. $('#update_secs').text(poll_current_time/1000);
  95. }
  96. var stop_auto_update = function() {
  97. clearInterval(countdown_interval);
  98. }
  99. var epoch = (new Date).getTime();
  100. var epochold = epoch;
  101. var timeDiff = function (delay) {
  102. if((epoch-epochold) > delay) {
  103. epochold = epoch = (new Date).getTime();
  104. return true;
  105. }else{
  106. epoch = (new Date).getTime();
  107. return;
  108. }
  109. }
  110. var poll = function(manualUpdate) {
  111. stop_auto_update();
  112. $('#update_secs').text("Updating...");
  113. $.ajax({
  114. url: document.location,
  115. success: function(data) {
  116. var loaded_posts = 0; // the number of new posts loaded in this update
  117. $(data).find('div.post.reply').each(function() {
  118. var id = $(this).attr('id');
  119. if($('#' + id).length == 0) {
  120. if (!new_posts) {
  121. first_new_post = this;
  122. }
  123. $(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
  124. new_posts++;
  125. loaded_posts++;
  126. $(document).trigger('new_post', this);
  127. recheck_activated();
  128. }
  129. });
  130. time_loaded = Date.now(); // interop with watch.js
  131. if ($('#auto_update_status').is(':checked')) {
  132. // If there are no new posts, double the delay. Otherwise set it to the min.
  133. if(loaded_posts == 0) {
  134. // if the update was manual, don't increase the delay
  135. if (manualUpdate == false) {
  136. poll_interval_delay *= 2;
  137. // Don't increase the delay beyond the maximum
  138. if(poll_interval_delay > poll_interval_maxdelay) {
  139. poll_interval_delay = poll_interval_maxdelay;
  140. }
  141. }
  142. } else {
  143. poll_interval_delay = poll_interval_mindelay;
  144. }
  145. auto_update(poll_interval_delay);
  146. } else {
  147. // Decide the message to show if auto update is disabled
  148. if (loaded_posts > 0)
  149. $('#update_secs').text("Thread updated with "+loaded_posts+" new post(s)");
  150. else
  151. $('#update_secs').text("No new posts found");
  152. }
  153. },
  154. error: function(xhr, status_text, error_text) {
  155. if (status_text == "error") {
  156. if (error_text == "Not Found") {
  157. $('#update_secs').text("Thread deleted or pruned");
  158. $('#auto_update_status').prop('checked', false);
  159. $('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
  160. return;
  161. } else {
  162. $('#update_secs').text("Error: "+error_text);
  163. }
  164. } else if (status_text) {
  165. $('#update_secs').text("Error: "+status_text);
  166. } else {
  167. $('#update_secs').text("Unknown error");
  168. }
  169. // Keep trying to update
  170. if ($('#auto_update_status').is(':checked')) {
  171. poll_interval_delay = poll_interval_errordelay;
  172. auto_update(poll_interval_delay);
  173. }
  174. }
  175. });
  176. return false;
  177. };
  178. $(window).scroll(function() {
  179. recheck_activated();
  180. // if the newest post is not visible
  181. if($(this).scrollTop() + $(this).height() <
  182. $('div.post:last').position().top + $('div.post:last').height()) {
  183. end_of_page = false;
  184. return;
  185. } else {
  186. if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
  187. poll(manualUpdate = true);
  188. }
  189. end_of_page = true;
  190. }
  191. });
  192. $('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
  193. if($("#auto_update_status").is(':checked')) {
  194. auto_update(poll_interval_delay);
  195. }
  196. });