Backspace to Go Back

Go back to the previous page using the backspace key

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Backspace to Go Back
// @namespace    gobackxFIRKx
// @description  Go back to the previous page using the backspace key
// @version      1.03
// @author       xFIRKx
// @match        *://*/*
// @grant        none
// @homepageURL  https://greasyfork.org/it/scripts/495770-backspace-to-go-back
// ==/UserScript==

(function() {
    'use strict';

    function handleKeydown(event) {
        // Check if the pressed key is the backspace key
        if ((event.key === 'Backspace' || event.keyCode === 8) && !event.defaultPrevented) {
            // Check if the focus is not on an editable element
            const activeElement = document.activeElement;
            const isInput = activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA' || activeElement.isContentEditable;
            const isReadOnly = activeElement.readOnly || activeElement.disabled;

            if (!isInput || isReadOnly) {
                event.preventDefault(); // Prevent the default action
                window.history.back(); // Go back to the previous page
            }
        }
    }

    // Add event listener for keydown events
    document.addEventListener('keydown', handleKeydown, true); // Use capture mode to ensure the event is caught early

    // Use MutationObserver to handle dynamic content changes
    const observer = new MutationObserver(() => {
        // Reattach the event listener if necessary
        document.removeEventListener('keydown', handleKeydown, true);
        document.addEventListener('keydown', handleKeydown, true);
    });

    // Observe changes in the document body
    observer.observe(document.body, { childList: true, subtree: true });

    // Additional observer for YouTube's specific dynamic content
    if (window.location.hostname.includes('youtube.com')) {
        const ytObserver = new MutationObserver(() => {
            const contentElement = document.querySelector('ytd-app');
            if (contentElement) {
                ytObserver.observe(contentElement, { childList: true, subtree: true });
            }
        });

        ytObserver.observe(document.body, { childList: true, subtree: true });
    }
})();