From 0383a3629abe2fb7dd5adc460bb1a14682847d40 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 13 Oct 2011 17:25:28 +0200 Subject: [PATCH] Use the Page Visibility API for Scroll BG, close #266 --- 4chan_x.user.js | 40 ++++++++++++++++++++++------------------ script.coffee | 34 ++++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 28820f6d7..7e4533af9 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -852,7 +852,6 @@ }, keydown: function(e) { var o, range, selEnd, selStart, ta, thread, valEnd, valMid, valStart, value, _ref, _ref2, _ref3; - updater.focus = true; if (((_ref = e.target.nodeName) === 'TEXTAREA' || _ref === 'INPUT') && !e.altKey && !e.ctrlKey && !(e.keyCode === 27)) { return; } @@ -1890,18 +1889,6 @@ updater = { init: function() { var checkbox, checked, dialog, html, input, name, title, _i, _len, _ref; - if (conf['Scrolling']) { - if (conf['Scroll BG']) { - updater.focus = true; - } else { - $.bind(window, 'focus', (function() { - return updater.focus = true; - })); - $.bind(window, 'blur', (function() { - return updater.focus = false; - })); - } - } html = "
-" + conf['Interval'] + "
"; checkbox = config.updater.checkbox; for (name in checkbox) { @@ -1923,7 +1910,10 @@ $.bind(input, 'click', function() { return conf[this.name] = this.checked; }); - if (input.name === 'Verbose') { + if (input.name === 'Scroll BG') { + $.bind(input, 'click', updater.cb.scrollBG); + updater.cb.scrollBG.call(input); + } else if (input.name === 'Verbose') { $.bind(input, 'click', updater.cb.verbose); updater.cb.verbose.call(input); } else if (input.name === 'Auto Update This') { @@ -1961,6 +1951,21 @@ return clearTimeout(updater.timeoutID); } }, + scrollBG: function() { + return updater.scrollBG = this.checked ? function() { + return true; + } : d.visibilityState ? function() { + return !d.hidden; + } : d.oVisibilityState ? function() { + return !d.oHidden; + } : d.mozVisibilityState ? function() { + return !d.mozHidden; + } : d.webkitVisibilityState ? function() { + return !d.webkitHidden; + } : function() { + return true; + }; + }, update: function() { var arr, body, id, input, replies, reply, scroll, _i, _len, _ref, _ref2; if (this.status === 404) { @@ -1989,12 +1994,12 @@ return; } replies = $$('.reply', body); - id = Number(((_ref2 = $('td[id]', updater.br.previousElementSibling)) != null ? _ref2.id : void 0) || 0); + id = Number(((_ref2 = $('td[id]', updater.br.previousSibling)) != null ? _ref2.id : void 0) || 0); arr = []; while ((reply = replies.pop()) && (reply.id > id)) { arr.push(reply.parentNode.parentNode.parentNode); } - scroll = conf['Scrolling'] && updater.focus && arr.length && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20); + scroll = conf['Scrolling'] && updater.scrollBG() && arr.length && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20); if (conf['Verbose']) { updater.count.textContent = '+' + arr.length; if (arr.length === 0) { @@ -2007,7 +2012,7 @@ $.before(updater.br, reply); } if (scroll) { - return scrollTo(0, d.body.scrollHeight); + return updater.br.previousSibling.scrollIntoView(); } } }, @@ -2661,7 +2666,6 @@ }, scroll: function(e) { var bottom, height, i, reply, _len, _ref; - updater.focus = true; height = d.body.clientHeight; _ref = unread.replies; for (i = 0, _len = _ref.length; i < _len; i++) { diff --git a/script.coffee b/script.coffee index ea8eb5f4a..ab985319c 100644 --- a/script.coffee +++ b/script.coffee @@ -608,7 +608,6 @@ keybinds = $.bind d, 'keydown', keybinds.keydown keydown: (e) -> - updater.focus = true return if e.target.nodeName in ['TEXTAREA', 'INPUT'] and not e.altKey and not e.ctrlKey and not (e.keyCode is 27) return unless key = keybinds.keyCode e @@ -1422,12 +1421,6 @@ threadHiding = updater = init: -> - if conf['Scrolling'] - if conf['Scroll BG'] - updater.focus = true - else - $.bind window, 'focus', (-> updater.focus = true) - $.bind window, 'blur', (-> updater.focus = false) html = "
-#{conf['Interval']}
" {checkbox} = config.updater for name of checkbox @@ -1451,7 +1444,10 @@ updater = if input.type is 'checkbox' $.bind input, 'click', $.cb.checked $.bind input, 'click', -> conf[@name] = @checked - if input.name is 'Verbose' + if input.name is 'Scroll BG' + $.bind input, 'click', updater.cb.scrollBG + updater.cb.scrollBG.call input + else if input.name is 'Verbose' $.bind input, 'click', updater.cb.verbose updater.cb.verbose.call input else if input.name is 'Auto Update This' @@ -1480,6 +1476,21 @@ updater = updater.timeoutID = setTimeout updater.timeout, 1000 else clearTimeout updater.timeoutID + scrollBG: -> + updater.scrollBG = + if @checked + -> true + else + if d.visibilityState + -> !d.hidden + else if d.oVisibilityState + -> !d.oHidden + else if d.mozVisibilityState + -> !d.mozHidden + else if d.webkitVisibilityState + -> !d.webkitHidden + else + -> true update: -> if @status is 404 updater.timer.textContent = '' @@ -1505,12 +1516,12 @@ updater = return replies = $$ '.reply', body - id = Number $('td[id]', updater.br.previousElementSibling)?.id or 0 + id = Number $('td[id]', updater.br.previousSibling)?.id or 0 arr = [] while (reply = replies.pop()) and (reply.id > id) arr.push reply.parentNode.parentNode.parentNode #table - scroll = conf['Scrolling'] && updater.focus && arr.length && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20) + scroll = conf['Scrolling'] && updater.scrollBG() && arr.length && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20) if conf['Verbose'] updater.count.textContent = '+' + arr.length if arr.length is 0 @@ -1522,7 +1533,7 @@ updater = while reply = arr.pop() $.before updater.br, reply if scroll - scrollTo 0, d.body.scrollHeight + updater.br.previousSibling.scrollIntoView() timeout: -> updater.timeoutID = setTimeout updater.timeout, 1000 @@ -1966,7 +1977,6 @@ unread = Favicon.update() scroll: (e) -> - updater.focus = true height = d.body.clientHeight for reply, i in unread.replies {bottom} = reply.getBoundingClientRect()