I'm looking to use the Youtube player API in my Youtube Chrome extension.
My goal is to be able to control the player's controls on Youtube website from my content script using the pageAction's popup.
It goes like this -
Clicking on a "play" button in the popup sends a message to the content script, the content script receives the message and use the player API event "playVideo()" to play the video.
Because of the content script and the page itself are isolated from each other then I can't simply get the player's object from the page, and from what I understand I can't use messages to send an object over from the page to the content script.
Now I could bypass all this trouble by using the HTML5 video events, but unfortunately this won't work on live streams since flash is still in use there, so I kind of have no other choice but loading and using the player API, am I correct?
Is there a way to control the player using URL parameters on Youtube site itself?
(I really prefer everything than loading an additional script and external one)
Where it comes to code I would like to get help setting the permissions needed to be able to load the external script please.
I saw that there is a need to declare a content_security_policy
in the manifest
file, but I do not understand the syntax and how to write it down, so I would like to have an example and whatever else is needed for it please.
In my content script I'm using the next code taken from the YT player API documentation to inject the script:
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady() {
console.log('--- The YT player API is ready from content script! ---');
}
document.getElementById('movie_player').playVideo()
and you will see that it works great :) now the question is how to load it to be in use in the content script please. – Gil Goldshlagerchrome.tabs.sendMessage
, and then sending another message this time from the content script to the injected script usingwindow.postMessage
, all info can be found here: Communication with the embedding page – Gil Goldshlager