The changes introduced in #1368818: HTTPS (SSL) support for YouTube player. specified to use always HTTPS as the protocol in Media Youtube iframes.
The problem is that using YouTube's iframe JS API (https://developers.google.com/youtube/iframe_api_reference) with existing iframes does not work if you specify an HTTPS url while using HTTP in your website.
This is the code I'm using to test this:
function my_module_preprocess_media_youtube_video(&$vars) {
if ($vars['options']['enablejsapi']) {
drupal_add_js('//www.youtube.com/iframe_api', 'external');
drupal_add_js(drupal_get_path('module', 'my_module') . '/my_module_media_youtube.js');
}
}
And in the JS:
var players = {};
function onYouTubeIframeAPIReady() {
(function ($) {
$('.media-youtube-player').each(function() {
var player = new YT.Player(this.id, {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
players[this.id] = player;
});
})(jQuery);
}
function onPlayerReady(event) {
console.log('onPlayerReady');
event.target.playVideo();
}
function onPlayerStateChange(event) {
console.log('onPlayerStateChange');
}
This works if I change HTTPS to HTTP in media_youtube iframes.
Comments
Comment #1
jherencia commentedFor the record, this is the patch that fixes my problems.
Comment #2
RobW commentedOK, we can fix this.
See #1368818: HTTPS (SSL) support for YouTube player. for the reasoning against using protocol relative urls. TL;DR, they break if viewed outside of a web browser context, e.g., RSS and email applications. Caching prevents us from using the site's http/s variable.
In Media: Vimeo, we addressed this with a formatter choice: http://drupal.org/node/1715438#comment-6315932. This isn't ideal because it takes up a lot of space in the formatter options and isn't global for the site. But M:YT and M:V don't have global config screens right now and I'd prefer to keep it that way.
I think the best compromise is adding a "specify protocol" checkbox to the video formatter that expands to show radios for http, https, and //. I'm not sure if content in email applications or using the js API is more common: the default should serve whichever is. Feedback needed before I code this up.
Comment #4
RobW commentedAfter giving this some thought, I think we should:
For general use, it seems like protocol relative urls solve a wide range of issues. If a user is creating an RSS feed they should use the RSS view mode, and specify a non-relative protocol there. Same with content emails. There won't be any js API manipulation when content is viewed in an RSS feed, so no worry about breaking API integration.
Comment #5
jherencia commentedOk, let me do this.
Comment #6
jherencia commentedComment #7
jherencia commentedThe patch commited contains the aproach described in #4, I've added a checkbox so normal users do not have to see the select.
It contains the removal of some trailing spaces.
Comment #8
RobW commentedNice work. I made a few changes:
I think it's looking pretty good.
Comment #9
jherencia commentedComment #10
jherencia commentedI've found that
is still in.
Comment #11
jherencia commentedComment #12
RobW commentedRight. API says: "If not defined, the current scheme is used, so the user stays on http or https respectively", so removed. Easier to take it out than to add logic.
Comment #13
jherencia commentedAlright, I've tested and every thing works right.
Comment #14
RobW commentedThanks for the great collaboration. Committed: http://drupalcode.org/project/media_youtube.git/commit/02be68f.