From f08ecd4ddc55f4a6a17d66590ba57e770eb6f499 Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Fri, 31 Aug 2018 02:35:11 -0400 Subject: Handle scrolling on Reddit redesign --- content_scripts/scroller.coffee | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/content_scripts/scroller.coffee b/content_scripts/scroller.coffee index 7202c682..b134dbe1 100644 --- a/content_scripts/scroller.coffee +++ b/content_scripts/scroller.coffee @@ -319,6 +319,15 @@ if DomUtils.isTopFrame() and window.location.host == "twitter.com" activatedElement = element ? getScrollingElement() func arguments... +if DomUtils.isTopFrame() and window.location.host in ["reddit.com", "new.reddit.com"] + for method in ["scrollTo", "scrollBy"] + do -> + func = Scroller[method] + Scroller[method] = -> + element = document.getElementById "overlayScrollContainer" + activatedElement = element ? getScrollingElement() + func arguments... + root = exports ? (window.root ?= {}) root.Scroller = Scroller extend window, root unless exports? -- cgit v1.2.3 From 256547a940b5ac182ba014add8f1fd234baa3a65 Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Tue, 4 Sep 2018 00:51:22 -0400 Subject: Refactor, part 1 --- content_scripts/scroller.coffee | 30 +++++++++++------------------- content_scripts/vimium_frontend.coffee | 1 + 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/content_scripts/scroller.coffee b/content_scripts/scroller.coffee index b134dbe1..19bf91cf 100644 --- a/content_scripts/scroller.coffee +++ b/content_scripts/scroller.coffee @@ -9,7 +9,7 @@ activatedElement = null # https://github.com/philc/vimium/pull/2168#issuecomment-236488091 getScrollingElement = -> - document.scrollingElement ? document.body + specialScrollingElement() ? document.scrollingElement ? document.body # Return 0, -1 or 1: the sign of the argument. # NOTE(smblott; 2014/12/17) We would like to use Math.sign(). However, according to this site @@ -239,6 +239,7 @@ CoreScroller = # Scroller contains the two main scroll functions which are used by clients. Scroller = init: -> + activatedElement = null handlerStack.push _name: 'scroller/active-element' DOMActivate: (event) -> handlerStack.alwaysContinueBubbling -> @@ -309,24 +310,15 @@ Scroller = element = findScrollableElement element, "x", amount, 1 CoreScroller.scroll element, "x", amount, false -# Hack to make expanded tweets scrollable on Twitter (See #3045). -if DomUtils.isTopFrame() and window.location.host == "twitter.com" - for method in ["scrollTo", "scrollBy"] - do -> - func = Scroller[method] - Scroller[method] = -> - element = document.querySelector "div.permalink-container div.permalink[role=main]" - activatedElement = element ? getScrollingElement() - func arguments... - -if DomUtils.isTopFrame() and window.location.host in ["reddit.com", "new.reddit.com"] - for method in ["scrollTo", "scrollBy"] - do -> - func = Scroller[method] - Scroller[method] = -> - element = document.getElementById "overlayScrollContainer" - activatedElement = element ? getScrollingElement() - func arguments... +specialScrollingElement = -> + selector = specialScrollingElementMap[window.location.host] + if selector + document.querySelector selector + +specialScrollingElementMap = + 'twitter.com': 'div.permalink-container div.permalink[role=main]' + 'reddit.com': '#overlayScrollContainer' + 'new.reddit.com': '#overlayScrollContainer' root = exports ? (window.root ?= {}) root.Scroller = Scroller diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 26aa3492..e93a7ad9 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -297,6 +297,7 @@ checkIfEnabledForUrl = do -> {isEnabledForUrl, passKeys, frameIsFocused, isFirefox} = response Utils.isFirefox = -> isFirefox installModes() unless normalMode + Scroller.init() # TODO hack to bust Scroller.activatedElement caching normalMode.setPassKeys passKeys # Hide the HUD if we're not enabled. HUD.hide true, false unless isEnabledForUrl -- cgit v1.2.3 From 72aaf054201755f837ba84f26a4d74846a81ffba Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 4 Sep 2018 11:03:32 +0100 Subject: Tweak #3119. 1. Use verb phrase for function name. 2. Add `Scroller.reset()` method. This *only* resets the activated element. 3. Reset the scroller only if the URL has changed. (Previously, in #3119, the scroller was also being reset when the tab gained the focus.) Based on a suggestion from @marcotc. --- content_scripts/scroller.coffee | 9 ++++++--- content_scripts/vimium_frontend.coffee | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/content_scripts/scroller.coffee b/content_scripts/scroller.coffee index 19bf91cf..94a0d4f1 100644 --- a/content_scripts/scroller.coffee +++ b/content_scripts/scroller.coffee @@ -9,7 +9,7 @@ activatedElement = null # https://github.com/philc/vimium/pull/2168#issuecomment-236488091 getScrollingElement = -> - specialScrollingElement() ? document.scrollingElement ? document.body + getSpecialScrollingElement() ? document.scrollingElement ? document.body # Return 0, -1 or 1: the sign of the argument. # NOTE(smblott; 2014/12/17) We would like to use Math.sign(). However, according to this site @@ -239,7 +239,6 @@ CoreScroller = # Scroller contains the two main scroll functions which are used by clients. Scroller = init: -> - activatedElement = null handlerStack.push _name: 'scroller/active-element' DOMActivate: (event) -> handlerStack.alwaysContinueBubbling -> @@ -249,6 +248,10 @@ Scroller = # yet implemented by Chrome. activatedElement = event.deepPath?[0] ? event.path?[0] ? event.target CoreScroller.init() + @reset() + + reset: -> + activatedElement = null # scroll the active element in :direction by :amount * :factor. # :factor is needed because :amount can take on string values, which scrollBy converts to element dimensions. @@ -310,7 +313,7 @@ Scroller = element = findScrollableElement element, "x", amount, 1 CoreScroller.scroll element, "x", amount, false -specialScrollingElement = -> +getSpecialScrollingElement = -> selector = specialScrollingElementMap[window.location.host] if selector document.querySelector selector diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index e93a7ad9..6fa118cf 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -297,7 +297,6 @@ checkIfEnabledForUrl = do -> {isEnabledForUrl, passKeys, frameIsFocused, isFirefox} = response Utils.isFirefox = -> isFirefox installModes() unless normalMode - Scroller.init() # TODO hack to bust Scroller.activatedElement caching normalMode.setPassKeys passKeys # Hide the HUD if we're not enabled. HUD.hide true, false unless isEnabledForUrl @@ -308,6 +307,7 @@ checkIfEnabledForUrl = do -> # When we're informed by the background page that a URL in this tab has changed, we check if we have the # correct enabled state (but only if this frame has the focus). checkEnabledAfterURLChange = forTrusted -> + Scroller.reset() # The URL changing feels like navigation to the user, so reset the scroller (see #3119). checkIfEnabledForUrl() if windowIsFocused() # If we are in the help dialog iframe, then HelpDialog is already defined with the necessary functions. -- cgit v1.2.3