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

JS refactoring part 2: helper functions, poyfills #3084

Merged
merged 17 commits into from
Jun 8, 2022

Conversation

AHOHNMYC
Copy link
Contributor

@AHOHNMYC AHOHNMYC commented May 6, 2022

Added script _helpers.js which includes portion of polyfills for IE11, clamping function and handy wrappers for:

  • XHR (for GET/POST, with optional retries and good for Invidious default parameters)
  • storage (localStorage with fallback to cookie)

To improve code readability and complexity:

  • some condition were reversed to remove negation
  • some function were renamed
  • added bunch of comments
  • some constants are replaced with their names (4 is MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED)
  • complex conditions are taken out to variables with a speaking name

Added polyfills usage where it is possible to reduce code and improve readability

Removed redundant window and document usage. window is global object and some properties like location are doubled in document

Repeated pieces of code are moved to global variables in the beginning of files

Constructions like function (x) {otherFunction(x)} replaced with just otherFunction(x)

Removed useless try-catches (but added one useful)

Rewritten themes management

Added support for per-frame navigation with , and . keys #2113. But "honest per-frame" will work only for 29.97 FPS videos

Fixes #2412 using preventDefault()

All new code checked to work with IE11

@AHOHNMYC AHOHNMYC changed the title Js helpers polyfills JS refactoring part 2: helper functions, poyfills May 6, 2022
@AHOHNMYC AHOHNMYC marked this pull request as ready for review May 12, 2022 11:50
@AHOHNMYC AHOHNMYC requested a review from a team as a code owner May 12, 2022 11:50
@AHOHNMYC AHOHNMYC requested review from unixfox and removed request for a team May 12, 2022 11:50
@AHOHNMYC
Copy link
Contributor Author

@SamantazFox please, deploy this PR on test.invidious.io 😸
I hope, this is the last monstrous PR with JS.

@AHOHNMYC AHOHNMYC mentioned this pull request May 12, 2022
8 tasks
@SamantazFox
Copy link
Member

@AHOHNMYC it's deployed!

Seems like a few things don't work, like player hotkeys. Maybe the trace below can help?

Uncaught ReferenceError: helpers is not defined
    remove_all_video_times https://test.invidious.io/js/player.js?v=059796c6:452
    <anonymous> https://test.invidious.io/js/player.js?v=059796c6:305
player.js:452:5
    remove_all_video_times https://test.invidious.io/js/player.js?v=059796c6:452
    <anonymous> https://test.invidious.io/js/player.js?v=059796c6:305

Uncaught ReferenceError: helpers is not defined
    setTheme https://test.invidious.io/js/themes.js?v=059796c6:30
    <anonymous> https://test.invidious.io/js/themes.js?v=059796c6:53
themes.js:30:9
    setTheme https://test.invidious.io/js/themes.js?v=059796c6:30
    <anonymous> https://test.invidious.io/js/themes.js?v=059796c6:53

Uncaught ReferenceError: helpers is not defined
    get_youtube_comments https://test.invidious.io/js/watch.js?v=059796c6:226
    <anonymous> https://test.invidious.io/js/watch.js?v=059796c6:339
watch.js:226:5
    get_youtube_comments https://test.invidious.io/js/watch.js?v=059796c6:226
    <anonymous> https://test.invidious.io/js/watch.js?v=059796c6:339

Uncaught ReferenceError: helpers is not defined
    <anonymous> https://test.invidious.io/js/notifications.js?v=059796c6:77
notifications.js:77:9
    <anonymous> https://test.invidious.io/js/notifications.js?v=059796c6:77

@AHOHNMYC
Copy link
Contributor Author

@SamantazFox after second applying _helpers.js was successfully loaded on page. But I've found bunch of bugs 🙀 :

  • keys for frame walking were reverted
  • JSON XHR in IE11 returned String instead of Object
  • Filters panel in IE11 didn't collapse
  • Theme changing unfortunately got recursive call

All this issues were fixed. Please, reapply this commit third time 😺

Optional {} and _helpers.js imports (one import in <head> would be enough, absolutely agree) are for next commits

@SamantazFox
Copy link
Member

@AHOHNMYC Done, I've deployed your latest commit :)

@AHOHNMYC
Copy link
Contributor Author

@SamantazFox thanks! I've tested almost everything.
Invidious in IE11 now looks usable. Comments are finally working! Filter panel is collapsing,
I think, it is really ready to review :)

Copy link
Member

@SamantazFox SamantazFox left a comment

Choose a reason for hiding this comment

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

Overall, it looks very good to me! I've made some small remarks, in addition to unifox's comment.

assets/js/notifications.js Outdated Show resolved Hide resolved
assets/js/notifications.js Outdated Show resolved Hide resolved
assets/js/player.js Outdated Show resolved Hide resolved
assets/js/player.js Outdated Show resolved Hide resolved
assets/js/player.js Show resolved Hide resolved
AHOHNMYC and others added 2 commits May 19, 2022 07:15
Co-authored-by: Samantaz Fox <coding@samantaz.fr>
partial rewrite notifications.js
innerText to textContent
fixed bug with clamping
assets/js/notifications.js Outdated Show resolved Hide resolved
assets/js/notifications.js Outdated Show resolved Hide resolved
extra spaces removed
@AHOHNMYC AHOHNMYC requested a review from SamantazFox May 30, 2022 07:46
@AHOHNMYC
Copy link
Contributor Author

AHOHNMYC commented May 31, 2022

Added six strings fixes #3089

@SamantazFox
Copy link
Member

SamantazFox commented Jun 4, 2022

@AHOHNMYC can you please put the <script> for _helpers.js in the template file rather than every .ecr file?

(Put it right before </head>:)

<link rel="stylesheet" href="/css/pure-min.css?v=<%= ASSET_COMMIT %>">
<link rel="stylesheet" href="/css/grids-responsive-min.css?v=<%= ASSET_COMMIT %>">
<link rel="stylesheet" href="/css/ionicons.min.css?v=<%= ASSET_COMMIT %>">
<link rel="stylesheet" href="/css/default.css?v=<%= ASSET_COMMIT %>">
</head>

Copy link
Member

@SamantazFox SamantazFox left a comment

Choose a reason for hiding this comment

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

Minor whitespace errors ^^

assets/js/_helpers.js Outdated Show resolved Hide resolved
assets/js/community.js Outdated Show resolved Hide resolved
assets/js/notifications.js Outdated Show resolved Hide resolved
assets/js/notifications.js Outdated Show resolved Hide resolved
assets/js/player.js Outdated Show resolved Hide resolved
assets/js/player.js Outdated Show resolved Hide resolved
assets/js/watch.js Outdated Show resolved Hide resolved
assets/js/watch.js Outdated Show resolved Hide resolved
assets/js/watch.js Outdated Show resolved Hide resolved
assets/js/player.js Outdated Show resolved Hide resolved
@SamantazFox SamantazFox merged commit 2313ca8 into iv-org:master Jun 8, 2022
@SamantazFox
Copy link
Member

Thanks a lot for your contribution to invidious! The JS rewrite is giving a ton of fresh air to the project ^^

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.

[Bug] Closing a reddit comment thread redirects to homepage
3 participants