Greasy Fork

Greasy Fork is available in English.

自动隐藏抖音header搜索

2023/6/12 19:20:21

当前为 2023-06-16 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        自动隐藏抖音header搜索
// @namespace   鼠标显示隐藏抖音header搜索
// @match       https://www.douyin.com/*
// @namespace   476321082
// @license      MIT
// @grant       none
// @version     0.4
// @author      -
// @description 2023/6/12 19:20:21
// ==/UserScript==


(function () {
    'use strict';
    const style = document.createElement('style');
    style.textContent = `
      .MiecXVmm.gCMIwEM1 {
        height: 0;
      }
      .MiecXVmm .adfr96Gm.RNsmkkvd {
        top: -80px;
      }
    `;
    document.head.appendChild(style);
    let target = null;
    let height = 0;
    let hidden = false;
    let menu = null;
    let isFirst = true;
    let isObserving = false;
    let lastMouseMoveTime = 0;
    const THROTTLE_DELAY = 100;
    let isReady = false;
    let intervalId = null;
     function init() {
        if (target.style.display !== 'none') {
            target.style.display = 'none';
            hidden = true;
        }
        document.addEventListener('mousemove', throttle(onMouseMove, THROTTLE_DELAY), { passive: true });
        target.addEventListener('mouseover', onMouseOver);
        observe();
    }
     function waitForHeader() {
        intervalId = setInterval(() => {
            target = document.getElementById('douyin-header');
            if (target) {
                clearInterval(intervalId);
                height = target.offsetHeight;
                init();
            }
        }, 1000);
    }
     document.addEventListener('readystatechange', () => {
        if (document.readyState === 'complete') {
            isReady = true;
            waitForHeader();
        }
    });
    function throttle(fn, delay) {
        let timer = null;
        return function (...args) {
            const currentTime = Date.now();
            if (!timer || currentTime - timer >= delay) {
                fn.apply(this, args);
                timer = currentTime;
            }
        };
    }
    function onMouseMove(e) {
        const y = e.clientY;
        if (y <= height / 2) {
            if (hidden) {
                target.style.display = 'block';
                hidden = false;
                height = target.offsetHeight;
            }
        } else if (y > height && !menuContains(y)) {
            if (!hidden) {
                target.style.display = 'none';
                hidden = true;
                menu = null;
            }
        }
    }
    function menuContains(y) {
        if (menu) {
            const menuHeight = menu.offsetHeight;
            const menuRect = menu.getBoundingClientRect();
            const menuTop = menuRect.top;
            const menuBottom = menuRect.bottom;
            return y >= menuTop && y <= menuBottom;
        }
        return false;
    }
    function onMouseOver(e) {
        if (e.target.tagName === 'DIV' && target.contains(e.target)) {
            isFirst = true;
        }
    }
    function observe() {
        if (isObserving) {
            return;
        }
        const observer = new MutationObserver(throttle(handleMutation, THROTTLE_DELAY));
        const options = {
            childList: true,
            subtree: true,
            attributes: true,
            attributeFilter: ['class']
        };
        observer.observe(target, options);
        isObserving = true;
    }
    function handleMutation(mutations) {
        for (const mutation of mutations) {
            if (mutation.addedNodes.length > 0 && isFirst) {
                for (const node of mutation.addedNodes) {
                    if (node.tagName === 'DIV' && target.contains(node)) {
                        menu = node;
                        isFirst = false;
                    }
                }
            }
            if (
                mutation.target.tagName === 'DIV' &&
                mutation.attributeName === 'class'
            ) {
                menu = mutation.target;
            }
        }
    }
    if (isReady) {
        init();
    }
})();