Bunpro: Jisho Button

Searches the sentence on Jisho.

  1. // ==UserScript==
  2. // @name Bunpro: Jisho Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5.11
  5. // @description Searches the sentence on Jisho.
  6. // @author Kumirei
  7. // @include *bunpro.jp/*
  8. // @exclude *community.bunpro.jp*
  9. // @require https://greasyfork.org/scripts/370623-bunpro-helpful-events/code/Bunpro:%20Helpful%20Events.js?version=974369
  10. // @require https://greasyfork.org/scripts/370219-bunpro-buttons-bar/code/Bunpro:%20Buttons%20Bar.js?version=1043642
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. ;(function () {
  15. //Wait until we're on the study page and can add the button
  16. $('HTML')[0].addEventListener('quiz-page', function () {
  17. //add button
  18. if (!$('#JishoButton').length)
  19. buttonsBar.addButton('JishoButton', 'Jisho', () =>
  20. window.open(`https://www.jisho.org/search/${parseSentence($('.study-question-japanese > div')[0])}`),
  21. )
  22.  
  23. //Bind J to the Jisho button
  24. $('#study-answer-input').on('keyup', function (e) {
  25. if (e.which == 74 && $('#submit-study-answer').attr('value') == 'Next') {
  26. $('#JishoButton').click()
  27. }
  28. })
  29. })
  30.  
  31. //Extracts the sentence from the sentence elements
  32. function parseSentence(sentenceElem) {
  33. var sentence = ''
  34.  
  35. var list = sentenceElem.childNodes
  36.  
  37. list.forEach(function (currentValue, currentIndex, listObj) {
  38. var elem = currentValue
  39. var name = currentValue.tagName
  40. var className = currentValue.className
  41.  
  42. if (name == 'SPAN') {
  43. if (['study-area-input'].includes(className)) {
  44. sentence += '____'
  45. } else if (['vocab-popout', 'gp-popout'].includes(className)) {
  46. const items = elem.childNodes
  47. items.forEach(function (item) {
  48. if (item.tagName == 'RUBY') {
  49. sentence += item.childNodes[0].data
  50. } else {
  51. sentence += item.textContent
  52. }
  53. })
  54. }
  55. } else if (name == 'RUBY') {
  56. sentence += elem.childNodes[0].data
  57. } else {
  58. if (elem instanceof HTMLElement) {
  59. sentence += elem.textContent
  60. } else if (elem instanceof Text) {
  61. sentence += elem.textContent
  62. }
  63. }
  64. })
  65.  
  66. return sentence
  67. }
  68. })()