Greasy Fork

Greasy Fork is available in English.

Generic URL ID Manipulator

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
})();