JPDB Show Pitch In Review Question

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        JPDB Show Pitch In Review Question
// @namespace   JPDB_Show_Pitch_In_Review_Question
// @match       https://jpdb.io/review*
// @grant       none
// @version     0.01
// @author      Flipp Fuzz
// @description 6/12/2024, 6:06:28 PM
// @run-at      document-idle
// @license MIT
// ==/UserScript==

(function() {
  'use strict';
  // Handling of answer is copied from https://greasyfork.org/en/scripts/459772-jpdb-auto-reveal-answer-sentence/code
  // -こう- implemented the switch from question to answer without really navigating
  // by simply replacing the content and location.href on clicking the "show answer" button
  window.onload = observeUrlChange(addPitchToQuestion);

  // this handles refreshing the page while on the answer screen
  addPitchToQuestion();
})();

function observeUrlChange(onChange) {
  let oldHref = document.location.href;
  const body = document.querySelector("body");
  const observer = new MutationObserver(mutations => {
    mutations.forEach(() => {
      if (oldHref !== document.location.href) {
        oldHref = document.location.href;
        onChange();
      }
    });
  });
  observer.observe(body, { childList: true, subtree: true });
}

function addPitchToQuestion() {
  if(new URL(location.href).searchParams.get('c')) {
    let pitchSearchResults = document.querySelectorAll('body > div.container.bugfix > div > div.review-reveal > div.result.vocabulary > div > div.subsection-pitch-accent > div > div > div > div');
    let insertAfterElement = document.querySelector('body > div.container.bugfix > div > div.review-reveal > div.answer-box > div.plain > a');

    if(!pitchSearchResults || pitchSearchResults.length == 0 || !insertAfterElement) {
      console.log(`pitchSearchResults: ${pitchSearchResults}`);
      console.log(`insertAfterElement: ${insertAfterElement}`);
      return;
    }

    // Create a div to add our new pitches into
    let containerDiv = document.createElement("div");
    containerDiv.setAttribute('class', 'answer-pitch-container');
    containerDiv.style.fontSize = "50%";
    containerDiv.style.paddingLeft = "5px";
    insertAfterElement.insertAdjacentElement('afterend', containerDiv);

    pitchSearchResults.forEach(pitch => {
        containerDiv.appendChild(pitch.cloneNode(true));
    });
  }
}