Greasy Fork is available in English.

Leetcode Points

see monthly progress of leetcode points

Fra 23.05.2022. Se den seneste versjonen.

  1. // ==UserScript==
  2. // @license MIT
  3. // @name Leetcode Points
  4. // @namespace https://github.com/pk2sd
  5. // @version 0.1
  6. // @description see monthly progress of leetcode points
  7. // @author https://leetcode.com/pK2015/
  8. // @match https://leetcode.com/store/
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
  10. // @require https://code.jquery.com/jquery-3.6.0.min.js
  11. // @grant none
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. $('document').ready(() => {
  16. $.ajax({
  17. url: 'https://leetcode.com/points/api/'
  18. }).done((response) => {
  19. parseDataAndAddSummary(response)
  20. })
  21.  
  22. let toTable = (map) => {
  23. let mapToTr = (totr) => {
  24. return Array.from(totr).sort((a, b) => {
  25. return parseInt(b[1]) - parseInt(a[1])
  26. }).map((entry) => {
  27. let key = entry[0]
  28. let value = entry[1]
  29. return '<tr>'
  30. + '<td>'
  31. + key
  32. + '</td>'
  33. + '<td>'
  34. + value
  35. + '</td>'
  36. + '</tr>'
  37. }).reduce((prev, cur) => prev + cur, '')
  38.  
  39. }
  40. return '<table>'
  41. + '<thead>'
  42. + '<tr>'
  43. + '<th>'
  44. + 'Activity'
  45. + '</th>'
  46. + '<th>'
  47. + 'Points'
  48. + '</th>'
  49. + '</tr>'
  50. + '</thead>'
  51. + '<tbody>'
  52. + mapToTr(map)
  53. + '</tbody>'
  54. + '</table>'
  55.  
  56.  
  57. }
  58.  
  59. let parseDataAndAddSummary = (points) => {
  60. const month = new Date().getMonth()
  61. const year = new Date().getFullYear()
  62. const npoints = points.scores.map(point => {
  63. return {...point, date: new Date(Date.parse(point.date))}
  64. }).filter(p => p.date.getMonth() == month && p.date.getFullYear() == year)
  65.  
  66. const total = npoints.map(p => p.score).reduce((x,y) => x+y,0);
  67. const activityToPointsMap = npoints.reduce((map, curPoint) => {
  68. if(map.has(curPoint.description)){
  69. map.set(curPoint.description, map.get(curPoint.description) + curPoint.score)
  70. } else {
  71. map.set(curPoint.description, curPoint.score)
  72. }
  73. return map
  74. }, new Map())
  75.  
  76. $('<p>', {id: 'points_p'}).prependTo('body')
  77. $('#points_p').html('<h4> Total points: ' + total + '</h4><br/>' + toTable(activityToPointsMap))
  78. $('#points_p').css('display', 'flex')
  79. $('#points_p').css('align-items', 'center')
  80. $('#points_p').css('justify-content', 'center')
  81. }
  82. })