Greasy Fork is available in English.

Leetcode problem acceptance assistant

Show the problem's acceptance on the problem page

  1. // ==UserScript==
  2. // @name Leetcode problem acceptance assistant
  3. // @namespace http://halfcrazy.me
  4. // @version 0.1
  5. // @description Show the problem's acceptance on the problem page
  6. // @author halfcrazy
  7. // @match http*://oj.leetcode.com/problems/*
  8. // @grant none
  9. // ==/UserScript==
  10. function getElementsByClassName(node,classname){
  11. if(node.getElementsByClassName){
  12. return node.getElementsByClassName(classname);
  13. }else{
  14. var results = new Array();
  15. var elems = node.getElementsByTag("*");
  16. for (var i=0;i<elems.length;i++){
  17. if(elems[i].className.indexOf(classname) != -1){
  18. results[elems.length] = elems[i];
  19. }
  20. }
  21. return results;
  22. }
  23. }
  24.  
  25. function changeTwoDecimal(x)
  26. {
  27. var f_x = parseFloat(x);
  28. if (isNaN(f_x))
  29. {
  30. alert('function:changeTwoDecimal->parameter error');
  31. return false;
  32. }
  33. f_x = Math.round(f_x *100)/100;
  34. return f_x;
  35. }
  36.  
  37. function insertAfter(newElement, targetElement)
  38. {
  39. var parent = targetElement.parentNode;
  40. if(parent.lastChild == targetElement)
  41. {
  42. parent.appendChild(newElement);
  43. }
  44. else
  45. {
  46. parent.insertBefore(newElement, targetElement.nextSibling);
  47. }
  48. }
  49.  
  50. var question_title = getElementsByClassName(document,"question-title");
  51. if(typeof(question_title[0]) == "undefined")
  52. {
  53. return;
  54. }
  55. else
  56. {
  57. var Total_Accepted = question_title[0].childNodes[3].getElementsByTagName("strong");
  58. var Total_Submissions = question_title[0].childNodes[5].getElementsByTagName("strong");
  59. var accepted = Total_Accepted[0].innerText;
  60. var submissions = Total_Submissions[0].innerText;
  61. var rate = changeTwoDecimal(accepted/submissions*100);
  62. var newNode = document.createElement("span");
  63. newNode.setAttribute("class", "total-ac text-info");
  64. newNode.innerHTML = "Accepted Rate: <strong>"+rate+"%"+"</strong>";
  65. insertAfter(newNode,question_title[0].childNodes[5]);
  66. }