onYouTubePlayerAPIReady doesn't get called after inject iframe via a chrome extension
up vote
0
down vote
favorite
I was playing around with the Youtube API and wanted to create a chrome extension that injects an iframe into the current website.
At first I'd try it out plainly and it works as intended:
(function()
// https://developers.google.com/youtube/iframe_api_reference
console.log('loaded');
const anchorPlay = document.createElement('a');
anchorPlay.id = 'play-button';
anchorPlay.innerHTML = 'Play';
const anchorPause = document.createElement('a');
anchorPause.id = 'pause-button';
anchorPause.innerHTML = 'Pause';
const iFrameElement = document.createElement('iframe');
iFrameElement.setAttribute('id', 'video');
iFrameElement.setAttribute(
'src',
'https://www.youtube.com/embed/krVkvyQjyBQ?enablejsapi=1&html5=1'
);
iFrameElement.setAttribute('allowfullscreen', '');
iFrameElement.setAttribute('frameborder', '0');
iFrameElement.setAttribute('height', '315');
iFrameElement.setAttribute('width', '560');
document.body.appendChild(anchorPlay);
document.body.appendChild(anchorPause);
document.body.appendChild(iFrameElement);
// global variable for the player
var player;
// this function gets called when API is ready to use
window.onYouTubePlayerAPIReady = function()
alert('called onYouTubePlayerAPIReady');
// create the global player from the specific iframe (#video)
player = new YT.Player('video',
events:
// call this function when player is ready to use
onReady: onPlayerReady
);
;
function onPlayerReady(event)
// bind events
var playButton = document.getElementById('play-button');
playButton.addEventListener('click', function()
player.playVideo();
);
var pauseButton = document.getElementById('pause-button');
pauseButton.addEventListener('click', function()
// player.pauseVideo();
const currTime = player.getCurrentTime();
alert(currTime);
);
// Inject YouTube API script
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/player_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
)();
https://codepen.io/thadeuszlay/pen/gQwwoZ
But once I create it as a chrome extension, the function window.onYouTubePlayerAPIReady
doesn't get called. While all the elements get loaded correctly (buttons and iFrame are there and I can see my console logs).
The console is clean from errors. I can also see that the YT-API script got inserted correctly in the <head>
. I deactivated my ad blocker, too.
In my Chrome extension, the content script looks exactly as the codepen code above. My manifest.json
looks like this:
"name": "test",
"version": "1.0.0",
"offline_enabled": true,
"description": "test.",
"permissions": ["activeTab", "tabs"],
"browser_action":
"default_popup": "popup.html",
"default_icon":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"icons":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"content_scripts": [
"css": ["styles.css"],
"js": ["content.js"],
"run_at": "document_idle",
"all_frames": true,
"matches": ["https://*/*", "http://*/*"]
],
"manifest_version": 2
javascript google-chrome-extension youtube-api
add a comment |
up vote
0
down vote
favorite
I was playing around with the Youtube API and wanted to create a chrome extension that injects an iframe into the current website.
At first I'd try it out plainly and it works as intended:
(function()
// https://developers.google.com/youtube/iframe_api_reference
console.log('loaded');
const anchorPlay = document.createElement('a');
anchorPlay.id = 'play-button';
anchorPlay.innerHTML = 'Play';
const anchorPause = document.createElement('a');
anchorPause.id = 'pause-button';
anchorPause.innerHTML = 'Pause';
const iFrameElement = document.createElement('iframe');
iFrameElement.setAttribute('id', 'video');
iFrameElement.setAttribute(
'src',
'https://www.youtube.com/embed/krVkvyQjyBQ?enablejsapi=1&html5=1'
);
iFrameElement.setAttribute('allowfullscreen', '');
iFrameElement.setAttribute('frameborder', '0');
iFrameElement.setAttribute('height', '315');
iFrameElement.setAttribute('width', '560');
document.body.appendChild(anchorPlay);
document.body.appendChild(anchorPause);
document.body.appendChild(iFrameElement);
// global variable for the player
var player;
// this function gets called when API is ready to use
window.onYouTubePlayerAPIReady = function()
alert('called onYouTubePlayerAPIReady');
// create the global player from the specific iframe (#video)
player = new YT.Player('video',
events:
// call this function when player is ready to use
onReady: onPlayerReady
);
;
function onPlayerReady(event)
// bind events
var playButton = document.getElementById('play-button');
playButton.addEventListener('click', function()
player.playVideo();
);
var pauseButton = document.getElementById('pause-button');
pauseButton.addEventListener('click', function()
// player.pauseVideo();
const currTime = player.getCurrentTime();
alert(currTime);
);
// Inject YouTube API script
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/player_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
)();
https://codepen.io/thadeuszlay/pen/gQwwoZ
But once I create it as a chrome extension, the function window.onYouTubePlayerAPIReady
doesn't get called. While all the elements get loaded correctly (buttons and iFrame are there and I can see my console logs).
The console is clean from errors. I can also see that the YT-API script got inserted correctly in the <head>
. I deactivated my ad blocker, too.
In my Chrome extension, the content script looks exactly as the codepen code above. My manifest.json
looks like this:
"name": "test",
"version": "1.0.0",
"offline_enabled": true,
"description": "test.",
"permissions": ["activeTab", "tabs"],
"browser_action":
"default_popup": "popup.html",
"default_icon":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"icons":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"content_scripts": [
"css": ["styles.css"],
"js": ["content.js"],
"run_at": "document_idle",
"all_frames": true,
"matches": ["https://*/*", "http://*/*"]
],
"manifest_version": 2
javascript google-chrome-extension youtube-api
2
The API runs as a normal web page script, not in the isolated world of content scripts, so you need to put the code that accesses page variables such as window.onYouTubePlayerAPIReady into web page context, see Insert code into the page context using a content script
– wOxxOm
Nov 11 at 6:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was playing around with the Youtube API and wanted to create a chrome extension that injects an iframe into the current website.
At first I'd try it out plainly and it works as intended:
(function()
// https://developers.google.com/youtube/iframe_api_reference
console.log('loaded');
const anchorPlay = document.createElement('a');
anchorPlay.id = 'play-button';
anchorPlay.innerHTML = 'Play';
const anchorPause = document.createElement('a');
anchorPause.id = 'pause-button';
anchorPause.innerHTML = 'Pause';
const iFrameElement = document.createElement('iframe');
iFrameElement.setAttribute('id', 'video');
iFrameElement.setAttribute(
'src',
'https://www.youtube.com/embed/krVkvyQjyBQ?enablejsapi=1&html5=1'
);
iFrameElement.setAttribute('allowfullscreen', '');
iFrameElement.setAttribute('frameborder', '0');
iFrameElement.setAttribute('height', '315');
iFrameElement.setAttribute('width', '560');
document.body.appendChild(anchorPlay);
document.body.appendChild(anchorPause);
document.body.appendChild(iFrameElement);
// global variable for the player
var player;
// this function gets called when API is ready to use
window.onYouTubePlayerAPIReady = function()
alert('called onYouTubePlayerAPIReady');
// create the global player from the specific iframe (#video)
player = new YT.Player('video',
events:
// call this function when player is ready to use
onReady: onPlayerReady
);
;
function onPlayerReady(event)
// bind events
var playButton = document.getElementById('play-button');
playButton.addEventListener('click', function()
player.playVideo();
);
var pauseButton = document.getElementById('pause-button');
pauseButton.addEventListener('click', function()
// player.pauseVideo();
const currTime = player.getCurrentTime();
alert(currTime);
);
// Inject YouTube API script
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/player_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
)();
https://codepen.io/thadeuszlay/pen/gQwwoZ
But once I create it as a chrome extension, the function window.onYouTubePlayerAPIReady
doesn't get called. While all the elements get loaded correctly (buttons and iFrame are there and I can see my console logs).
The console is clean from errors. I can also see that the YT-API script got inserted correctly in the <head>
. I deactivated my ad blocker, too.
In my Chrome extension, the content script looks exactly as the codepen code above. My manifest.json
looks like this:
"name": "test",
"version": "1.0.0",
"offline_enabled": true,
"description": "test.",
"permissions": ["activeTab", "tabs"],
"browser_action":
"default_popup": "popup.html",
"default_icon":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"icons":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"content_scripts": [
"css": ["styles.css"],
"js": ["content.js"],
"run_at": "document_idle",
"all_frames": true,
"matches": ["https://*/*", "http://*/*"]
],
"manifest_version": 2
javascript google-chrome-extension youtube-api
I was playing around with the Youtube API and wanted to create a chrome extension that injects an iframe into the current website.
At first I'd try it out plainly and it works as intended:
(function()
// https://developers.google.com/youtube/iframe_api_reference
console.log('loaded');
const anchorPlay = document.createElement('a');
anchorPlay.id = 'play-button';
anchorPlay.innerHTML = 'Play';
const anchorPause = document.createElement('a');
anchorPause.id = 'pause-button';
anchorPause.innerHTML = 'Pause';
const iFrameElement = document.createElement('iframe');
iFrameElement.setAttribute('id', 'video');
iFrameElement.setAttribute(
'src',
'https://www.youtube.com/embed/krVkvyQjyBQ?enablejsapi=1&html5=1'
);
iFrameElement.setAttribute('allowfullscreen', '');
iFrameElement.setAttribute('frameborder', '0');
iFrameElement.setAttribute('height', '315');
iFrameElement.setAttribute('width', '560');
document.body.appendChild(anchorPlay);
document.body.appendChild(anchorPause);
document.body.appendChild(iFrameElement);
// global variable for the player
var player;
// this function gets called when API is ready to use
window.onYouTubePlayerAPIReady = function()
alert('called onYouTubePlayerAPIReady');
// create the global player from the specific iframe (#video)
player = new YT.Player('video',
events:
// call this function when player is ready to use
onReady: onPlayerReady
);
;
function onPlayerReady(event)
// bind events
var playButton = document.getElementById('play-button');
playButton.addEventListener('click', function()
player.playVideo();
);
var pauseButton = document.getElementById('pause-button');
pauseButton.addEventListener('click', function()
// player.pauseVideo();
const currTime = player.getCurrentTime();
alert(currTime);
);
// Inject YouTube API script
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/player_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
)();
https://codepen.io/thadeuszlay/pen/gQwwoZ
But once I create it as a chrome extension, the function window.onYouTubePlayerAPIReady
doesn't get called. While all the elements get loaded correctly (buttons and iFrame are there and I can see my console logs).
The console is clean from errors. I can also see that the YT-API script got inserted correctly in the <head>
. I deactivated my ad blocker, too.
In my Chrome extension, the content script looks exactly as the codepen code above. My manifest.json
looks like this:
"name": "test",
"version": "1.0.0",
"offline_enabled": true,
"description": "test.",
"permissions": ["activeTab", "tabs"],
"browser_action":
"default_popup": "popup.html",
"default_icon":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"icons":
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
,
"content_scripts": [
"css": ["styles.css"],
"js": ["content.js"],
"run_at": "document_idle",
"all_frames": true,
"matches": ["https://*/*", "http://*/*"]
],
"manifest_version": 2
javascript google-chrome-extension youtube-api
javascript google-chrome-extension youtube-api
edited Nov 11 at 8:46
asked Nov 11 at 1:35
thadeuszlay
66411236
66411236
2
The API runs as a normal web page script, not in the isolated world of content scripts, so you need to put the code that accesses page variables such as window.onYouTubePlayerAPIReady into web page context, see Insert code into the page context using a content script
– wOxxOm
Nov 11 at 6:59
add a comment |
2
The API runs as a normal web page script, not in the isolated world of content scripts, so you need to put the code that accesses page variables such as window.onYouTubePlayerAPIReady into web page context, see Insert code into the page context using a content script
– wOxxOm
Nov 11 at 6:59
2
2
The API runs as a normal web page script, not in the isolated world of content scripts, so you need to put the code that accesses page variables such as window.onYouTubePlayerAPIReady into web page context, see Insert code into the page context using a content script
– wOxxOm
Nov 11 at 6:59
The API runs as a normal web page script, not in the isolated world of content scripts, so you need to put the code that accesses page variables such as window.onYouTubePlayerAPIReady into web page context, see Insert code into the page context using a content script
– wOxxOm
Nov 11 at 6:59
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245100%2fonyoutubeplayerapiready-doesnt-get-called-after-inject-iframe-via-a-chrome-exte%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The API runs as a normal web page script, not in the isolated world of content scripts, so you need to put the code that accesses page variables such as window.onYouTubePlayerAPIReady into web page context, see Insert code into the page context using a content script
– wOxxOm
Nov 11 at 6:59