Skip to content
New issue

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

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

Already on GitHub? # to your account

(Bug): Dislikes count on the userscript are not correct on some locales #520

Closed
Izheil opened this issue Mar 31, 2022 · 5 comments · Fixed by #540
Closed

(Bug): Dislikes count on the userscript are not correct on some locales #520

Izheil opened this issue Mar 31, 2022 · 5 comments · Fixed by #540
Labels
bug Something isn't working

Comments

@Izheil
Copy link

Izheil commented Mar 31, 2022

Browser

Firefox, Vivaldi

Browser Version

Firefox Stable 98.0.2, Vivaldi 5.1.2567.66 (Stable channel)

Extension or Userscript?

Userscript

Extension/Userscript Version

0.9.0

Video link where you see the problem

https://www.youtube.com/watch?v=VgfIxCsg8xY

What happened?

The dislike returned by the api was 1412, but the dislikes count showed 1,4M.

The issue seems to be on the numberFormat function of the userscript, most specifically the locale part of it, since just replacing all the contents of the function with this makes it work as it should:

function numberFormat(numberState) {
  return numberState.toLocaleString();
}

That is without using the roundDown function, but using it works all the same, I just prefered to see the actual number instead of the rounding.

How to reproduce/recreate?

Install userscript on violentmonkey, use spanish locale, and go to the video link of the problem.
Happens on both Firefox and Vivaldi (Vivaldi uses the rendering engine of chrome 98 at the point of testing)

@Izheil Izheil added the bug Something isn't working label Mar 31, 2022
@Izheil
Copy link
Author

Izheil commented Mar 31, 2022

A better fix would probably be to not set the rounding until it reaches a million, just like what happens for likes, so the code would be kept as-is with just that first check:

function numberFormat(numberState) {
  let userLocales;
  
  if (numberState < 1000000)
    return numberState.toLocaleString();
  
  try {
    userLocales = new URL(
      Array.from(document.querySelectorAll("head > link[rel='search']"))
        ?.find((n) => n?.getAttribute("href")?.includes("?locale="))
        ?.getAttribute("href")
    )?.searchParams?.get("locale");
  } catch {}
  const formatter = Intl.NumberFormat(
    document.documentElement.lang || userLocales || navigator.language,
    {
      notation: "compact",
    }
  );

  return formatter.format(roundDown(numberState));
}

@cyrildtm
Copy link
Contributor

cyrildtm commented Apr 1, 2022

It is a known issue. Youtube uses a different number format than the current standard. Unfortunately it's Youtube making things harder for everyone, not us.

This issue happens for certain locales, and even differently for some other locales. We try to accommodate all user's needs while maintaining consistency. See issues #364 #201 #407
There will be a new feature for extension users that let them choose 1) whether to display the long format or short format in their locales (a bit more complicated than that) and 2) whether to round down the numbers at all. See PR #435 and changelog https://github.com/Anarios/return-youtube-dislike/blob/19b582fb9d379ae69c3b6bd92c71bb5a339dbdcc/Extensions/combined/changelog/images/number_format.jpg

I guess we can do something similar to Userscript, but this time we need a way to let users choose without using GUI.

One thing we want to avoid is to implement a formatter that works for one locale but breaks for another. For now the extension's implementation seems to be an acceptable workaround -- let the users have full control.

What is your locale string?

navigator.language
'en-US'

@Izheil
Copy link
Author

Izheil commented Apr 1, 2022

My locale is 'es-ES'.

The workaround for the extension seems to be acceptable considering the issues you mentioned, as for the userscript, one simple way to do the same would be to just set a constant at the start of the script letting them choose which of those 3 ways that the extension already offers should be used.

Maybe adding some comment at the start of the script like

// EDITABLE SECTION START

/* This variable changes how the dislikes are shown. Acceptable values are:
   - "rounded" -> K for a thousand, M for a million
   - "rounded-locale" -> Uses the numbering of your machine language
   - "exact" -> Sets the dislikes as the actual number of them, without rounding */
const dislikesCounterDisplay = "rounded-locale";

// EDITABLE SECTION END

I think people using userscripts should at least check the code they are installing, so it would at least be seen there (or at least, the option will exist for those that look for it).

Another option would be to insert an element with the option inputs that is only shown on hover of the 10px of the bottom/side of the window, or some dialog that shows when you press a specific keyboard shortcut, the problem with that would be that it could affect extensions that also do that, so the constant approach would probably be the best option.

@cyrildtm
Copy link
Contributor

cyrildtm commented Apr 1, 2022

I like the simple approach, since it's a one-time set up and you use the same settings forever.
Just one heads up, the upcoming extension's implementation is gonna be always using the Intl formatter with three options: "compact long", "compact short" and "standard"
Another switch will turn off rounding down. So you should be able to see "123.456" if you use "standard" and no round down.

 Intl.NumberFormat(
    'es-es',
    {
      notation: 'standard',
      compactDisplay: 'short',
    }) .format(1234567)
'1.234.567'

 Intl.NumberFormat(
    'es-es',
    {
      notation: 'compact',
      compactDisplay: 'short',
    }) .format(1234567)
'1,2 M'

 Intl.NumberFormat(
    'es-es',
    {
      notation: 'compact',
      compactDisplay: 'long',
    }) .format(1234567)
'1,2 millones'

All the above three combinations are "locale", while you could see different results if you enable round-down, which changes the number before sending it to the formatter.

Your "rounded" option sounds like the old way before Intl became a thing, at least from what I learned from that SO thread cited above. It doesn't work for you anyway, so hope you are still happy if it's not implemented. AFAIK many folks don't like it either.

Another thing is are you seeing like count numbers as exact numbers? Because I haven't figured out why it's happening. At least one other user using the Spanish version had the same problem. See #364 (comment) After logging in the like counter changes. I didn't ask that user's locale string but judging from the number's appearance it looks like 'es-es'
I just didn't know why Youtube returns an exact number for 'es-es', and I still don't. I vaguely remember Intl returns exact numbers for certain locale groups even with 'compact short' specified, but as you can see from my above console output that's not the case.

@Izheil
Copy link
Author

Izheil commented Apr 1, 2022

Yeah, it was just an example, the naming should be whatever is easier to implement, or what you think best represents each thing.

And yep, I'm seeing them as full numbers when it's a small count:
Full likes

Even when it's over 100k like in this video it's shown in full number:
Full likes K

When it's a bigger number (over 1M) like in this video it shows with the shortening:
Full likes M

It happens the same on Vivaldi as well, but the userscript doesn't change the likes for what I see (although it does have a setLikes function declared that isn't used anywhere), so I assume that Youtube just sets things different depending on the region, since the Intl method with the compact parameter in the userscript would return "mil" for thousand and "M" for million if it was used even on the K's shortening.

cyrildtm added a commit to cyrildtm/return-youtube-dislike that referenced this issue Apr 18, 2022
cyrildtm added a commit to cyrildtm/return-youtube-dislike that referenced this issue Apr 18, 2022
cyrildtm added a commit to cyrildtm/return-youtube-dislike that referenced this issue Apr 20, 2022
Userscript custom colors

using user options Anarios#539

Userscript user options

introduce user options to userscript. Using existing options available in extension for now, plus showing update popup.
Part of Anarios#539

userscript custom number formats

using user options Anarios#539
resolves Anarios#520
cyrildtm added a commit to cyrildtm/return-youtube-dislike that referenced this issue Apr 24, 2022
Userscript custom colors

using user options Anarios#539

Userscript user options

introduce user options to userscript. Using existing options available in extension for now, plus showing update popup.
Part of Anarios#539

userscript custom number formats

using user options Anarios#539
resolves Anarios#520
cyrildtm added a commit to cyrildtm/return-youtube-dislike that referenced this issue Apr 24, 2022
Userscript custom colors

using user options Anarios#539

Userscript user options

introduce user options to userscript. Using existing options available in extension for now, plus showing update popup.
Part of Anarios#539

userscript custom number formats

using user options Anarios#539
resolves Anarios#520
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants