Copy and Search URL Fragment + Page Navigator

Space to Google search current URL fragment, Q to go to previous page, E to go to next page.

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 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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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         Copy and Search URL Fragment + Page Navigator
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Space to Google search current URL fragment, Q to go to previous page, E to go to next page.
// @author       Druid
// @match        *://fitgirl-repacks.site/*
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('keydown', function(event) {
        const currentUrl = window.location.href;

        // SPACE - Google search the URL fragment
        if (event.code === 'Space') {
            event.preventDefault();

            let urlFragment = currentUrl.replace(/https?:\/\/[^/]+\//, '').replace(/-/g, ' ').replace(/\/$/, '');
            const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(urlFragment)}`;
            window.open(searchUrl, '_blank');
        }

        // Q - Go to previous page
        if (event.code === 'KeyQ') {
            event.preventDefault();

            let newUrl = currentUrl.replace(/(\/page\/)(\d+)(\/?)/, function(match, prefix, pageNum) {
                let prevPage = Math.max(parseInt(pageNum) - 1, 1);
                return `${prefix}${prevPage}/`;
            });

            // If no /page/ in URL, go to page 1
            if (!/\/page\/\d+/.test(currentUrl)) {
                newUrl = currentUrl.replace(/\/$/, '') + '/page/1/';
            }

            window.location.href = newUrl;
        }

        // E - Go to next page
        if (event.code === 'KeyE') {
            event.preventDefault();

            if (/\/page\/\d+/.test(currentUrl)) {
                let newUrl = currentUrl.replace(/(\/page\/)(\d+)(\/?)/, function(match, prefix, pageNum) {
                    let nextPage = parseInt(pageNum) + 1;
                    return `${prefix}${nextPage}/`;
                });
                window.location.href = newUrl;
            } else {
                // If no page number in URL, start with page 2
                let baseUrl = currentUrl.replace(/\/$/, '');
                window.location.href = baseUrl + '/page/2/';
            }
        }
    });
})();