Greasy Fork

页面ID快速跳转

显示当前页面ID,并允许通过快捷键增减ID来跳转页面

当前为 2024-09-09 提交的版本,查看 最新版本

// ==UserScript==  
// @name         页面ID快速跳转  
// @namespace    http://tampermonkey.net/  
// @version      0.1  
// @description  显示当前页面ID,并允许通过快捷键增减ID来跳转页面  
// @author       Your Name  
// @match        *://*/*  
// @license      LGPL
// @grant        none  
// ==/UserScript==   
  
(function() {  
    'use strict';  
  
    // 存储解析后的URL部分  
    let baseUrl = '';  
    let pageId = '';  
    let extension = '';  
  
    // 解析URL的函数  
    function parseUrl() {  
        const url = window.location.href;  
        const regex = /([^/]*\/?)([^/\.]*?)(\.(html|jsp|htm|php))$/i;  
        const match = url.match(regex);  
  
        if (match) {  
            baseUrl = match[1];  
            pageId = match[2];  
            extension = match[3];  
  
            // 检查pageId是否全为数字  
            if (!/^\d+$/.test(pageId)) {  
                pageId = '';  
            }  
        }  
  
        updateDisplay();  
    }  
  
    // 更新页面ID显示  
    function updateDisplay() {  
        const displayDiv = document.getElementById('pageIdDisplay');  
        if (!displayDiv) {  
            const newDiv = document.createElement('div');  
            newDiv.id = 'pageIdDisplay';  
            newDiv.style.position = 'fixed';  
            newDiv.style.bottom = '20px';  
            newDiv.style.right = '20px';  
            newDiv.style.fontSize = '36px';  
            newDiv.style.color = 'orange';  
            newDiv.style.zIndex = '10000';  
            newDiv.style.padding = '5px 10px';  
            newDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';  
            newDiv.style.borderRadius = '10px';  
            document.body.appendChild(newDiv);  
        }  
        displayDiv.textContent = 'Page ID: ' + (pageId || 'Not Found');  
    }  
  
    // 处理键盘快捷键  
    document.addEventListener('keydown', function(event) {  
        if (!pageId) return;  
  
        const currentId = parseInt(pageId, 10);  
        let newId = '';  
  
        if (event.ctrlKey && event.key === 'ArrowRight') {  
            newId = (currentId + 1).toString();  
        } else if (event.ctrlKey && event.key === 'ArrowLeft') {  
            newId = (currentId - 1).toString();  
        }  
  
        if (newId) {  
            const newUrl = baseUrl + newId + extension;  
            window.location.href = newUrl;  
        }  
    });  
  
    // 页面加载完成后解析URL  
    window.onload = parseUrl;  
})();