Better Luogu Problem Searcher

洛谷题目跳转器优化

  1. // ==UserScript==
  2. // @name Better Luogu Problem Searcher
  3. // @namespace n/a
  4. // @version 0.0.3
  5. // @description 洛谷题目跳转器优化
  6. // @author iotang
  7. // @match https://www.luogu.com.cn
  8. // @match https://www.luogu.com.cn/
  9. // @match https://www.luogu.com.cn/#feed
  10. // @match https://www.luogu.com.cn/#feed/
  11. // @match http://www.luogu.com.cn
  12. // @match http://www.luogu.com.cn/
  13. // @match http://www.luogu.com.cn/#feed
  14. // @match http://www.luogu.com.cn/#feed/
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. (function()
  19. {
  20. 'use strict';
  21.  
  22. var href = "https://www.luogu.com.cn/problem";
  23.  
  24. function jumpfn() {
  25. var target = document.getElementsByClassName("am-form-field")[0].value;
  26. if(target === "")return;
  27.  
  28. var go = href + "/list?keyword=" + target + "&content=true";
  29.  
  30. location.href = go;
  31. }
  32.  
  33. function isProblemId(text) { // https://greasyfork.org/zh-CN/scripts/388947-luogu-problem-jumper
  34. if (text.match(/^AT[0-9]{1,4}$/) == text) return true;
  35. if (text.match(/^CF[0-9]{1,4}[A-Z][0-9]{0,1}$/) == text) return true;
  36. if (text.match(/^SP[0-9]{1,5}$/) == text) return true;
  37. if (text.match(/^P[0-9]{4}$/) == text) return true;
  38. if (text.match(/^UVA[0-9]{1,5}$/) == text) return true;
  39. if (text.match(/^U[0-9]{1,6}$/) == text) return true;
  40. if (text.match(/^T[0-9]{1,6}$/) == text) return true;
  41. return false;
  42. }
  43.  
  44. function searchfn() {
  45. var target = document.getElementsByClassName("am-form-field")[0].value;
  46. if(target === "")return;
  47.  
  48. var targetu = target.toUpperCase();
  49. var go = href;
  50.  
  51. if(target.match(/^[1-9][0-9][0-9][0-9]+$/) == target)go = go + "/P" + target;
  52. else if(targetu.match(/^[0-9]{1,4}[A-Z][0-9]{0,1}$/) == targetu)go = go + "/CF" + targetu;
  53. else if(isProblemId(targetu))go = go + "/" + targetu;
  54. else {go = href + "/list?keyword=" + target + "&content=true";}
  55.  
  56. location.href = go;
  57. }
  58.  
  59. var button =
  60. document.getElementsByClassName("lg-index-content")[0]
  61. .getElementsByClassName("lg-article lg-index-stat")[0]
  62. .getElementsByClassName("am-btn am-btn-danger am-btn-sm")[0];
  63.  
  64. button.className = "am-btn am-btn-success am-btn-sm";
  65. button.name = "gotosearch";
  66. button.id = "gotosearch";
  67. button.innerHTML = "搜索";
  68. button.onclick = function() {jumpfn();};
  69.  
  70. document.getElementsByClassName("am-form-field")[0].placeholder = "输入题号或题目名,按回车确认";
  71. document.getElementsByClassName("am-form-field")[0].onkeyup = function(event){if(event.keyCode === 13){searchfn();}};
  72.  
  73. document.getElementsByClassName("lg-article lg-index-stat")[0].getElementsByTagName("h2")[0].innerHTML = "问题搜索";
  74.  
  75. document.getElementsByName("gotorandom")[0].innerHTML = "随机";
  76. })();