Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major improvements in code extensibility, reduced API usage, eliminate redundancy, etc. #348

Merged
merged 5 commits into from
Jul 11, 2018

Conversation

GaurangTandon
Copy link

@GaurangTandon GaurangTandon commented Jul 10, 2018

My main motive in this four days attempt was to first reduce the heavy API usage by introducing filters. As I got engrossed in the code further, I managed to fix issues related to code extensibility, redudancy, refactoring, caching, etc. So, this turned out to be a slightly larger update than expected.

This PR builds on the code directly from the latest branch (dev2.2.15). I have tested all the functions that I changed. They all work, except for a few quirks that were present since before.

These are the most important changes:

  1. remove synchronous ajax call - "Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user’s experience." under getFromAPI
  2. throttled all observer calls - it is a given that MutationObserver fires dozens of times a second, but the user we're designing this app for won't even notice changes faster than 250ms.
  3. the apiParameter list in common.info.json was redundant, since none of the files are actually using the corresponding methods apiParameter or metaApiParameter. So, I removed that.
  4. parseCrossSiteLinks now also works for bare links in comments
  5. four new helper functions under sox.helpers - as they are frequently used in the code
  6. redefined the constant currentAPIParameter using the helper method above

Apart from that, all the other changes fix bugs, or make the code extensible, faster, less redundant, etc. However, there are a few things I could not fix:

  1. downvotedPostsEditAlert - I don't know where to begin refactoring this from. It has three API calls, and more than four hundred lines of code. I can't even comprehend how it is doing what it's supposed to do. Perhaps, you might want to refactor it later yourself. At least reduce its length, its huge :O
  2. What is the $anchor in showQuestionStateInSuggestedEditReviewQueue? I think you forgot to define that variable.
  3. What is the need for Yahoo! YQL API in standOutDupeCloseMigrated? The SE API already provides us with migrated_from and migrated_to details.
  4. How do you handle more than one dupe links to be clicked on the clickable stamp for standOutDupeCloseMigrated? It is a known fact that moderators can mark more than one dupe targets for a single question.
  5. hotNetworkQuestionsFiltering is unable to hide those links which are under the "more hot questions" link. As soon as you click that link, all the list items under that automatically appear. I think it needs an onclick handler for that.
  6. standOutDupeCloseMigrated dupe stamp link is /q/ID when it is set, but becomes /undefined when you click on it.
  7. the getQuestionTags part of hotNetworkQuestionsFiltering has been buggy since previous version. there is no next element sibling of i.getQuestionTags

Since this is a big change, feel free to go through it manually and comment and ping me for changes on individual lines - I'll be more than happy to explain my changes. Even though I have tested all the changed methods, feel free to test them again :)

And when you're happy, we can push these changes, possibly under a new 2.3.0 version number ;)

@shu8
Copy link
Member

shu8 commented Jul 10, 2018

@GaurangTandon Wow, thanks a lot for your time! I will go through this as soon as I can, but I'm sure it'll be fine!

I'll also have a look at the points you mentioned and try fixing them too :) I was thinking of moving downvotedPostsEditAlert back out into a separate userscript too, because it's too big to maintain in SOX!

@@ -81,22 +81,19 @@

copyCommentsLink: function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were changes to this function by accident? (It seems to have rolled back my changes)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. I do recall starting work on the latest dev branch. Ah...you went from dev*.*.14 to dev*.*.15 by the time I finished work, Hadn't noticed that. Sorry! Feel free to roll back to your original.


//display:block to fix https://github.com/soscripted/sox/issues/243:
$('#question-header').css('display', 'block').prepend('<div title="SOX: this is a hot network question!" ' + (sox.location.on('english.stackexchange.com') ? 'style="padding:13px"' : '') + ' class="sox-hot"><i class="fab fa-free-code-camp"></i><div>');
$(document.getElementById('question-header')).css('display', 'block').prepend('<div title="SOX: this is a hot network question!" ' + (sox.location.on('english.stackexchange.com') ? 'style="padding:13px"' : '') + ' class="sox-hot"><i class="fab fa-free-code-camp"></i><div>');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any point in using vanilla JS just to make it a jQuery object straight after? :/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, $(document.getElementById(ID)) is quite faster than $("#ID")...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GaurangTandon fair enough, I never knew that!

@shu8 shu8 merged commit fe3c415 into soscripted:dev Jul 11, 2018
@shu8
Copy link
Member

shu8 commented Jul 11, 2018

@GaurangTandon merged! thanks again for all the changes -- it must have taken quite some time! :)

@GaurangTandon
Copy link
Author

That's wonderful :D

shu8 added a commit that referenced this pull request Jul 11, 2018
@shu8
Copy link
Member

shu8 commented Jul 11, 2018

@GaurangTandon I've fixed most of the issues you mentioned, but I couldn't reproduce the duplicate button's link being broken -- does it still happen?

I have no idea why I used YQL for the migrated questions 😆 I also had something in there to check for 'migrated from' but AFAIK the migrated notice only shows on the 'from' site?

For multiple duplicates, it just shows one of them...

I'll be taking out downvotedPostsEditAlert soon.

@GaurangTandon
Copy link
Author

GaurangTandon commented Jul 11, 2018

I couldn't reproduce the duplicate button's link being broken -- does it still happen?

If I go to this search page, and click the first duplicate link, it immediately transforms from /q/46898 into /undefined, taking me to stackoverflow.com/undefined :/ The link also changes if you right click on this link (verify in the dev tools).

I've little clue as to why this happens. If it possible that you have a rogue click event handler (probably a delegated one) somewhere in the code? :O

I'm testing on a fresh Chrome profile specifically for testing SOX only, so effect of other addons is zero.

@shu8
Copy link
Member

shu8 commented Jul 11, 2018

@GaurangTandon very weird :/ it only happens on the search page, not on any other page e.g. https://stackoverflow.com/questions.

I'll look at features that act on the search page...

@shu8
Copy link
Member

shu8 commented Jul 11, 2018

Fixed it!


If you're interested, it was:

 $(".js-search-results").on("mousedown touchstart", "div.result-link a", function (ev) {
     this.href = $(this).data("searchsession");
 });

on line 1332 of the /search page:

image

I honestly have no clue why it's there :/

I tried changing it to:

var question = data.items[0],
      questionId = question.closed_details.original_questions[0].question_id,
      $linkToAdd = $('<a/>', {
          'style': 'display: inline',
          'href': 'https://' + sox.site.url + '/q/' + questionId,
          'click': function(e) {
               e.preventDefault();
               return;
           }
      }),
      $spanToAdd = $('<span>', {
          'class': 'standOutDupeCloseMigrated-duplicate',
          'title': 'click to visit duplicate',
          'html': '&nbsp;duplicate&nbsp;'
     });
     
     //styling for https://github.com/soscripted/sox/issues/181
     $anchor.after($linkToAdd.append($spanToAdd));

but that click handler never got triggered, no idea why.

So, in the end, I just added a data-searchsession attribute to the sox span itself, so the search code can work for us rather than against!

@GaurangTandon
Copy link
Author

GaurangTandon commented Jul 12, 2018

If you're interested, it was:

Woah, how did you manage to find that line of code in the middle of nowhere o.O 10/10 great!

I honestly have no clue why it's there :/

Oh, I get it! This code must be for their internal analytics: tracking the referrer. The original .result link a had a data-searchsession attribute that reads: /questions/ID/SLUG?s=1|86.0153. The s=1|86.0153 is a query parameter that must be there to track user's clicking patterns.

but that click handler never got triggered, no idea why

I guess this is because e.preventDefault() prevents the default behavior associated with an input (for example, focusing the URL bar (default behavior) on pressing Ctrl-L (input)). The code you showed on line 1332 of the /search page is not a default behavior, so it didn't get any suppressed :(

So, in the end, I just added a data-searchsession attribute to the sox span itself, so the search code can work for us rather than against!

That is the perfect trick my friend ;) applauses

@GaurangTandon
Copy link
Author

I've fixed most of the issues you mentioned

This is really minor so I'll comment it here. getQuestionTags works but it immediately redirects to the linked question on click, because the i.getQuestionTags itself is part of the link.

@shu8
Copy link
Member

shu8 commented Jul 12, 2018 via email

@shu8
Copy link
Member

shu8 commented Jul 14, 2018

@GaurangTandon I'm looking at it now -- do you mean this box shouldn't be clickable?

image

@GaurangTandon
Copy link
Author

Aye, this box itself, and also the i.getQuestionTags icon that is there before this box appears. Both should not be a part of the link.

shu8 added a commit that referenced this pull request Jul 14, 2018
@shu8
Copy link
Member

shu8 commented Jul 14, 2018

@GaurangTandon should be fixed in dev 2.2.22 :)

@GaurangTandon
Copy link
Author

Dude....that's probably an overkill :( Those i.getQuestionTags everywhere are getting into my eyes:

image

Look, the thing is that this i.getQuestionTags function isn't going to be used too often. So, we can simply set it to show the question's tags on hovering over the question link itself. Let me share a PR.

@shu8
Copy link
Member

shu8 commented Jul 15, 2018

Fair enough, although I feel we need some visual indicator that you can hover over them, and add this to the feature's extended_desc. A simple 'hover to show tags' just under the 'Hot Network Questions' could do?

@GaurangTandon
Copy link
Author

GaurangTandon commented Jul 15, 2018

You will hover over the links to click them anyway. Once you hover over them, the tags list immediately pops out below the question link. Try it out! ;)

@shu8
Copy link
Member

shu8 commented Jul 15, 2018

The idea is to see the question tags before you click.

If you don't know to hover to show the tags, there's no reason you should hover (by clicking) at all because you just wouldn't go to the question.

@GaurangTandon
Copy link
Author

So, you're saying that once this update goes alive, any user - interested in this feature - won't hover over any HNQ question, even once, even by mistake, before thinking that showQuestionTags no longer works?

That's a bit of an overstatement in my humble opinion, considering that there is almost a negligible delay in the fetching of the tags, I have pushed another update to #351, that should make the app's response time zero. Please have a look.

@shu8
Copy link
Member

shu8 commented Jul 15, 2018

I'm saying it's not an obvious feature, and there's no need for a user to accidentally realise they can show the tags by hovering when we can just add a line of text that makes it obvious!

@GaurangTandon
Copy link
Author

Well, alright with that.

shu8 added a commit that referenced this pull request Sep 2, 2018
* new dev

* #305

* #307

* #310

* #310

* Better fix for #310

The old fix placed the SOX button to the right of the username, but the username only shows up in the top bar if you're logged in. This meant the SOX button would be absent if you were logged out. This new code should fix that.

* #310

* #309, #313

* #311, #312

* #256

* #319, #317, #316

* #256

* #318

* #294 deprecate enhanced editor

* #308, other small fixes

* #320

* minor fixes (#324)

change scroll top top feature JSON info

* fixes #329 (#330)

also reduces jQuery dependency

* v2.1.4 DEV before push to master

also adds #326

* v2.2.0

* inital DEV push

* update readme to state GM not supported; #306

* reduce dependency on `hotkeys`+minor details (#333)

* #334 fixed

* #334

* #334

* #338

* #335, #338

* Micro optimizations (#336)

* update source link

* micro optimizations

- converted jQuery .not to CSS selector for faster perf (https://stackoverflow.com/questions/8845811/performance-differences-between-using-not-and-not-selectors)
- introduced a separate function to eliminate thrice-repeated (!) code, to keep it DRY

* fixes some issues (#339)

described in #338 (comment)

* fixes #338

* fixes #338 (#340)

1. used addEventListener instead of onload
2. descriptive names (`reader`)
3. optimized extraction of the image URL from the `data`
4. made all declarations `var` and moved them into one place for consistency
5. shortened the POST url to simply `'/upload/image?https=true',` ;P

* #322 implemented

* Sticky property

* Update sox.user.js

* Updating button colors

The main SE buttons use a lighter color than the SOX buttons

* Update sox.css

* Minor fixes

Patched a problem with vote buttons, and fixed the color of the notify on edit button

* Update sox.user.js

* minor changes/fixes

* #341 temporary fix

* Updated mod diamond icon

Updated to the icon, to match the one used by the new topbar

* Updated diamond icon

* Updated mod diamond

* improvements in colorAnswerer (#344)

* improvements in colorAnswerer
1. constant variables+cache answeredID
2. reduce jQuery dependency+shorter a[href] selector via user ID
3. change coloration

* optimization: use filters throughout (#345)

reduces bandwidth consumption

* fixes a few features; adds CSS

Fixes moveBounty, dragBounty, copyCommentsLink.
Adds class for colorAnswerer.
Updates jQuery

* fixes quickAuthorInfo fontawesome icon

* fix a few of #308

* Major improvements in code extensibility, reduced API usage, eliminate redundancy, etc. (#348)

* fixes various bugs mentioned in #348

* fixes standOutDupeCloseMigrated for search pages

* remove unwanted console logs

* #347, various minor tweaks

* fixes small bug in parseCrossSiteLinks

e.g. on this comment: https://meta.stackoverflow.com/questions/370902/i-need-a-lot-of-help-to-write-a-query-for-the-stack-exchange-data-explorer-sh?cb=1#comment608548_370902

* Fixed Meta SE's chat link

Added a / to the end of Meta SE's if statement to fix it. Also replaced "discuss" with "meta" for Area 51 (to match the new URL for Area 51 Discussions), shortened a bit of code, and added some comments

* Fixed Meta SE links

* Changed topbar z-index

Changed the fixed topbar's z-index to match the one on SO, in order to prevent elements like the usercard (z-index: 1000) from covering it

* Made it clearer what the checkbox does

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Fixed Area 51 Discussions bug

* #348 minor tweak

* fix (#350)

...for answerer ID 13 also matching commenter ids beginning with 13 (1345, 13555, etc.)

* #352, #353 remove yahoo YQL use

* #352

* PR/351 (#355)

* update getQuestionTags

* provide immediate response to user

* minor changes

* #325: remove rangyInputs dependency (#354)

* bump version number

* remove downvotedPostsEditAlert; remove rangyinputs `require`; implement mini changelog in dialog

* install stale and no-response bots

* #356 added, minor bug fix

* start #349, start to move to es6

* onlyShowCommentActionsOnHover bug fix

* #357

* deprecate fixedTopbar feature for #349

* add showTagWikiLinkOnTagPopup feature

* #359 fixed

* #358 fixed

* fixes #360

* fixes for #361

* #361 changes

* v2.3.0 final
@shu8
Copy link
Member

shu8 commented Sep 2, 2018

I (finally!) got round to pushing this to master under 2.3.0! Thanks a lot for your help in this version @GaurangTandon! :) 🎉

@GaurangTandon
Copy link
Author

GaurangTandon commented Sep 2, 2018 via email

shu8 added a commit that referenced this pull request Feb 3, 2019
* new dev

* #305

* #307

* #310

* #310

* Better fix for #310

The old fix placed the SOX button to the right of the username, but the username only shows up in the top bar if you're logged in. This meant the SOX button would be absent if you were logged out. This new code should fix that.

* #310

* #309, #313

* #311, #312

* #256

* #319, #317, #316

* #256

* #318

* #294 deprecate enhanced editor

* #308, other small fixes

* #320

* minor fixes (#324)

change scroll top top feature JSON info

* fixes #329 (#330)

also reduces jQuery dependency

* v2.1.4 DEV before push to master

also adds #326

* v2.2.0

* inital DEV push

* update readme to state GM not supported; #306

* reduce dependency on `hotkeys`+minor details (#333)

* #334 fixed

* #334

* #334

* #338

* #335, #338

* Micro optimizations (#336)

* update source link

* micro optimizations

- converted jQuery .not to CSS selector for faster perf (https://stackoverflow.com/questions/8845811/performance-differences-between-using-not-and-not-selectors)
- introduced a separate function to eliminate thrice-repeated (!) code, to keep it DRY

* fixes some issues (#339)

described in #338 (comment)

* fixes #338

* fixes #338 (#340)

1. used addEventListener instead of onload
2. descriptive names (`reader`)
3. optimized extraction of the image URL from the `data`
4. made all declarations `var` and moved them into one place for consistency
5. shortened the POST url to simply `'/upload/image?https=true',` ;P

* #322 implemented

* Sticky property

* Update sox.user.js

* Updating button colors

The main SE buttons use a lighter color than the SOX buttons

* Update sox.css

* Minor fixes

Patched a problem with vote buttons, and fixed the color of the notify on edit button

* Update sox.user.js

* minor changes/fixes

* #341 temporary fix

* Updated mod diamond icon

Updated to the icon, to match the one used by the new topbar

* Updated diamond icon

* Updated mod diamond

* improvements in colorAnswerer (#344)

* improvements in colorAnswerer
1. constant variables+cache answeredID
2. reduce jQuery dependency+shorter a[href] selector via user ID
3. change coloration

* optimization: use filters throughout (#345)

reduces bandwidth consumption

* fixes a few features; adds CSS

Fixes moveBounty, dragBounty, copyCommentsLink.
Adds class for colorAnswerer.
Updates jQuery

* fixes quickAuthorInfo fontawesome icon

* fix a few of #308

* Major improvements in code extensibility, reduced API usage, eliminate redundancy, etc. (#348)

* fixes various bugs mentioned in #348

* fixes standOutDupeCloseMigrated for search pages

* remove unwanted console logs

* #347, various minor tweaks

* fixes small bug in parseCrossSiteLinks

e.g. on this comment: https://meta.stackoverflow.com/questions/370902/i-need-a-lot-of-help-to-write-a-query-for-the-stack-exchange-data-explorer-sh?cb=1#comment608548_370902

* Fixed Meta SE's chat link

Added a / to the end of Meta SE's if statement to fix it. Also replaced "discuss" with "meta" for Area 51 (to match the new URL for Area 51 Discussions), shortened a bit of code, and added some comments

* Fixed Meta SE links

* Changed topbar z-index

Changed the fixed topbar's z-index to match the one on SO, in order to prevent elements like the usercard (z-index: 1000) from covering it

* Made it clearer what the checkbox does

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Many fixes to dialog buttons

Fixes many of the bugs from #308

* Fixed Area 51 Discussions bug

* #348 minor tweak

* fix (#350)

...for answerer ID 13 also matching commenter ids beginning with 13 (1345, 13555, etc.)

* #352, #353 remove yahoo YQL use

* #352

* PR/351 (#355)

* update getQuestionTags

* provide immediate response to user

* minor changes

* #325: remove rangyInputs dependency (#354)

* bump version number

* remove downvotedPostsEditAlert; remove rangyinputs `require`; implement mini changelog in dialog

* install stale and no-response bots

* #356 added, minor bug fix

* start #349, start to move to es6

* onlyShowCommentActionsOnHover bug fix

* #357

* deprecate fixedTopbar feature for #349

* add showTagWikiLinkOnTagPopup feature

* #359 fixed

* #358 fixed

* fixes #360

* fixes for #361

* #361 changes

* v2.3.0 final

* 2.3.0 DEV

* fix for #363

* implemented 'feature packs' in settings dialog

* debugging for #363, fixes #364

* #363 debugging

* fix for #363

* #361 implemented (access token no longer mandatory); minor code cleanup

* #365

* fix #368

* Add #315

* fix #366

* start to use linting

* fix #372

* add #370

* clean up code

* implement #374

* fix #373

* add #370; deprecate pasteImagesDirectly

* #374 replace 'help' instead of prepend new link

* change indentation to 2 spaces

* lint

* implement #296

* implements #376 (linkify meta diamond dialog title)

* fix #375 (topAnswers post score not being found)

* improve customMagicLinks feature -- add button to help menu on all pages

* fix sticky vote buttons feature

* keep new meta posts under diamond even after clicking (#378)

* editComment improve design, add more defaults (#377)

* fix better css feature

* editComment: make dialog delete button smaller

* improve editComment for #377; allow editing; new setting storage format

* add delay to comment features for links to deleted comments #379

* bump to v2.4.0

* Remove accidental letter introduced into file

* Bump installation link version numbers

* Lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants