Advertisement
Guest User

Auto-Refresh

a guest
Sep 14th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. $.fn.scrollStopped = function(callback) {
  2. $(this).scroll(function(){
  3. var self = this, $this = $(self);
  4. if ($this.data('scrollTimeout')) {
  5. clearTimeout($this.data('scrollTimeout'));
  6. }
  7. $this.data('scrollTimeout', setTimeout(callback,250,self));
  8. });
  9. };
  10.  
  11. function makeIcon(mode){
  12. var favicon = $("link[rel='shortcut icon']");
  13.  
  14. if (!favicon.length) {
  15. var favicon = $('<link rel="shortcut icon"></link>').appendTo('head');
  16. }
  17.  
  18. $("link[rel='shortcut icon']").attr("href", configRoot+"static/favicon"+(mode?"-"+mode:"")+".ico");
  19. }
  20.  
  21. +function(){
  22. var notify = false;
  23. auto_reload_enabled = true; // for watch.js to interop
  24.  
  25. $(document).ready(function(){
  26.  
  27. // Adds Options panel item
  28. if (typeof localStorage.auto_thread_update === 'undefined') {
  29. localStorage.auto_thread_update = 'true'; //default value
  30. }
  31. if (window.Options && Options.get_tab('general')) {
  32. Options.extend_tab("general", "<fieldset id='auto-update-fs'><legend>"+_("Auto update")+"</legend>"
  33. + ('<label id="auto-thread-update"><input type="checkbox">' + _('Auto update thread') + '</label>')
  34. + ('<label id="auto_thread_desktop_notifications"><input type="checkbox">' + _('Show desktop notifications when users quote me') + '</label>')
  35. + ('<label id="auto_thread_desktop_notifications_all"><input type="checkbox">' + _('Show desktop notifications on all replies') + '</label>')
  36. + '</fieldset>');
  37.  
  38. $('#auto-thread-update>input').on('click', function() {
  39. if ($('#auto-thread-update>input').is(':checked')) {
  40. localStorage.auto_thread_update = 'true';
  41. } else {
  42. localStorage.auto_thread_update = 'false';
  43. }
  44. });
  45.  
  46. $('#auto_thread_desktop_notifications>input,#auto_thread_desktop_notifications_all>input').on('click', function() {
  47. if (!("Notification" in window)) return;
  48.  
  49. var setting = $(this).parent().attr('id');
  50.  
  51. if ($(this).is(':checked')) {
  52. Notification.requestPermission(function(permission){
  53. if (permission === "granted") {
  54. localStorage[setting] = 'true';
  55. }
  56. });
  57. if (Notification.permission === "granted") {
  58. localStorage[setting] = 'true';
  59. }
  60. } else {
  61. localStorage[setting] = 'false';
  62. }
  63. });
  64.  
  65. if (localStorage.auto_thread_update === 'true') {
  66. $('#auto-thread-update>input').prop('checked', true);
  67. }
  68.  
  69. if (localStorage.auto_thread_desktop_notifications === 'true') {
  70. $('#auto_thread_desktop_notifications>input').prop('checked', true);
  71. notify = "mention";
  72. }
  73.  
  74. if (localStorage.auto_thread_desktop_notifications_all === 'true') {
  75. $('#auto_thread_desktop_notifications_all>input').prop('checked', true);
  76. notify = "all";
  77. }
  78. }
  79.  
  80. // not thread
  81. if (active_page != 'thread')
  82. return;
  83.  
  84. var countdown_interval;
  85.  
  86. // Add an update link
  87. $('span#thread-links').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>");
  88.  
  89. // Set the updater checkbox according to user setting
  90. if (localStorage.auto_thread_update === 'true') {
  91. $('#auto_update_status').prop('checked', true);
  92. }
  93.  
  94. // Grab the settings
  95. var settings = new script_settings('auto-reload');
  96. var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
  97. var poll_interval_maxdelay = settings.get('max_delay', 600000);
  98. var poll_interval_errordelay = settings.get('error_delay', 30000);
  99.  
  100. // number of ms to wait before reloading
  101. var poll_interval_delay = poll_interval_mindelay;
  102. var poll_current_time = poll_interval_delay;
  103.  
  104. var end_of_page = false;
  105.  
  106. var new_posts = 0;
  107. var first_new_post = null;
  108.  
  109. var title = document.title;
  110.  
  111. if (typeof update_title == "undefined") {
  112. var update_title = function() {
  113. if (new_posts) {
  114. document.title = "("+new_posts+") "+title;
  115. } else {
  116. document.title = title;
  117. makeIcon(false);
  118. }
  119. };
  120. }
  121.  
  122. if (typeof add_title_collector != "undefined")
  123. add_title_collector(function(){
  124. return new_posts;
  125. });
  126.  
  127. var window_active = true;
  128. $(window).focus(function() {
  129. window_active = true;
  130. recheck_activated();
  131.  
  132. // Reset the delay if needed
  133. if(settings.get('reset_focus', true)) {
  134. poll_interval_delay = poll_interval_mindelay;
  135. }
  136. });
  137. $(window).blur(function() {
  138. window_active = false;
  139. });
  140.  
  141.  
  142. $('#auto_update_status').click(function() {
  143. if($("#auto_update_status").is(':checked')) {
  144. auto_update(poll_interval_mindelay);
  145. } else {
  146. stop_auto_update();
  147. $('#update_secs').text("");
  148. }
  149.  
  150. });
  151.  
  152.  
  153. var decrement_timer = function() {
  154. poll_current_time = poll_current_time - 1000;
  155. $('#update_secs').text(poll_current_time/1000);
  156.  
  157. if (poll_current_time <= 0) {
  158. poll(manualUpdate = false);
  159. }
  160. }
  161.  
  162. var recheck_activated = function(end_of_page) {
  163. if (typeof end_of_page == "undefined") var end_of_page = false;
  164. if (end_of_page || (new_posts && window_active &&
  165. $(window).scrollTop() + $(window).height() >=
  166. $('div.boardlist.bottom').position().top)) {
  167.  
  168. new_posts = 0;
  169. }
  170. update_title();
  171. first_new_post = null;
  172. };
  173.  
  174. // automatically updates the thread after a specified delay
  175. var auto_update = function(delay) {
  176. clearInterval(countdown_interval);
  177.  
  178. poll_current_time = delay;
  179. countdown_interval = setInterval(decrement_timer, 1000);
  180. $('#update_secs').text(poll_current_time/1000);
  181. }
  182.  
  183. var stop_auto_update = function() {
  184. clearInterval(countdown_interval);
  185. }
  186.  
  187. var epoch = (new Date).getTime();
  188. var epochold = epoch;
  189.  
  190. var timeDiff = function (delay) {
  191. if((epoch-epochold) > delay) {
  192. epochold = epoch = (new Date).getTime();
  193. return true;
  194. }else{
  195. epoch = (new Date).getTime();
  196. return;
  197. }
  198. }
  199.  
  200. var poll = function(manualUpdate) {
  201. stop_auto_update();
  202. $('#update_secs').text(_("Updating..."));
  203.  
  204. $.ajax({
  205. url: document.location,
  206. success: function(data) {
  207. var loaded_posts = 0; // the number of new posts loaded in this update
  208. $(data).find('div.post.reply').each(function() {
  209. var id = $(this).attr('id');
  210. if($('#' + id).length == 0) {
  211. if (!new_posts) {
  212. first_new_post = this;
  213. makeIcon('reply');
  214. if (notify === "all" || (notify === "mention" && $(this).find('.own_post').length)) {
  215. var body = $(this).children('.body').html().replace(/<br\s*[\/]?>/gi, "\n");
  216. var n = new Notification("New reply to "+$('title').text(), {body: $('<div/>').html(body).text()});
  217. }
  218. }
  219. $(this).insertAfter($('div.post:not(.post-hover):last').next()).after('<br class="clear">');
  220. new_posts++;
  221. loaded_posts++;
  222. $(document).trigger('new_post', this);
  223. recheck_activated();
  224. }
  225. });
  226. time_loaded = Date.now(); // interop with watch.js
  227.  
  228.  
  229. if ($('#auto_update_status').is(':checked')) {
  230. // If there are no new posts, double the delay. Otherwise set it to the min.
  231. if(loaded_posts == 0) {
  232. // if the update was manual, don't increase the delay
  233. if (manualUpdate == false) {
  234. poll_interval_delay *= 2;
  235.  
  236. // Don't increase the delay beyond the maximum
  237. if(poll_interval_delay > poll_interval_maxdelay) {
  238. poll_interval_delay = poll_interval_maxdelay;
  239. }
  240. }
  241. } else {
  242. poll_interval_delay = poll_interval_mindelay;
  243. }
  244.  
  245. auto_update(poll_interval_delay);
  246. } else {
  247. // Decide the message to show if auto update is disabled
  248. if (loaded_posts > 0)
  249. $('#update_secs').text(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
  250. else
  251. $('#update_secs').text(_("No new posts found"));
  252. }
  253. },
  254. error: function(xhr, status_text, error_text) {
  255. if (status_text == "error") {
  256. if (error_text == "Not Found") {
  257. $('#update_secs').text(_("Thread deleted or pruned"));
  258. $('#auto_update_status').prop('checked', false);
  259. $('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
  260. return;
  261. } else {
  262. $('#update_secs').text("Error: "+error_text);
  263. }
  264. } else if (status_text) {
  265. $('#update_secs').text(_("Error: ")+status_text);
  266. } else {
  267. $('#update_secs').text(_("Unknown error"));
  268. }
  269.  
  270. // Keep trying to update
  271. if ($('#auto_update_status').is(':checked')) {
  272. poll_interval_delay = poll_interval_errordelay;
  273. auto_update(poll_interval_delay);
  274. }
  275. }
  276. });
  277.  
  278. return false;
  279. };
  280.  
  281. $(window).scrollStopped(function() {
  282. // if the newest post is not visible
  283. if($(this).scrollTop() + $(this).height() <
  284. $('div.post:last').position().top + $('div.post:last').height()) {
  285. end_of_page = false;
  286. } else {
  287. if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
  288. poll(manualUpdate = true);
  289. }
  290. end_of_page = true;
  291. }
  292. recheck_activated(end_of_page);
  293. });
  294.  
  295. $('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
  296.  
  297. if($("#auto_update_status").is(':checked')) {
  298. auto_update(poll_interval_delay);
  299. }
  300. });
  301. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement