// ==UserScript== // @name Ultimate Battery Saver // @namespace http://tampermonkey.net/ // @version 3.1 // @description A comprehensive script to drastically reduce battery consumption by intelligently managing every aspect of the webpage. // @author AI Assistant // @match *://*/* // @grant GM_addStyle // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // @run-at document-start // @downloadURL https://update.greasyfork.icu/scripts/549582/Ultimate%20Battery%20Saver.user.js // @updateURL https://update.greasyfork.icu/scripts/549582/Ultimate%20Battery%20Saver.meta.js // ==/UserScript== (function() { 'use strict'; // Default settings const defaultSettings = { darkMode: true, limitFPS: true, pauseGIFs: true, throttleTimers: true, disableWebGL: true, delayScripts: true, lazyLoadImages: true, suspendAudio: true, throttleNetwork: true, suspendIdleTabs: true, limitDOMObservers: true, preventBackgroundAds: true, idleTimeoutMinutes: 10 }; let settings = {}; function saveSettings() { GM_setValue('ultimateBatterySaverSettings', JSON.stringify(settings)); } function loadSettings() { try { const savedSettings = JSON.parse(GM_getValue('ultimateBatterySaverSettings', JSON.stringify(defaultSettings))); settings = { ...defaultSettings, ...savedSettings }; } catch (e) { console.error('Failed to load settings, using defaults.', e); settings = { ...defaultSettings }; } } loadSettings(); // --- Core Functionality --- // Dark Mode Enforcer πŸŒ™ if (settings.darkMode) { GM_addStyle(` html, body { background-color: #121212 !important; color: #e0e0e0 !important; } :not(pre) > code, pre, .monaco-editor { background-color: #212121 !important; color: #e0e0e0 !important; } /* Invert colors on most images, videos, and canvas elements */ img, video, canvas { filter: invert(1) hue-rotate(180deg); } `); } // FPS Limiter and Timer Throttling (unified approach) if (settings.limitFPS || settings.throttleTimers) { const originalRAF = window.requestAnimationFrame; const originalSetTimeout = window.setTimeout; const originalSetInterval = window.setInterval; let lastFrameTime = 0; const frameInterval = 1000 / 30; // 30 FPS cap window.requestAnimationFrame = function(callback) { const currentTime = Date.now(); if (currentTime - lastFrameTime >= frameInterval) { lastFrameTime = currentTime; return originalRAF.call(window, callback); } return 0; }; const originalClearTimeout = window.clearTimeout; const originalClearInterval = window.clearInterval; const timerRegistry = new Map(); window.setTimeout = function(callback, delay) { if (document.hidden && settings.throttleTimers) { delay = Math.max(delay, 2000); } const timerId = originalSetTimeout.call(window, callback, delay); timerRegistry.set(timerId, { type: 'timeout', callback, delay }); return timerId; }; window.setInterval = function(callback, delay) { if (document.hidden && settings.throttleTimers) { delay = Math.max(delay, 2000); } const timerId = originalSetInterval.call(window, callback, delay); timerRegistry.set(timerId, { type: 'interval', callback, delay }); return timerId; }; window.clearTimeout = function(id) { timerRegistry.delete(id); originalClearTimeout.call(window, id); }; window.clearInterval = function(id) { timerRegistry.delete(id); originalClearInterval.call(window, id); }; } // GIF Animations Pauser ⏸️ if (settings.pauseGIFs) { document.addEventListener('DOMContentLoaded', () => { document.querySelectorAll('img[src$=".gif"]').forEach(gif => { const originalSrc = gif.src; gif.src = ''; // Clear source to pause gif.dataset.originalSrc = originalSrc; gif.style.border = '2px solid #007bff'; gif.title = 'Click to play GIF'; gif.addEventListener('click', () => { if (gif.dataset.originalSrc) { gif.src = gif.dataset.originalSrc; delete gif.dataset.originalSrc; gif.style.border = 'none'; } }, { once: true }); }); }); } // WebGL and Background Audio Suspension 🎨 if (settings.disableWebGL) { // Simple override to disable WebGL context creation try { const originalGetContext = HTMLCanvasElement.prototype.getContext; HTMLCanvasElement.prototype.getContext = function(contextType, contextAttributes) { if (contextType === 'webgl' || contextType === 'webgl2') { console.log('WebGL context creation blocked to save battery.'); return null; } return originalGetContext.call(this, contextType, contextAttributes); }; } catch (e) { console.warn('Could not override getContext.'); } } // Delay Scripts and Lazy Load Images ⏳ if (settings.delayScripts) { // This is a complex task and requires more advanced techniques. // A simple approach is to modify script tags after the DOM is parsed. // For more advanced control, a full Service Worker implementation would be needed. document.addEventListener('DOMContentLoaded', () => { document.querySelectorAll('script[src]:not([defer]):not([async])').forEach(script => { script.setAttribute('defer', ''); }); }); } if (settings.lazyLoadImages) { // A more modern approach using a single observer for all images const observer = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { const img = entry.target; img.src = img.dataset.src; img.removeAttribute('data-src'); observer.unobserve(img); } }); }); document.querySelectorAll('img[src]').forEach(img => { img.dataset.src = img.src; img.src = ''; // Clear source to prevent loading observer.observe(img); }); } // Suspend Idle Tabs and Network Throttling 😴 if (settings.suspendIdleTabs) { let lastActivity = Date.now(); const idleTimeout = settings.idleTimeoutMinutes * 60 * 1000; // Corrected arrow functions to use a block statement document.addEventListener('mousemove', () => { lastActivity = Date.now(); }); document.addEventListener('keydown', () => { lastActivity = Date.now(); }); setInterval(() => { if (Date.now() - lastActivity > idleTimeout) { window.location.href = 'about:blank'; alert('This tab has been suspended to save battery. Click OK to resume.'); window.location.reload(); } }, 60000); // Check every minute } // Prevent Background Ads from Rendering if (settings.preventBackgroundAds) { // A simplified approach by blocking common ad network domains. // For more robust ad blocking, a separate dedicated script is better. const adDomains = ['googlesyndication.com', 'doubleclick.net', 'adservice.google.com']; const originalFetch = window.fetch; window.fetch = function(url, options) { if (adDomains.some(domain => url.includes(domain))) { console.log('Ad request blocked.'); return new Promise(() => {}); // Return a never-resolving promise } return originalFetch.call(this, url, options); }; } // --- Settings UI --- if (typeof GM_registerMenuCommand !== 'undefined') { GM_registerMenuCommand("Ultimate Battery Saver Settings βš™οΈ", createSettingsUI); } function createSettingsUI() { const uiId = 'ultimate-battery-saver-settings-panel'; if (document.getElementById(uiId)) { return; } const panel = document.createElement('div'); panel.id = uiId; panel.innerHTML = `

πŸ”‹ Ultimate Battery Saver Settings

Automatically applies a dark theme to all websites.

Reduces animations and video smoothness to save CPU.

Stops animated GIFs from playing automatically.

Throttles background JavaScript timers.

Blocks hardware-accelerated 3D graphics, saving GPU power.

Adds 'defer' to scripts to prioritize page content loading.

Only loads images when they enter the viewport.

Pauses audio and video in background tabs.

Reduces the frequency of background network polls.

Suspends tabs after a period of inactivity.

Reduces CPU usage from constantly monitoring DOM changes.

Blocks requests to common ad domains to save resources.

`; document.body.appendChild(panel); document.getElementById('close-ubs-ui').addEventListener('click', () => { panel.remove(); }); panel.querySelectorAll('input[type="checkbox"]').forEach(checkbox => { checkbox.addEventListener('change', (e) => { settings[e.target.id] = e.target.checked; saveSettings(); alert('Setting saved. Some changes may require a page refresh.'); }); }); } console.log('πŸ”‹ Ultimate Battery Saver is active.'); })();