Skip to content

Instantly share code, notes, and snippets.

@ShizukuIchi
Last active January 28, 2019 03:37
Show Gist options
  • Save ShizukuIchi/14a6148a51707b3aaf2922a59d41c841 to your computer and use it in GitHub Desktop.
Save ShizukuIchi/14a6148a51707b3aaf2922a59d41c841 to your computer and use it in GitHub Desktop.
Float YouTube video by viewport intersection.
// Base on https://gist.github.com/realdennis/b254ce2ac0efd54e602a6b1bb64c643a#file-float-youtube-video-using-monkey-js
// Intersection API version
// Last edit: 01/28/2019
(() => {
const float_video = () => {
if (window._floatIsOpen === true) return;
window._floatIsOpen = true;
const style = document.createElement('style');
style.innerHTML = `
.float-video{
left: unset !important;
right: 20px !important;
top: unset !important;
bottom: 20px !important;
width: 398px !important;
height: 224px !important;
position: fixed !important;
}
`;
document.body.append(style);
document.querySelector('div#movie_player').style.zIndex = 2;
const ratio = 0.2;
const target = document.querySelector('ytd-player');
const video = document.querySelector('video');
const options = {
threshold: ratio,
};
const callback = entries => {
entries.forEach(entry => {
if (entry.intersectionRatio >= ratio)
video.classList.remove('float-video');
else video.classList.add('float-video');
});
};
const observer = new IntersectionObserver(callback, options);
observer.observe(target);
};
if (location.href.includes('watch')) float_video();
else {
document.addEventListener('yt-navigate-finish', e => {
if (e.target.baseURI.includes('watch')) float_video();
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment