URL Page Navigator

Increment an integer value in the URL with keyboard shortcuts to go next page or previous page

当前为 2024-01-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name URL Page Navigator
  3. // @version 1.5
  4. // @description Increment an integer value in the URL with keyboard shortcuts to go next page or previous page
  5. // @grant none
  6. // @match *://*/*
  7. // @license MIT
  8. // @namespace https://greasyfork.org/users/875241
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. function incrementIntegerValue(url) {
  15. let regex = /(\d+)(?=.*\d)/;
  16. let match = url.match(regex);
  17. if (match) {
  18. let number = parseInt(match[0], 10);
  19. let incrementedNumber = number + 1;
  20. if(number >= 1000 && number <= 9999){
  21. let secondLastMatch = url.match(/(\d+)(?=.*\d)/g)[1];
  22. let secondLastNumber = parseInt(secondLastMatch, 10);
  23. return url.replace(new RegExp(secondLastMatch, 'g'), secondLastNumber + 1);
  24. } else {
  25. return url.replace(regex, incrementedNumber);
  26. }
  27. }
  28. return url;
  29. }
  30.  
  31. function handleShortcut(event) {
  32. if (event.altKey && event.key === 'k') {
  33. event.preventDefault();
  34. const currentUrl = window.location.href;
  35. const incrementedUrl = incrementIntegerValue(currentUrl);
  36. if (incrementedUrl !== currentUrl) {
  37. window.location.href = incrementedUrl;
  38. }
  39. }
  40. }
  41.  
  42. document.addEventListener('keydown', handleShortcut);
  43. })();