URL Page ID Manipulator

Parse URL and manipulate page ID. Licensed under LGPL.

  1. // ==UserScript==
  2. // @name URL Page ID Manipulator
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Parse URL and manipulate page ID. Licensed under LGPL.
  6. // @match *://*/*
  7. // @license LGPL
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // 解析 URL
  15. function parseURL(url) {
  16. const regex = /^(.*?)(\d+)(\.[a-z]+)$/;
  17. const match = url.match(regex);
  18. if (match) {
  19. return {
  20. frontURL: match[1],
  21. pageID: parseInt(match[2]),
  22. backURL: match[3]
  23. };
  24. }
  25. return null;
  26. }
  27.  
  28. // 更新页面 ID 显示
  29. function updatePageIDDisplay(pageID) {
  30. const style = document.createElement('style');
  31. style.textContent = `
  32. .page-id-display {
  33. position: fixed;
  34. bottom: 0;
  35. right: 0;
  36. font-size: 36px;
  37. color: orange;
  38. }
  39. `;
  40. document.head.appendChild(style);
  41. let displayElement = document.querySelector('.page-id-display');
  42. if (!displayElement) {
  43. displayElement = document.createElement('div');
  44. displayElement.classList.add('page-id-display');
  45. document.body.appendChild(displayElement);
  46. }
  47. displayElement.textContent = pageID;
  48. }
  49.  
  50. // 监听按键事件
  51. document.addEventListener('keydown', function(event) {
  52. const urlData = parseURL(window.location.href);
  53. if (urlData) {
  54. if (event.ctrlKey && event.key === 'ArrowRight') {
  55. urlData.pageID++;
  56. window.location.href = urlData.frontURL + urlData.pageID + urlData.backURL;
  57. } else if (event.ctrlKey && event.key === 'ArrowLeft') {
  58. urlData.pageID--;
  59. window.location.href = urlData.frontURL + urlData.pageID + urlData.backURL;
  60. }
  61. }
  62. });
  63.  
  64. const urlData = parseURL(window.location.href);
  65. if (urlData) {
  66. updatePageIDDisplay(urlData.pageID);
  67. }
  68. })();