JPDB Show Pitch In Review Question

6/12/2024, 6:06:28 PM

  1. // ==UserScript==
  2. // @name JPDB Show Pitch In Review Question
  3. // @namespace JPDB_Show_Pitch_In_Review_Question
  4. // @match https://jpdb.io/review*
  5. // @grant none
  6. // @version 0.01
  7. // @author Flipp Fuzz
  8. // @description 6/12/2024, 6:06:28 PM
  9. // @run-at document-idle
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. // Handling of answer is copied from https://greasyfork.org/en/scripts/459772-jpdb-auto-reveal-answer-sentence/code
  16. // -こう- implemented the switch from question to answer without really navigating
  17. // by simply replacing the content and location.href on clicking the "show answer" button
  18. window.onload = observeUrlChange(addPitchToQuestion);
  19.  
  20. // this handles refreshing the page while on the answer screen
  21. addPitchToQuestion();
  22. })();
  23.  
  24. function observeUrlChange(onChange) {
  25. let oldHref = document.location.href;
  26. const body = document.querySelector("body");
  27. const observer = new MutationObserver(mutations => {
  28. mutations.forEach(() => {
  29. if (oldHref !== document.location.href) {
  30. oldHref = document.location.href;
  31. onChange();
  32. }
  33. });
  34. });
  35. observer.observe(body, { childList: true, subtree: true });
  36. }
  37.  
  38. function addPitchToQuestion() {
  39. if(new URL(location.href).searchParams.get('c')) {
  40. let pitchSearchResults = document.querySelectorAll('body > div.container.bugfix > div > div.review-reveal > div.result.vocabulary > div > div.subsection-pitch-accent > div > div > div > div');
  41. let insertAfterElement = document.querySelector('body > div.container.bugfix > div > div.review-reveal > div.answer-box > div.plain > a');
  42.  
  43. if(!pitchSearchResults || pitchSearchResults.length == 0 || !insertAfterElement) {
  44. console.log(`pitchSearchResults: ${pitchSearchResults}`);
  45. console.log(`insertAfterElement: ${insertAfterElement}`);
  46. return;
  47. }
  48.  
  49. // Create a div to add our new pitches into
  50. let containerDiv = document.createElement("div");
  51. containerDiv.setAttribute('class', 'answer-pitch-container');
  52. containerDiv.style.fontSize = "50%";
  53. containerDiv.style.paddingLeft = "5px";
  54. insertAfterElement.insertAdjacentElement('afterend', containerDiv);
  55.  
  56. pitchSearchResults.forEach(pitch => {
  57. containerDiv.appendChild(pitch.cloneNode(true));
  58. });
  59. }
  60. }