Advertisement
OSS1

custom js

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