diff --git a/lib/modules/keyboardNav.js b/lib/modules/keyboardNav.js index 87f960338d..e3c038cd50 100644 --- a/lib/modules/keyboardNav.js +++ b/lib/modules/keyboardNav.js @@ -1,17 +1,20 @@ /* @flow */ import $ from 'jquery'; -import { once, maxBy, without, memoize } from 'lodash-es'; +import { maxBy, memoize, once, without } from 'lodash-es'; import { Module } from '../core/module'; import * as Modules from '../core/modules'; +import * as Options from '../core/options'; +import { i18n, openNewTab } from '../environment'; import { NAMED_KEYS, - Thing, SelectedThing, + Thing, click, - downcast, currentSubreddit, currentUserProfile, + downcast, + execRegexes, filterMap, getPercentageVisibleYAxis, hashKeyArray, @@ -22,21 +25,21 @@ import { loggedInUser, matchesPageLocation, niceKeyCode, + regexes, scrollToElement, string, - watchForElements, waitForEvent, + watchForElements, } from '../utils'; -import { i18n, openNewTab } from '../environment'; -import * as Options from '../core/options'; import * as CommandLine from './commandLine'; +import * as CommentDepth from './commentDepth'; import * as CommentNavigator from './commentNavigator'; import * as EasterEgg from './easterEgg'; import * as FilteReddit from './filteReddit'; import * as HideChildComments from './hideChildComments'; import * as NeverEndingReddit from './neverEndingReddit'; -import * as Notifications from './notifications'; import * as NoParticipation from './noParticipation'; +import * as Notifications from './notifications'; import * as SaveComments from './saveComments'; import * as SettingsNavigation from './settingsNavigation'; import * as ShowImages from './showImages'; @@ -1201,7 +1204,41 @@ function followPermalink(newWindow = false) { function followComments(newWindow = false, selected = getSelected()) { const a = assertElement(selected.getCommentsLink()); - navigateTo(a.href, { newWindow }); + const url = new URL(a.href, location.href); + + // no need to proceed if depth already exists in the query string + if (url.searchParams.has('depth')) { + navigateTo(a.href, { newWindow }); + } + + if (regexes.commentPermalink.test(url.pathname) && ( + !CommentDepth.module.options.commentPermalinks.value || + !CommentDepth.module.options.commentPermalinksContext.value && url.searchParams.has('context') + )) { + navigateTo(a.href, { newWindow }); + } + + const matches = execRegexes.comments(url.pathname); + if (!matches) navigateTo(a.href, { newWindow }); + + // check for subreddit specific values + const subreddit = matches && matches[1].toLowerCase(); + const [, commentDepth, minimumComments] = ( + CommentDepth.module.options.subredditCommentDepths.value.find(([subreddits]) => subreddits.toLowerCase().split(',').includes(subreddit)) || + [null, CommentDepth.module.options.defaultCommentDepth.value, CommentDepth.module.options.defaultMinimumComments.value] + ); + + // NaN or 0 (show everything) + if (!parseInt(commentDepth, 10)) navigateTo(a.href, { newWindow }); + + // if the comment count is less than the minimum, show everything + const minimumCount = parseInt(minimumComments, 10); + if (minimumCount && selected && selected.isPost() && (selected.getCommentCount() || 0) < minimumCount) { + navigateTo(a.href, { newWindow }); + } + + url.searchParams.set('depth', commentDepth); + navigateTo(url.href, { newWindow }); } function vote(way: 'downmod' | 'upmod', preventToggle = false, selected = getSelected()) { diff --git a/locales/locales/en.json b/locales/locales/en.json index ee83b04370..20f0b40ed9 100644 --- a/locales/locales/en.json +++ b/locales/locales/en.json @@ -217,7 +217,7 @@ "message": "Custom Comment Depth" }, "commentDepthDesc": { - "message": "Allows you to set the preferred depth of comments you wish to see when clicking on comments links.\n\n0 = Everything, 1 = Root level, 2 = Responses to root level, 3 = Responses to responses to root level, etc." + "message": "Allows you to set the preferred depth of comments you wish to see when clicking on comments links or navigate using the Follow Comments keyboard shortcut.\n\n0 = Everything, 1 = Root level, 2 = Responses to root level, 3 = Responses to responses to root level, etc." }, "commentHidePerName": { "message": "Comment Hide Persistor"