Useless Things Series: The Line

Adds a fading line mouse tail at the top of webpages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Useless Things Series: The Line
// @version      1.2
// @description  Adds a fading line mouse tail at the top of webpages
// @match        *://*/*
// @grant        none
// @license      MIT
// @namespace    https://greasyfork.org/users/1126616
// ==/UserScript==

(function() {
  'use strict';

  const tail = createTail();

  document.body.appendChild(tail);

  function createTail() {
    const tailElement = document.createElement('div');
    tailElement.style.position = 'fixed';
    tailElement.style.top = '0';
    tailElement.style.left = '0';
    tailElement.style.width = '0';
    tailElement.style.height = '4px';
    tailElement.style.opacity = '1';
    tailElement.style.transition = 'width 0.3s linear, opacity 1s ease-out';
    return tailElement;
  }

  function updateTail(event) {
    const mouseX = event.clientX;
    const mouseY = event.clientY;

    tail.style.width = mouseX + 'px';
    tail.style.opacity = '1';
  }

  function fadeOutTail() {
    tail.style.opacity = '0';
  }

  function changeColor() {
    const randomColor = getRandomColor();
    tail.style.backgroundColor = randomColor;
  }

  function getRandomColor() {
    const letters = '0123456789ABCDEF';
    let color = '#';
    for (let i = 0; i < 6; i++) {
      color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
  }

  function fadeInTail() {
    tail.style.opacity = '1';
    changeColor();
  }

  document.addEventListener('mousemove', updateTail);

  let timeoutId;

  document.addEventListener('mousemove', function() {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(fadeOutTail, 2000);
  });

  document.addEventListener('mouseover', fadeInTail);

})();