Fandom press ESC for search + tweaks

when you press esc on a fandom wiki it will now open and select the search bar, also makes the page wider and disables highlight actions

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fandom press ESC for search + tweaks
// @namespace    https://github.com/Alistair1231/my-userscripts/
// @version      0.2.5
// @description  when you press esc on a fandom wiki it will now open and select the search bar, also makes the page wider and disables highlight actions
// @author       Alistair1231
// @match        *://*.fandom.com/wiki/*
// @grant      GM.addStyle
// @license      MIT
// ==/UserScript==
// https://greasyfork.org/en/scripts/454358-fandom-press-esc-for-search-widescreen
// https://openuserjs.org/scripts/Alistair1231/Fandom_press_ESC_for_search_+_widescreen
// https://github.com/Alistair1231/my-userscripts/raw/master/fandom-press-esc-for-search.user.js

(function () {
    'use strict';

    GM.addStyle(`
    #highlight__main-container {
        display: none;
    }
    `);

    const qS = (x) => document.querySelector(x);
    // const qSA = (x) => document.querySelectorAll(x);

    function resizeContainer() {
        var resizableContainer = qS(".resizable-container");
        if (resizableContainer && resizableContainer.offsetLeft > 200) {
            resizableContainer.style.maxWidth = "80%";
        }else{
            resizableContainer.style.maxWidth = "100%";
        }
    }


    // observe for changes in the document body (this is for when a new page is clicked)
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            // check if the mutation is a node insertion
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                for (var i = 0; i < mutation.addedNodes.length; i++) {
                    resizeContainer();
                }
            }
        });
    });

    // resize the container on window resize
    window.addEventListener('resize', function () {
        resizeContainer();
    });

    // resize the container if already present on the page
    resizeContainer();



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

    var active = false;


    window.addEventListener('keydown', function (e) {
        if (e.key === "Escape") {
            e.preventDefault();
            console.log(active);
            // close search if it is open
            if (active) {
                document.getElementById('firstHeading').click();
                active = false;
            }
            // open search
            else {
                const searchButton = document.querySelector("header.fandom-community-header a.wiki-tools__search[title='Search']");
                if (searchButton) {
                    searchButton.click();
                    active = true;
                }
            }
        }
    });




})();