🏠 Home 

WaniKani No Scroll

Don't scroll on "Show Information"

  1. // ==UserScript==
  2. // @name WaniKani No Scroll
  3. // @namespace http://www.wanikani.com
  4. // @version 0.1.4
  5. // @description Don't scroll on "Show Information"
  6. // @author polv
  7. // @match *://www.wanikani.com/*
  8. // @match *://preview.wanikani.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
  10. // @license MIT
  11. // @homepage https://github.com/patarapolw/wanikani-userscript/blob/master/userscripts/no-scroll.user.js
  12. // @grant none
  13. // ==/UserScript==
  14. // @ts-check
  15. (function () {
  16. 'use strict';
  17. const el = {
  18. quiz: /** @type {HTMLElement | null} */ (null),
  19. addtionalContent: /** @type {HTMLElement | null} */ (null),
  20. };
  21. const noScroll = ({ target }) => {
  22. target.scrollTop = 0;
  23. };
  24. const contentLoadedObserver = new MutationObserver((muts) => {
  25. const { quiz } = el;
  26. if (!quiz) return;
  27. for (const mut of muts) {
  28. const { target } = mut;
  29. if (
  30. target instanceof HTMLElement &&
  31. target.getAttribute('data-loaded') === 'true'
  32. ) {
  33. quiz.addEventListener('scroll', noScroll);
  34. setTimeout(() => {
  35. quiz.removeEventListener('scroll', noScroll);
  36. }, 500);
  37. break;
  38. }
  39. }
  40. });
  41. let intervalFindElement = 0;
  42. const startScript = () => {
  43. stopScript();
  44. intervalFindElement = setInterval(() => {
  45. el.addtionalContent = document.querySelector('turbo-frame#subject-info');
  46. if (!el.addtionalContent) return;
  47. el.quiz = document.querySelector('.quiz');
  48. if (!el.quiz) return;
  49. contentLoadedObserver.observe(el.addtionalContent, {
  50. attributes: true,
  51. attributeFilter: ['data-loaded'],
  52. });
  53. stopScript();
  54. }, 500);
  55. };
  56. const stopScript = () => {
  57. if (intervalFindElement) {
  58. clearInterval(intervalFindElement);
  59. intervalFindElement = 0;
  60. }
  61. };
  62. startScript();
  63. window.addEventListener('turbo:load', (e) => {
  64. // @ts-ignore
  65. const url = e.detail.url;
  66. if (!url) return;
  67. if (/(session|quiz|review|extra_study|subject-lessons)/.test(url)) {
  68. startScript();
  69. } else {
  70. stopScript();
  71. }
  72. });
  73. })();