From ee54df825a71dab1bfb03fdf91138240f3110a9d Mon Sep 17 00:00:00 2001 From: Michael Save Date: Thu, 15 Mar 2012 14:44:15 +1100 Subject: auto-reload.js: bringing AJAX to tinyboard --- js/auto-reload.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 js/auto-reload.js (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js new file mode 100644 index 00000000..3789a136 --- /dev/null +++ b/js/auto-reload.js @@ -0,0 +1,33 @@ +/* + * auto-reload.js + * https://github.com/savetheinternet/Tinyboard-Tools/blob/master/js/auto-reload.js + * + * Brings AJAX to Tinyboard. + * + * Released under the MIT license + * Copyright (c) 2012 Michael Save + * + * Usage: + * $config['additional_javascript'][] = $config['root'] . 'auto-reload.js'; + * + */ + +$(document).ready(function(){ + if($('div.banner').length == 0) + return; // not index + + setInterval(function() { + $.ajax({ + url: document.location, + success: function(data) { + $(data).find('div.post.reply').each(function() { + var id = $(this).attr('id'); + if($('#' + id).length == 0) { + $(this).insertAfter($('div.reply:last').next()).after('
'); + } + }); + } + }); + }, 6000); +}); + -- cgit v1.3 From 34301da174acd3cfb326f03693657aeacd58be27 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Thu, 15 Mar 2012 16:19:26 +1100 Subject: local-time.js: convert times into user's timezone --- js/auto-reload.js | 3 ++- js/forced-anon.js | 4 ++-- js/local-time.js | 42 ++++++++++++++++++++++++++++++++++++++++++ js/quick-reply.js | 4 ++-- 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 js/local-time.js (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js index 3789a136..70b4acc3 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -8,7 +8,8 @@ * Copyright (c) 2012 Michael Save * * Usage: - * $config['additional_javascript'][] = $config['root'] . 'auto-reload.js'; + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/auto-reload.js'; * */ diff --git a/js/forced-anon.js b/js/forced-anon.js index 86af4063..6949769d 100644 --- a/js/forced-anon.js +++ b/js/forced-anon.js @@ -6,8 +6,8 @@ * Copyright (c) 2012 Michael Save * * Usage: - * $config['additional_javascript'][] = $config['root'] . 'jquery.min.js'; - * $config['additional_javascript'][] = $config['root'] . 'forced-anon.js'; + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/forced-anon.js'; * */ diff --git a/js/local-time.js b/js/local-time.js new file mode 100644 index 00000000..6d0a163a --- /dev/null +++ b/js/local-time.js @@ -0,0 +1,42 @@ +/* + * local-time.js + * https://github.com/savetheinternet/Tinyboard-Tools/blob/master/js/local-time.js + * + * Released under the MIT license + * Copyright (c) 2012 Michael Save + * + * Usage: + * $config['quick_reply'] = true; + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/local-time.js'; + * + */ + +$(document).ready(function(){ + var iso8601 = function(s) { + s = s.replace(/\.\d\d\d+/,""); // remove milliseconds + s = s.replace(/-/,"/").replace(/-/,"/"); + s = s.replace(/T/," ").replace(/Z/," UTC"); + s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 + return new Date(s); + }; + var zeropad = function(num, count) { + return [Math.pow(10, count - num.toString().length), num].join('').substr(1); + }; + + $('time').each(function() { + if(!$(this).text().match(/^\d+\/\d+\/\d+ \(\w+\) \d+:\d+:\d+$/)) + return; + + var t = iso8601($(this).attr('datetime')); + + $(this).text( + // date + zeropad(t.getMonth() + 1, 2) + "/" + zeropad(t.getDate(), 2) + "/" + t.getFullYear().toString().substring(2) + + " (" + ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][t.getDay()] + ") " + + // time + zeropad(t.getHours(), 2) + ":" + zeropad(t.getMinutes(), 2) + ":" + zeropad(t.getSeconds(), 2) + ); + }); +}); + diff --git a/js/quick-reply.js b/js/quick-reply.js index 0062cd1e..cc1a45a2 100644 --- a/js/quick-reply.js +++ b/js/quick-reply.js @@ -7,8 +7,8 @@ * * Usage: * $config['quick_reply'] = true; - * $config['additional_javascript'][] = $config['root'] . 'jquery.min.js'; - * $config['additional_javascript'][] = $config['root'] . 'quick-reply.js'; + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/quick-reply.js'; * */ -- cgit v1.3 From a3b3e1cc64ff79dc670e84e7843e99ad83a61676 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Thu, 15 Mar 2012 16:42:58 +1100 Subject: Don't check for new posts unless user is scrolled past the most recent one. --- js/auto-reload.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js index 70b4acc3..83e9751e 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -18,6 +18,9 @@ $(document).ready(function(){ return; // not index setInterval(function() { + if($(window).scrollTop() + $(window).height() < $('div.post.reply:last').position().top + $('div.post.reply:last').height()) + return; // not scrolled past last reply + $.ajax({ url: document.location, success: function(data) { @@ -29,6 +32,6 @@ $(document).ready(function(){ }); } }); - }, 6000); + }, 5000); }); -- cgit v1.3 From af7b5b5d78b5981a2b47cea0afa614bc70a6c7f5 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Thu, 15 Mar 2012 20:40:52 +1100 Subject: better scroll events --- js/auto-reload.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js index 83e9751e..5070c9ca 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -17,10 +17,9 @@ $(document).ready(function(){ if($('div.banner').length == 0) return; // not index - setInterval(function() { - if($(window).scrollTop() + $(window).height() < $('div.post.reply:last').position().top + $('div.post.reply:last').height()) - return; // not scrolled past last reply - + var poll_interval; + + var poll = function() { $.ajax({ url: document.location, success: function(data) { @@ -32,6 +31,20 @@ $(document).ready(function(){ }); } }); - }, 5000); + + poll_interval = setTimeout(poll, 5000); + }; + + $(window).scroll(function() { + if($(this).scrollTop() + $(this).height() < $('div.post.reply:last').position().top + $('div.post.reply:last').height()) { + clearTimeout(poll_interval); + poll_interval = false; + return; + } + + if(poll_interval === false) { + poll_interval = setTimeout(poll, 1500); + } + }).trigger('scroll'); }); -- cgit v1.3 From f48d338ce051070a0eb096ab5cac073f19467549 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Fri, 16 Mar 2012 09:08:03 +1100 Subject: $('div.post.reply:last') is too specific; use $('div.post:last') or else it won't include the OP --- js/auto-reload.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js index 5070c9ca..0921c36d 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -26,7 +26,7 @@ $(document).ready(function(){ $(data).find('div.post.reply').each(function() { var id = $(this).attr('id'); if($('#' + id).length == 0) { - $(this).insertAfter($('div.reply:last').next()).after('
'); + $(this).insertAfter($('div.post:last').next()).after('
'); } }); } @@ -36,7 +36,7 @@ $(document).ready(function(){ }; $(window).scroll(function() { - if($(this).scrollTop() + $(this).height() < $('div.post.reply:last').position().top + $('div.post.reply:last').height()) { + if($(this).scrollTop() + $(this).height() < $('div.post:last').position().top + $('div.post:last').height()) { clearTimeout(poll_interval); poll_interval = false; return; -- cgit v1.3 From 21a1152ebead57c60c76c341d4f8ec8bc737949b Mon Sep 17 00:00:00 2001 From: Michael Save Date: Sun, 18 Mar 2012 23:55:01 +1100 Subject: custom event for new posts, so auto-reload.js etc will work with forced-anon.js and post-hover.js --- js/auto-reload.js | 3 ++- js/forced-anon.js | 42 ++++++++++++++++++++++++------------------ js/post-hover.js | 24 +++++++++++++++--------- 3 files changed, 41 insertions(+), 28 deletions(-) (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js index 0921c36d..44a4b9be 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -27,12 +27,13 @@ $(document).ready(function(){ var id = $(this).attr('id'); if($('#' + id).length == 0) { $(this).insertAfter($('div.post:last').next()).after('
'); + $(document).trigger('new_post', this); } }); } }); - poll_interval = setTimeout(poll, 5000); + poll_interval = setTimeout(poll, 500); }; $(window).scroll(function() { diff --git a/js/forced-anon.js b/js/forced-anon.js index 0f7f1c70..9b9df1eb 100644 --- a/js/forced-anon.js +++ b/js/forced-anon.js @@ -11,24 +11,26 @@ * */ -$(document).ready(function(){ - enable_fa = function() { - $('p.intro label').each(function() { - if($(this).children('a.capcode').length == 0) { - var id = $(this).parent().children('a.post_no:eq(1)').text(); - - if($(this).children('a.email').length != 0) - var p = $(this).children('a.email'); - else - var p = $(this); - - old_info[id] = {'name': p.children('span.name').text(), 'trip': p.children('span.trip').text()}; - - p.children('span.name').text('Anonymous'); - if(p.children('span.trip').length != 0) - p.children('span.trip').text(''); - } - }); +$(document).ready(function() { + var force_anon = function() { + if($(this).children('a.capcode').length == 0) { + var id = $(this).parent().children('a.post_no:eq(1)').text(); + + if($(this).children('a.email').length != 0) + var p = $(this).children('a.email'); + else + var p = $(this); + + old_info[id] = {'name': p.children('span.name').text(), 'trip': p.children('span.trip').text()}; + + p.children('span.name').text('Anonymous'); + if(p.children('span.trip').length != 0) + p.children('span.trip').text(''); + } + }; + + var enable_fa = function() { + $('p.intro label').each(force_anon); }; var disable_fa = function() { @@ -75,5 +77,9 @@ $(document).ready(function(){ if(forced_anon) enable_fa(); + $(document).bind('new_post', function(e, post) { + if(forced_anon) + $(post).find('p.intro label').each(force_anon); + }); }); diff --git a/js/post-hover.js b/js/post-hover.js index f6ac69aa..c76ca0d8 100644 --- a/js/post-hover.js +++ b/js/post-hover.js @@ -13,17 +13,19 @@ $(document).ready(function(){ var dont_fetch_again = []; - $('p.body a:not([rel="nofollow"])').each(function() { + var init_hover = function() { + var link = $(this); + var id; - if(id = $(this).text().match(/^>>(\d+)$/)) { + if(id = $(link).text().match(/^>>(\d+)$/)) { id = id[1]; } var post = false; var hovering = false; var hovered_at; - $(this).hover(function(e) { + $(link).hover(function(e) { hovering = true; hovered_at = {'x': e.pageX, 'y': e.pageY}; @@ -52,7 +54,7 @@ $(document).ready(function(){ start_hover(this); } else { var link = this; - var url = $(this).attr('href').replace(/#.*$/, ''); + var url = $(link).attr('href').replace(/#.*$/, ''); if($.inArray(url, dont_fetch_again) != -1) { return; @@ -64,13 +66,10 @@ $(document).ready(function(){ context: document.body, success: function(data) { $(data).find('div.post.reply').each(function() { - if($('#' + $(this).attr('id')).length == 0) - $('div.post:first').prepend($(this).css('display', 'none').addClass('hidden')); + if($('#' + $(link).attr('id')).length == 0) + $('div.post:first').prepend($(link).css('display', 'none').addClass('hidden')); }); - if(typeof window.enable_fa == 'function' && localStorage['forcedanon']) - enable_fa(); - post = $('div.post#reply_' + id); if(hovering && post.length > 0) start_hover(link); @@ -105,6 +104,13 @@ $(document).ready(function(){ hover.css('left', (e.pageX ? e.pageX : hovered_at['x'])).css('top', top); }); + }; + + $('p.body a:not([rel="nofollow"])').each(init_hover); + + // allow to work with auto-reload.js, etc. + $(document).bind('new_post', function(e, post) { + $(post).find('p.body a:not([rel="nofollow"])').each(init_hover); }); }); -- cgit v1.3 From d38801fc4ed63e6caa4d4a39b88c1d250a09b6b9 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Sun, 18 Mar 2012 23:57:38 +1100 Subject: auto-reload.js: poll every 5 seconds, not 500 ms --- js/auto-reload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/auto-reload.js') diff --git a/js/auto-reload.js b/js/auto-reload.js index 44a4b9be..f6981bdf 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -33,7 +33,7 @@ $(document).ready(function(){ } }); - poll_interval = setTimeout(poll, 500); + poll_interval = setTimeout(poll, 5000); }; $(window).scroll(function() { -- cgit v1.3