From fa28560982590127705a7abf3619bff75f9dde83 Mon Sep 17 00:00:00 2001 From: czaks Date: Mon, 23 Dec 2013 17:31:47 +0100 Subject: [PATCH] auto-reload.js: add a number indicating new posts in the tab title --- js/auto-reload.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/js/auto-reload.js b/js/auto-reload.js index f3f04639..725b4b51 100644 --- a/js/auto-reload.js +++ b/js/auto-reload.js @@ -23,7 +23,33 @@ $(document).ready(function(){ var poll_interval; var end_of_page = false; + + var orig_title = document.title; + var new_posts = 0; + var first_new_post = null; + var update_title = function() { + document.title = (new_posts ? "("+new_posts+") " : "") + orig_title; + }; + + var window_active = true; + $(window).focus(function() { + window_active = true; + recheck_activated(); + }); + $(window).blur(function() { + window_active = false; + }); + var recheck_activated = function() { + if (new_posts && window_active && + $(window).scrollTop() + $(window).height() >= + $(first_new_post).position().top) { + + new_posts = 0; + } + update_title(); + }; + var poll = function() { $.ajax({ url: document.location, @@ -31,8 +57,13 @@ $(document).ready(function(){ $(data).find('div.post.reply').each(function() { var id = $(this).attr('id'); if($('#' + id).length == 0) { + if (!new_posts) { + first_new_post = this; + } $(this).insertAfter($('div.post:last').next()).after('
'); + new_posts++; $(document).trigger('new_post', this); + recheck_activated(); } }); } @@ -43,7 +74,10 @@ $(document).ready(function(){ }; $(window).scroll(function() { - if($(this).scrollTop() + $(this).height() < $('div.post:last').position().top + $('div.post:last').height()) { + recheck_activated(); + + if($(this).scrollTop() + $(this).height() < + $('div.post:last').position().top + $('div.post:last').height()) { end_of_page = false; return; }