URL Page Navigator

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

目前為 2024-01-07 提交的版本,檢視 最新版本

// ==UserScript==
// @name      URL Page Navigator
// @version   1.6
// @description Increment an integer value in the URL with keyboard shortcuts to go next page or previous page
// @grant     none
// @match     *://*/*
// @license   MIT
// @namespace https://greasyfork.org/users/875241
// ==/UserScript==

(function() {
 'use strict';

 function incrementIntegerValue(path) {
   let regex = /(\d+)(?=.*\d)/;
   let match = path.match(regex);
   if (match) {
       let number = parseInt(match[0], 10);
       let incrementedNumber = number + 1;
       if(number >= 1000 && number <= 9999){
           let secondLastMatch = path.match(/(\d+)(?=.*\d)/g)[1];
           let secondLastNumber = parseInt(secondLastMatch, 10);
           return path.replace(new RegExp(secondLastMatch, 'g'), secondLastNumber + 1);
       } else {
           return path.replace(regex, incrementedNumber);
       }
   }
   return path;
 }

 function handleShortcut(event) {
   if (event.altKey && event.key === 'k') {
       event.preventDefault();
       const currentUrl = window.location.href;
       const urlComponents = currentUrl.split('/');
       const currentPath = urlComponents.slice(3).join('/');
       const incrementedPath = incrementIntegerValue(currentPath);
       if (incrementedPath !== currentPath) {
           urlComponents[3] = incrementedPath;
           const incrementedUrl = urlComponents.join('/');
           window.location.href = incrementedUrl;
       }
   }
 }

 document.addEventListener('keydown', handleShortcut);
})();