Skip to content

Instantly share code, notes, and snippets.

@your-diary
Last active January 26, 2021 09:08
Show Gist options
  • Save your-diary/166999f899aa01a26315a13a22e9e5b8 to your computer and use it in GitHub Desktop.
Save your-diary/166999f899aa01a26315a13a22e9e5b8 to your computer and use it in GitHub Desktop.
Shows the latency for a YouTube live stream.
// ==UserScript==
// @name [YouTube] Shows latency.
// @description Shows latency.
// @match https://www.youtube.com/watch?v=*
// @namespace ynn
// ==/UserScript==
(function() {
"use strict";
function f() {
const e_movie_player = document.querySelector('#movie_player');
const e_date = document.querySelector('#date');
if (e_movie_player === null || e_date === null) {
window.setTimeout(f, 500);
} else {
if (document.querySelector('.view-count').innerText.endsWith('views')) {
console.log('showLatency(): This is not a live stream. Exited.');
return;
} else {
console.log('showLatency(): Started.');
}
const c = document.createElement('style');
c.innerText = '.html5-video-info-panel { display: none; }';
document.head.appendChild(c);
e_movie_player.dispatchEvent(new Event('contextmenu'));
document.querySelector('div.ytp-menuitem:nth-child(7)').click();
const latency = document.querySelector('.html5-video-info-panel-content > div:nth-child(12) > span:nth-child(2) > span:nth-child(2)');
const latency_display = document.createElement('yt-formatted-string');
latency_display.classList.add('style-scope', 'yt-view-count-renderer');
e_date.insertAdjacentElement('beforeend', latency_display);
window.setInterval(() => {
latency_display.innerText = `・Latency ${latency.innerText.replace(/.s/, 's')}`;
// latency_display.innerText = `・Latency ${latency.innerText}`;
}, 1000);
}
}
f();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment