URL Page Navigator

Increment or decrement 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.2
  4. // @description Increment or decrement 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. const regex = /(\d+)(?!.*\d)/;
  16. const match = url.match(regex);
  17. if (match) {
  18. const number = parseInt(match[0], 10);
  19. const incrementedNumber = number + 1;
  20. return url.replace(regex, incrementedNumber);
  21. }
  22. return url;
  23. }
  24.  
  25. function decrementIntegerValue(url) {
  26. const regex = /(\d+)(?!.*\d)/;
  27. const match = url.match(regex);
  28. if (match) {
  29. const number = parseInt(match[0], 10);
  30. const decrementedNumber = number - 1;
  31. return url.replace(regex, decrementedNumber);
  32. }
  33. return url;
  34. }
  35.  
  36. function handleShortcut(event) {
  37. if (event.altKey && event.key === 'k') {
  38. event.preventDefault();
  39. const currentUrl = window.location.href;
  40. const incrementedUrl = incrementIntegerValue(currentUrl);
  41. if (incrementedUrl !== currentUrl) {
  42. window.location.href = incrementedUrl;
  43. }
  44. } else if (event.altKey && event.key === 'l') {
  45. event.preventDefault();
  46. const currentUrl = window.location.href;
  47. const decrementedUrl = decrementIntegerValue(currentUrl);
  48. if (decrementedUrl !== currentUrl) {
  49. window.location.href = decrementedUrl;
  50. }
  51. }
  52. }
  53.  
  54. document.addEventListener('keydown', handleShortcut);
  55. })();