Generic URL ID Manipulator

Extract ID from various URLs, display it, and enable keyboard navigation

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Generic URL ID Manipulator
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Extract ID from various URLs, display it, and enable keyboard navigation
// @author       You
// @match        https://*/*
// @match        http://*/*
// @license      LGPL
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 提取URL中的ID
    function extractIdFromUrl(url) {
        // 正则表达式可以根据需要调整以匹配不同网站的ID格式
        const idMatch = url.match(/(\d+)\.(html|jsp|php|aspx?|do|action|servlet|cfm|py|pl)(\?.*)?(#.*)?$/);
        return idMatch ? parseInt(idMatch[1], 10) : null;
    }

    // 显示ID在页面右下角
    function displayId(id) {
        const displayDivId = 'tampermonkey-id-display';
        let displayElement = document.getElementById(displayDivId);

        if (!displayElement) {
            displayElement = document.createElement('div');
            displayElement.id = displayDivId;
            displayElement.style.position = 'fixed';
            displayElement.style.bottom = '10px';
            displayElement.style.right = '10px';
            displayElement.style.fontSize = '36px';
            displayElement.style.color = 'orange';
            document.body.appendChild(displayElement);
        }

        if (id !== null) {
            displayElement.textContent = id;
            displayElement.style.display = 'block';
        } else {
            displayElement.style.display = 'none';
        }
    }

    // 根据当前ID和新ID构建新的URL
    function buildNewUrl(currentUrl, newId) {
        // 根据网站的不同,构建新URL的逻辑可能有所不同
        const urlParts = currentUrl.split('/');
        const lastPart = urlParts.pop();
        const fileExtensionMatch = lastPart.match(/(\.\w+)(\?.*)?(#.*)?$/);
        if (fileExtensionMatch) {
            urlParts.push(newId + fileExtensionMatch[0]);
        }
        return urlParts.join('/');
    }

    // 跳转至新页面
    function navigate(direction) {
        const currentUrl = window.location.href;
        const currentId = extractIdFromUrl(currentUrl);

        if (currentId !== null) {
            const newId = direction === 'right' ? currentId + 1 : currentId - 1;
            const newUrl = buildNewUrl(currentUrl, newId);
            window.location.href = newUrl;
        }
    }

    // 绑定键盘事件
    document.addEventListener('keydown', function(event) {
        if (event.ctrlKey && event.key === 'ArrowRight') {
            navigate('right');
        } else if (event.ctrlKey && event.key === 'ArrowLeft') {
            navigate('left');
        }
    });

    // 初始化
    const id = extractIdFromUrl(window.location.href);
    displayId(id);
})();