// ==UserScript==
// @name Facebook Reel Speed Controller + Overlay
// @namespace https://facebook.com
// @version 2.0
// @description Bảng điều khiển tốc độ Video/Reels trên Facebook, ẩn overlay
// @author NewbieMt
// @match https://www.facebook.com/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// ==/UserScript==
(function () {
"use strict";
const styles = `
.fb-custom-speed-panel {
position: absolute; top: 70px; left: 10px; z-index: 2147483647;
background: rgba(0,0,0,0.4); color: white; padding: 6px;
border-radius: 6px; font-size: 14px; display: flex; flex-direction: column; gap: 5px;
backdrop-filter: blur(4px);
transition: all 0.3s ease;
cursor: grab;
}
.fb-custom-speed-panel:active { cursor: grabbing; }
.fb-custom-speed-panel:hover { background: rgba(0,0,0,0.8); }
.fb-custom-speed-panel select { background: #333; color: white; border: 1px solid #555; border-radius: 3px; }
.fb-custom-btn {
background: #444; color: white; border: none; border-radius: 4px;
cursor: pointer; padding: 2px 8px; transition: 0.2s;
}
.fb-custom-btn:hover { background: #666; }
.fb-custom-toggle-btn {
position: absolute; top: 10px; right: 10px; z-index: 2147483647;
background: rgba(0,0,0,0.6); color: white; border: none; padding: 5px 10px;
border-radius: 5px; cursor: pointer;
}
.fb-custom-close-mini {
position: absolute; top: 5px; left: 5px; z-index: 2147483647;
background: rgba(0,0,0,0.7); color: white; border: none; padding: 5px 8px;
border-radius: 50%; cursor: pointer; font-size: 14px; font-weight: bold;
}
/* Ẩn mọi thứ không chứa thẻ video (và không phải UI của chúng ta) */
.fb-hide-overlay *:not(:has(video)):not(video):not(.fb-custom-speed-panel):not(.fb-custom-speed-panel *):not(.fb-custom-toggle-btn):not(.fb-custom-toggle-btn *):not(.fb-custom-close-mini) {
opacity: 0 !important;
pointer-events: none !important;
}
`;
if (typeof GM_addStyle !== "undefined") {
GM_addStyle(styles);
} else {
const styleEl = document.createElement("style");
styleEl.textContent = styles;
document.head.appendChild(styleEl);
}
function findRootPlayerDiv(video) {
if (!video) return null;
let el = video;
while (el && el !== document.body) {
if (el.matches('div[data-testid="video_player_root"]')) return el;
el = el.parentElement;
}
return video.closest('div[role="presentation"]') || video.parentElement;
}
function updateVideoSpeed(video, newSpeed, ui) {
newSpeed = Math.max(0.1, parseFloat(newSpeed.toFixed(2)));
video.playbackRate = newSpeed;
GM_setValue("fbReelSpeed", newSpeed);
if (ui) {
ui.input.value = newSpeed;
ui.select.value = ui.select.querySelector(`option[value="${newSpeed}"]`) ? newSpeed : "";
}
}
function createSpeedControl(video) {
if (video.dataset.hasSpeedControl) return;
const root = findRootPlayerDiv(video);
if (!root) return;
video.dataset.hasSpeedControl = "true";
root.style.position = "relative";
const savedSpeed = GM_getValue("fbReelSpeed", 1.0);
const container = document.createElement("div");
container.className = "fb-custom-speed-panel";
container.innerHTML = `
`;
const sel = container.querySelector('.fbs-sel');
sel.innerHTML = [0.25, 0.75, 1, 1.25, 1.5, 2, 2.5, 2.75, 3, 5]
.map(s => ``).join('');
const ui = { input: container.querySelector('.fbs-inp'), select: sel };
sel.onchange = () => updateVideoSpeed(video, parseFloat(sel.value), ui);
ui.input.onchange = () => updateVideoSpeed(video, parseFloat(ui.input.value), ui);
// ponytail: dropped hold-to-change, click instead
container.querySelector('.fbs-dec').onclick = () => updateVideoSpeed(video, parseFloat(ui.input.value) - 0.1, ui);
container.querySelector('.fbs-inc').onclick = () => updateVideoSpeed(video, parseFloat(ui.input.value) + 0.1, ui);
container.querySelector('.fbs-cfg').onclick = () => {
const r2 = container.querySelector('.fbs-r2');
r2.style.display = r2.style.display === "none" ? "flex" : "none";
};
let isDragging = false, startX, startY, startLeft, startTop;
container.onmousedown = (e) => {
if (['INPUT', 'BUTTON', 'SELECT', 'OPTION'].includes(e.target.tagName)) return;
isDragging = true; startX = e.clientX; startY = e.clientY;
startLeft = container.offsetLeft; startTop = container.offsetTop;
e.preventDefault();
};
window.addEventListener('mousemove', (e) => {
if (!isDragging) return;
container.style.left = Math.max(0, Math.min(startLeft + e.clientX - startX, root.offsetWidth - container.offsetWidth)) + 'px';
container.style.top = Math.max(0, Math.min(startTop + e.clientY - startY, root.offsetHeight - container.offsetHeight)) + 'px';
container.style.bottom = 'auto';
});
window.addEventListener('mouseup', () => {
if (isDragging) {
isDragging = false;
GM_setValue("fbReelSpeedPos", { top: container.style.top, left: container.style.left });
}
});
root.appendChild(container);
const savedPos = GM_getValue("fbReelSpeedPos", null);
if (savedPos) {
container.style.left = Math.max(0, Math.min(parseFloat(savedPos.left), root.offsetWidth - container.offsetWidth)) + 'px';
container.style.top = Math.max(0, Math.min(parseFloat(savedPos.top), root.offsetHeight - container.offsetHeight)) + 'px';
container.style.bottom = 'auto';
}
video.playbackRate = savedSpeed;
}
function createOverlayToggle(video) {
if (video.dataset.hasOverlayToggle) return;
const root = findRootPlayerDiv(video);
if (!root) return;
video.dataset.hasOverlayToggle = "true";
root.insertAdjacentHTML('beforeend', '');
root.lastElementChild.onclick = () => root.classList.toggle("fb-hide-overlay");
}
function addCloseButtonToMiniPlayer() {
document.querySelectorAll('div[style*="position: fixed"][style*="bottom"]').forEach(container => {
if (container.dataset.hasCloseBtn || !container.querySelector("video")) return;
container.dataset.hasCloseBtn = "true";
container.insertAdjacentHTML('beforeend', '');
container.lastElementChild.onclick = () => container.remove();
});
}
function init() {
document.querySelectorAll("video").forEach((v) => {
if (!v.offsetParent) return;
createSpeedControl(v);
createOverlayToggle(v);
});
addCloseButtonToMiniPlayer();
}
init();
let timeoutId = null;
new MutationObserver(() => {
if (timeoutId) return;
timeoutId = setTimeout(() => {
init();
timeoutId = null;
}, 800);
}).observe(document.body, { childList: true, subtree: true });
})();