Rotate Letters Script

Rotates each letter on the page randomly from 0 to 360 degrees to reset passive reading habits by forcing attention

ของเมื่อวันที่ 01-10-2025 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Rotate Letters Script
// @namespace    https://mordenstar.com
// @version      1.0
// @description  Rotates each letter on the page randomly from 0 to 360 degrees to reset passive reading habits by forcing attention
// @author       Shaun Pedicini
// @homepageURL  https://mordenstar.com
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function processTextNode(textNode) {
        const parentNode = textNode.parentNode;
        if (!parentNode) {
            return;
        }

        const text = textNode.textContent || '';
        const fragment = document.createDocumentFragment();

        for (const char of text) {
            if (/[A-Za-z]/.test(char)) {
                const span = document.createElement('span');
                span.textContent = char;
                // span.style.display = 'inline-block'; // Necessary for transform to work correctly
                // span.style.transform = `rotate(${sign * rotation}deg)`;
                // span.style.transition = 'transform 5.0s';

                const rotation = Math.floor(Math.random() * 25); // Random angle between 0 and 25
                const sign = Math.random() > 0.5 ? -1 : 1;
                const finalRotation = sign * rotation;

                span.style.display = 'inline-block'; // Necessary for transform to work correctly
                span.style.transform = `rotate(0deg)`; // Start from 0 degrees
                span.style.transition = 'transform 7.0s';
                span.style.transitionDelay = '1s'; // Delay animation by 1 second

                fragment.appendChild(span);

                // Use requestAnimationFrame to ensure the browser has rendered the initial state
                requestAnimationFrame(() => {
                    span.style.transform = `rotate(${finalRotation}deg)`; // Rotate to final angle
                });
            } else {
                fragment.appendChild(document.createTextNode(char));
            }
        }

        parentNode.replaceChild(fragment, textNode);
    }

    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            processTextNode(node);
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            const element = node;

            // Skip script, style, textarea, and input elements
            if (['SCRIPT', 'STYLE', 'TEXTAREA', 'INPUT', 'CODE', 'PRE'].includes(element.tagName)) {
                return;
            }

            // Process child nodes
            for (const child of Array.from(node.childNodes)) {
                processNode(child);
            }
        }
    }

    // Start processing from the body element
    if (document.body) {
        processNode(document.body);
    } else {
        window.addEventListener('DOMContentLoaded', () => {
            if (document.body) {
                processNode(document.body);
            }
        });
    }
})();