Luogu Problem Jumper

双击题号,自动跳转

  1. // ==UserScript==
  2. // @name Luogu Problem Jumper
  3. // @version 2.0
  4. // @description 双击题号,自动跳转
  5. // @author C2020陈铭浩
  6. // @match https://www.luogu.com.cn/*
  7. // @match https://*.blog.luogu.com.cn/*
  8. // @grant none
  9. // @namespace Anguei
  10. // ==/UserScript==
  11.  
  12. function isProblemId(text) {
  13. if (text.match(/AT[0-9]{1,4}/) == text) return true;
  14. if (text.match(/CF[0-9]{1,4}[A-Z][0-9]{0,1}/) == text) return true;
  15. if (text.match(/SP[0-9]{1,5}/) == text) return true;
  16. if (text.match(/P[0-9]{4}/) == text) return true;
  17. if (text.match(/UVA[0-9]{1,5}/) == text) return true;
  18. if (text.match(/U[0-9]{1,6}/) == text) return true;
  19. if (text.match(/T[0-9]{1,6}/) == text) return true;
  20. return false;
  21. }
  22.  
  23. function jump() {
  24. var selection = window.getSelection();
  25. var selected = selection.toString().replace(' ', '').toUpperCase();
  26. var url;
  27.  
  28. if (event.ctrlKey) {
  29. var myBlog = document.querySelectorAll('.ops>a[href*=blog]')[0];
  30. url = myBlog.href + 'solution-';
  31. } else {
  32. url = 'https://www.luogu.com.cn/problem/';
  33. }
  34.  
  35. if (isProblemId(selected)) {
  36. window.open(url + selected);
  37. }
  38. }
  39.  
  40. function jumpMobile() {
  41. var selection = window.getSelection();
  42. var selected = selection.toString().replace(' ', '').toUpperCase();
  43. var url = window.location.href;
  44.  
  45. if (isProblemId(selected)) {
  46. var parent = selection.anchorNode.parentNode;
  47. if (parent.className == 'am-comment-bd' &&
  48. parent.parentNode.innerHTML.match(/href="\/space\/show\?uid=3"/) != undefined) {
  49. window.open('https://' + url.match(/uid=([0-9]+)/)[1] + '.blog.luogu.com.cn/solution-' + selected);
  50. } else {
  51. window.open('https://www.luogu.com.cn/problem/' + selected);
  52. }
  53. }
  54. }
  55.  
  56. function checkMobile() {
  57. var ua = navigator.userAgent;
  58. return window.screen.width / window.screen.height < 0.6 ||
  59. ua.indexOf('Android') > -1;
  60. }
  61.  
  62. if (checkMobile()) {
  63. document.addEventListener('selectionchange', jumpMobile);
  64. } else {
  65. document.ondblclick = jump;
  66. }