Skip to content

Instantly share code, notes, and snippets.

@tmiland
Last active December 27, 2017 07:40
Show Gist options
  • Save tmiland/e2a86addea3ef5900b4f5445e17e80ce to your computer and use it in GitHub Desktop.
Save tmiland/e2a86addea3ef5900b4f5445e17e80ce to your computer and use it in GitHub Desktop.
Play YouTube on Facebook
// ==UserScript==
// @name Play YouTube on Facebook
// @description Play YouTube Videos on Facebook without leaving the site.
// @namespace https://gist.github.com/tmiland/e2a86addea3ef5900b4f5445e17e80ce
// @version 1.0
// @date 27-12-2017
// @author tmiland
// @match https://www.facebook.com/*
// @require http://code.jquery.com/jquery-3.2.1.min.js
// @grant none
/* Thanks to https://chrome.google.com/webstore/detail/my-today-song-super-duper/mbnofkhnoflaknikohfaedmdaiafohpg for the code. :)
I am not the author of this code, i have just modified it sligtly to my likings, so i can use it as a userscript. */
// ==/UserScript==
(function() {
'use strict';
$(document).ready(function () {
$(document).on("click", 'a', function (event) {
var href = $(this).attr('href');
href = getId(href);
if (href == 'error') {
return;
}
event.preventDefault();
// pause all currently playing YouTube frames
$('.youtube_frame').each(function(){
this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
}
);
// Old code for reference
//$(this).replaceWith('<iframe class="youtube_frame" src="//www.youtube.com/embed/' + href + '?autoplay=1&enablejsapi=1" frameborder="0" allowfullscreen></iframe>');
//Added privacy option
$(this).replaceWith('<iframe class="youtube_frame" src="https://www.youtube-nocookie.com/embed/' + href + '?autoplay=1&enablejsapi=1" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>');
});
function getId(url) {
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
return match[2];
} else {
return 'error';
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment