reddit-moderator-toolbox icon indicating copy to clipboard operation
reddit-moderator-toolbox copied to clipboard

Refactor watchPushState to be more fault-tolerant

Open eritbh opened this issue 6 years ago • 2 comments

The current system for watchPushState (https://github.com/toolbox-team/reddit-moderator-toolbox-redesign/blob/18f82c8/extension/data/tbutils.js#L2903-L3059) relies on lots of individual regular expressions for every possible page type. One of the drawbacks to this method is that sometimes, the context object details don't include subreddit even if we're clearly in a subreddit. An obvious case of this was #68, where wiki pages didn't work, but I want to rewrite this section to handle individual parts of the URL on their own instead of just detecting full URLs, so things like subreddit can be there even on unknown page types.

Also, calling a whole bunch of regex methods going down that chain is probably not the most efficient. That can probably be refactored.

eritbh avatar May 25 '19 23:05 eritbh

Actually is there anywhere else in the code we have to get information about a URL? It might make sense to split this off into a getURLInfo helper kinda like the getThingInfo helpers. It would return info like whether the URL is in a subreddit, what post/comment IDs there are, what wiki page is pointed to, and then we can just call that method with the current URL from this section.

eritbh avatar Aug 03 '20 13:08 eritbh

As #344 is a dupe I'll put that info here. A possible solution for this would be to break down the path into an array based on each element. We could then basically use that to go through there based on the first element of the path, then the second, etc.

So for example if the first key in the array is an r we know it is subreddit related and can pass it through something that further dissects subreddit urls.

@Geo1088 I don't really think we do have the need for this sort of information elsewhere out the top of my head but I could be wrong. It couldn't hurt to make it into a getURLInfo function or something related as there probably are a few places where we actually do something similar (though afaik we mostly do it the other way around to create reddit urls)

creesch avatar Aug 03 '20 13:08 creesch