BunPro: JLPT Percentage

Adds percentages to the progress bars.

  1. // ==UserScript==
  2. // @name BunPro: JLPT Percentage
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.12
  5. // @description Adds percentages to the progress bars.
  6. // @author Kumirei
  7. // @match https://bunpro.jp/*
  8. // @require https://greasyfork.org/scripts/432418-wait-for-selector/code/Wait%20For%20Selector.js?version=974366
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. ;(function ($, wfs) {
  13. $('head').append(
  14. '<style id="BunProPercentageScript">' +
  15. ' .profile-jlpt-level .progress .percentage {' +
  16. ' position: absolute; ' +
  17. ' left: 50%;' +
  18. ' line-height: 13px;' +
  19. ' font-size: 12px;' +
  20. ' margin-top: -1px;' +
  21. ' transform: translate(-50%,1px);' +
  22. ' text-shadow: 1px 0px black;' +
  23. ' }' +
  24. '</style>',
  25. )
  26. const percentNumberToString = (percentNumber) => {
  27. if (percentNumber === 0) {
  28. return '0%' // instead of "0.00%";
  29. } else if (percentNumber === 100) {
  30. return '100%'
  31. } else {
  32. return (percentNumber.toFixed(1) + '%').replace('.0%', '%')
  33. }
  34. }
  35. wfs.wait('.profile-jlpt-level .progress-bar', function (e) {
  36. var percentNumber = Number($(e).attr('aria-valuenow'))
  37. var percentString = percentNumberToString(percentNumber)
  38.  
  39. const parentNode = $(e.parentNode)
  40. for (const child of parentNode[0].children) {
  41. if (child.classList.contains('percentage')) {
  42. return
  43. }
  44. }
  45. parentNode.append('<span class="percentage">' + percentString + '</span>')
  46. })
  47.  
  48. wfs.wait('.profile-jlpt-level', function (e) {
  49. if (!$('.profile-jlpt-level.total').length) {
  50. var bar = $('.profile-jlpt-level')[0].cloneNode(true)
  51. bar.className += ' total'
  52. $(bar).find('.percentage').remove()
  53. bar.childNodes[1].innerText = 'Tot'
  54. var barelem = $(bar).find('.progress-bar')
  55. var total = 0
  56. var learned = 0
  57. $('[id$="jlpt_level_progress_bars"] .progress-count').each(function (i, e) {
  58. var counts = e.childNodes[0].textContent.split('/')
  59. total += Number(counts[1])
  60. learned += Number(counts[0])
  61. })
  62. var percentage = (learned / total) * 100
  63. barelem.attr('aria-valuenow', percentage)
  64. barelem.attr('style', 'width: ' + percentage + '%;')
  65. barelem.parent().append('<span class="percentage">' + percentNumberToString(percentage) + '</span>')
  66. $(bar).find('.progress-count')[0].innerText = String(learned) + '/' + String(total)
  67. var lastbar = $('.profile-jlpt-level')
  68. $(lastbar[lastbar.length - 1]).after(bar)
  69. }
  70. })
  71. })(window.jQuery, window.wfs)