Skip to content

Instantly share code, notes, and snippets.

@phunanon
Last active November 15, 2023 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phunanon/2c36872200aa798c12bf2282b1327c72 to your computer and use it in GitHub Desktop.
Save phunanon/2c36872200aa798c12bf2282b1327c72 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name YouTube budget
// @version 0.1.2
// @description Budgets YT time
// @author Patrick Bowen
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
const minutes = 20;
//Storing the last time since all videos were paused
const save = n => localStorage.setItem('YT', n);
const load = () => new Date(Number(localStorage.getItem('YT')));
let previousCreditMs = null;
(function() {
if (GM_info.isIncognito) {
return;
}
setInterval(() => {
const vid = document.querySelector('#movie_player video');
if (!vid) {
return;
}
const debitMs = Number(!vid.paused) * 2_000;
const time = load();
const creditMs = Math.max(Math.min(Date.now() - time, 1_000 * 60 * minutes), 0);
if (!previousCreditMs) previousCreditMs = creditMs;
document.title = `${Math.floor(creditMs / 1_000)}s ${document.title.replace(/^[0-9s]+? /, '')}`;
//Pause video if we have fewer than a second of credit remaining, or if two videos are playing at once
if (creditMs <= 1000 || (previousCreditMs - creditMs) > 1_500) {
vid.pause();
}
previousCreditMs = creditMs;
save(Date.now() - creditMs + debitMs);
}, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment