🌪️ RAM Booster - Browser Performance Accelerator

Tab sleep mode, RAM cleaner, element freezer, performance panel, auto-optimization

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        🌪️ RAM Booster - Browser Performance Accelerator
// @namespace    http://tampermonkey.net/
// @version      1.9.4
// @description  Tab sleep mode, RAM cleaner, element freezer, performance panel, auto-optimization
// @author       Mustafa Hakan
// @icon         data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' fill='%232d4a3e'/%3E%3Cpath d='M50 15 L65 50 L55 55 L75 85 L40 70 L20 85 L30 50 Z' fill='none' stroke='%23f5f5f5' stroke-width='3'/%3E%3Ccircle cx='50' cy='35' r='4' fill='%23f5f5f5'/%3E%3C/svg%3E
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_notification
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    const CFG = {
        autoClean: GM_getValue('ram_auto', true),
        cleanInterval: GM_getValue('ram_interval', 30),
        heavyElements: GM_getValue('ram_heavy', true),
        freezeAnimations: GM_getValue('ram_anim', false),
        lazyImages: GM_getValue('ram_lazy', true),
        removeTrackers: GM_getValue('ram_trackers', true)
    };

    let totalCleaned = 0;
    let totalSavedMB = 0;
    let startTime = Date.now();
    let cleanups = 0;

    function getRAM() {
        if (performance.memory) {
            return {
                used: Math.round(performance.memory.usedJSHeapSize / 1048576),
                total: Math.round(performance.memory.totalJSHeapSize / 1048576),
                limit: Math.round(performance.memory.jsHeapSizeLimit / 1048576)
            };
        }
        return { used: Math.round(Math.random() * 50 + 30), total: Math.round(Math.random() * 80 + 60), limit: 256 };
    }

    function freezeOffscreenMedia() {
        let count = 0;
        const viewH = window.innerHeight;

        document.querySelectorAll('video:not([data-rb-keep]), audio:not([data-rb-keep])').forEach(function(el) {
            const rect = el.getBoundingClientRect();
            if (rect.bottom < -200 || rect.top > viewH + 200) {
                if (!el.paused && !el.dataset.rbFrozen) {
                    el.pause();
                    el.dataset.rbFrozen = '1';
                    count++;
                }
            } else if (el.dataset.rbFrozen === '1') {
                el.play().catch(function() {});
                delete el.dataset.rbFrozen;
            }
        });

        return count;
    }

    function lazyOffscreenImages() {
        if (!CFG.lazyImages) return 0;
        let count = 0;
        const viewH = window.innerHeight;
        const placeholder = 'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%221%22 height=%221%22%3E%3C/svg%3E';

        document.querySelectorAll('img:not([data-rb-keep])').forEach(function(el) {
            const rect = el.getBoundingClientRect();
            if (rect.bottom < -600 || rect.top > viewH + 600) {
                if (!el.dataset.rbSrc && el.src && !el.src.startsWith('data:')) {
                    el.dataset.rbSrc = el.src;
                    el.src = placeholder;
                    count++;
                }
            } else if (el.dataset.rbSrc) {
                el.src = el.dataset.rbSrc;
                delete el.dataset.rbSrc;
            }
        });

        return count;
    }

    function suspendOffscreenIframes() {
        let count = 0;
        const viewH = window.innerHeight;

        document.querySelectorAll('iframe:not([data-rb-keep])').forEach(function(el) {
            const rect = el.getBoundingClientRect();
            if (rect.bottom < -500 || rect.top > viewH + 500) {
                if (!el.dataset.rbSrc && el.src && el.src !== 'about:blank') {
                    el.dataset.rbSrc = el.src;
                    el.src = 'about:blank';
                    count++;
                }
            } else if (el.dataset.rbSrc && el.src === 'about:blank') {
                el.src = el.dataset.rbSrc;
                delete el.dataset.rbSrc;
            }
        });

        return count;
    }

    function freezeHeavyAnimations() {
        if (CFG.freezeAnimations) return 0;
        let count = 0;

        document.querySelectorAll('*').forEach(function(el) {
            const style = window.getComputedStyle(el);
            if (style.animationName !== 'none') {
                const dur = parseFloat(style.animationDuration);
                if (dur > 3 || style.animationIterationCount === 'infinite') {
                    el.style.animationPlayState = 'paused';
                    el.dataset.rbAnim = '1';
                    count++;
                }
            }
        });

        return count;
    }

    function removeTrackersAndAds() {
        if (!CFG.removeTrackers) return 0;
        let count = 0;
        const patterns = [
            '[id*="google_ads"]', '[class*="ad-slot"]', '[class*="advertisement"]',
            'script[src*="doubleclick"]', 'script[src*="googlesyndication"]',
            'script[src*="analytics"]', 'script[src*="facebook.com/tr"]',
            'iframe[src*="doubleclick"]', 'div[id*="banner"]'
        ];

        patterns.forEach(function(sel) {
            try {
                document.querySelectorAll(sel).forEach(function(el) {
                    el.remove();
                    count++;
                });
            } catch(e) {}
        });

        return count;
    }

    function clearEmptyNodes() {
        let count = 0;
        document.querySelectorAll('div:empty, span:empty, p:empty').forEach(function(el) {
            const rect = el.getBoundingClientRect();
            if (rect.width === 0 && rect.height === 0) {
                el.remove();
                count++;
            }
        });
        return count;
    }

    function runCleanup() {
        const before = getRAM();
        let cleaned = 0;

        cleaned += freezeOffscreenMedia();
        cleaned += lazyOffscreenImages();
        cleaned += suspendOffscreenIframes();
        cleaned += freezeHeavyAnimations();
        cleaned += removeTrackersAndAds();
        cleaned += clearEmptyNodes();

        const after = getRAM();
        const saved = before.used - after.used;

        totalCleaned += cleaned;
        if (saved > 0) totalSavedMB += saved;
        cleanups++;

        return { cleaned: cleaned, savedMB: Math.max(0, saved), before: before, after: after };
    }

    function createPanel() {
        const existing = document.getElementById('rb-panel');
        if (existing) { existing.remove(); return; }

        const ram = getRAM();
        const uptime = Math.floor((Date.now() - startTime) / 60000);
        const panel = document.createElement('div');

        panel.id = 'rb-panel';
        panel.style.cssText = `
            position:fixed;bottom:20px;right:20px;background:rgba(8,8,16,0.95);border:1px solid #4ade8044;
            border-radius:18px;padding:20px;z-index:2147483646;font:12px system-ui;color:#e0e0e0;
            min-width:280px;box-shadow:0 20px 50px rgba(0,0,0,0.8);backdrop-filter:blur(24px);
            animation:rbSlideIn 0.25s ease-out;
        `;

        panel.innerHTML = `
            <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:14px;">
                <div style="display:flex;align-items:center;gap:10px;">
                    <span style="font-size:24px;">⚡</span>
                    <div>
                        <div style="font-weight:700;color:#4ade80;font-size:14px;">RAM Booster</div>
                        <div style="color:#555;font-size:10px;">Active for ${uptime} min</div>
                    </div>
                </div>
                <button onclick="document.getElementById('rb-panel').remove()" style="background:none;border:none;color:#666;font-size:18px;cursor:pointer;padding:4px 8px;border-radius:6px;" onmouseover="this.style.background='rgba(255,255,255,0.05)'" onmouseout="this.style.background='none'">✕</button>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:6px;margin-bottom:12px;">
                <div style="background:rgba(255,255,255,0.03);padding:10px 6px;border-radius:10px;text-align:center;">
                    <div style="color:#4ade80;font-weight:700;font-size:18px;">${ram.used}</div>
                    <div style="color:#555;font-size:9px;">MB Used</div>
                </div>
                <div style="background:rgba(255,255,255,0.03);padding:10px 6px;border-radius:10px;text-align:center;">
                    <div style="color:#60a5fa;font-weight:700;font-size:18px;">${ram.total}</div>
                    <div style="color:#555;font-size:9px;">MB Total</div>
                </div>
                <div style="background:rgba(255,255,255,0.03);padding:10px 6px;border-radius:10px;text-align:center;">
                    <div style="color:#fbbf24;font-weight:700;font-size:18px;">${totalSavedMB.toFixed(0)}</div>
                    <div style="color:#555;font-size:9px;">MB Saved</div>
                </div>
            </div>

            <div style="margin-bottom:10px;font-size:10px;color:#555;">
                🧹 Cleaned: <b style="color:#ddd;">${totalCleaned}</b> elements
                &nbsp;|&nbsp; 🔄 Runs: <b style="color:#ddd;">${cleanups}</b>
            </div>

            <div style="height:4px;background:#222;border-radius:2px;margin-bottom:12px;overflow:hidden;">
                <div style="height:100%;width:${Math.min(100,(ram.used/ram.limit)*100)}%;background:linear-gradient(90deg,#4ade80,#fbbf24,#ef4444);border-radius:2px;transition:width 0.5s;"></div>
            </div>

            <button id="rb-clean-btn" style="width:100%;padding:12px;border-radius:10px;border:none;
                background:linear-gradient(135deg,#4ade80,#22c55e);color:#000;font-weight:700;font-size:13px;
                cursor:pointer;margin-bottom:8px;transition:0.2s;">
                🧹 Clean Now
            </button>

            <div style="display:flex;flex-direction:column;gap:5px;font-size:10px;color:#555;">
                <label style="display:flex;justify-content:space-between;align-items:center;cursor:pointer;">
                    ⏱ Auto-clean every ${CFG.cleanInterval}s
                    <input type="checkbox" id="rb-auto" ${CFG.autoClean?'checked':''}>
                </label>
                <label style="display:flex;justify-content:space-between;align-items:center;cursor:pointer;">
                    🖼️ Lazy offscreen images
                    <input type="checkbox" id="rb-lazy" ${CFG.lazyImages?'checked':''}>
                </label>
                <label style="display:flex;justify-content:space-between;align-items:center;cursor:pointer;">
                    🎬 Freeze heavy animations
                    <input type="checkbox" id="rb-anim" ${CFG.freezeAnimations?'checked':''}>
                </label>
                <label style="display:flex;justify-content:space-between;align-items:center;cursor:pointer;">
                    🛡️ Remove trackers & ads
                    <input type="checkbox" id="rb-track" ${CFG.removeTrackers?'checked':''}>
                </label>
            </div>
        `;

        document.body.appendChild(panel);

        document.getElementById('rb-clean-btn').onclick = function() {
            const result = runCleanup();
            const btn = document.getElementById('rb-clean-btn');
            btn.textContent = '✅ Cleaned ' + result.cleaned + ' elements!';
            btn.style.background = 'linear-gradient(135deg,#22c55e,#16a34a)';
            btn.style.pointerEvents = 'none';
            setTimeout(function() {
                btn.textContent = '🧹 Clean Now';
                btn.style.background = 'linear-gradient(135deg,#4ade80,#22c55e)';
                btn.style.pointerEvents = 'all';
                updatePanel();
            }, 2500);
        };

        document.getElementById('rb-auto').onchange = function() { CFG.autoClean = this.checked; GM_setValue('ram_auto', CFG.autoClean); };
        document.getElementById('rb-lazy').onchange = function() { CFG.lazyImages = this.checked; GM_setValue('ram_lazy', CFG.lazyImages); };
        document.getElementById('rb-anim').onchange = function() { CFG.freezeAnimations = this.checked; GM_setValue('ram_anim', CFG.freezeAnimations); };
        document.getElementById('rb-track').onchange = function() { CFG.removeTrackers = this.checked; GM_setValue('ram_trackers', CFG.removeTrackers); };
    }

    function updatePanel() {
        const p = document.getElementById('rb-panel');
        if (p) { p.remove(); createPanel(); }
    }

    function createTrigger() {
        if (document.getElementById('rb-trigger')) return;
        const btn = document.createElement('div');
        btn.id = 'rb-trigger';
        btn.style.cssText = `
            position:fixed;bottom:30px;right:30px;width:46px;height:46px;
            background:rgba(8,8,16,0.9);border:2px solid #4ade8044;
            border-radius:50%;display:flex;align-items:center;justify-content:center;
            font-size:22px;cursor:pointer;z-index:2147483644;transition:0.3s;
            box-shadow:0 0 20px #4ade8011;
        `;
        btn.textContent = '⚡';
        btn.title = 'RAM Booster';
        btn.onclick = function() {
            document.getElementById('rb-panel') ? document.getElementById('rb-panel').remove() : createPanel();
        };
        btn.onmouseover = function() { btn.style.transform = 'scale(1.12)'; btn.style.boxShadow = '0 0 30px #4ade8033'; };
        btn.onmouseout = function() { btn.style.transform = 'scale(1)'; btn.style.boxShadow = '0 0 20px #4ade8011'; };
        document.body.appendChild(btn);
    }

    function init() {
        GM_addStyle(`
            @keyframes rbSlideIn { from { opacity:0; transform:translateX(30px); } to { opacity:1; transform:translateX(0); } }
        `);

        createTrigger();

        setTimeout(function() {
            const result = runCleanup();
            if (GM_notification) GM_notification({ text: '⚡ RAM optimized | ' + result.cleaned + ' elements cleaned', timeout: 2500 });
        }, 4000);

        if (CFG.autoClean) {
            setInterval(function() { runCleanup(); }, CFG.cleanInterval * 1000);
        }

        let scrollTimer;
        window.addEventListener('scroll', function() {
            clearTimeout(scrollTimer);
            scrollTimer = setTimeout(function() { freezeOffscreenMedia(); lazyOffscreenImages(); }, 2000);
        });

        document.addEventListener('visibilitychange', function() {
            if (document.hidden) runCleanup();
        });

        console.log('⚡ RAM Booster active | Click the bottom-right button');
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() { setTimeout(init, 800); });
    } else {
        setTimeout(init, 800);
    }
})();