Greasy Fork is available in English.
2023/6/12 19:20:21
当前为
// ==UserScript==
// @name 自动隐藏抖音header搜索
// @namespace 鼠标显示隐藏抖音header搜索
// @match https://www.douyin.com/*
// @namespace 476321082
// @license MIT
// @grant none
// @version 0.2
// @author -
// @description 2023/6/12 19:20:21
// ==/UserScript==
(function () {
'use strict';
const style = document.createElement('style');
style.textContent = `
.MiecXVmm.gCMIwEM1.WPC30Qlr {
height: 0;
}
`;
document.head.appendChild(style);
const target = document.getElementById('douyin-header');
let height = target.offsetHeight;
let hidden = false;
let menu = null;
let isFirst = true;
let isObserving = false;
let lastMouseMoveTime = 0;
const THROTTLE_DELAY = 100;
let isReady = false;
document.addEventListener('readystatechange', () => {
if (document.readyState === 'complete') {
isReady = true;
init();
}
});
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 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();
}
})();