크롬 확장 콘텐츠 스크립트를 사용하여 iframe API가있는 YouTube 동영상을 기존 페이지에 삽입하려고합니다. 하지만 onYouTubeIframeAPIReady
을 트리거 할 수 없습니다.youtube video in chrome extension 콘텐츠 스크립트
manifest.json을
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*", "file://*/*", "*://*/*"],
"js": ["content-script.js"]
}
],
내용 script.js 나는 그것이 webview
작업을 할 수 있었다 크롬 응용 프로그램에서
const appEl = document.createElement('div');
appEl.id = 'my-app';
appEl.innerHTML = `<div id="youtube-iframe"></div>`;
const bodyEl = document.querySelector('body');
bodyEl.insertBefore(appEl, bodyEl.firstChild);
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
document.querySelector('body').appendChild(tag);
window.onYouTubeIframeAPIReady =() => {
this.player = new YT.Player('youtube-iframe', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
}
});
}
function onPlayerReady(event) {
console.log('player ready');
event.target.playVideo();
};
하지만이 보이지 않는다 확장 프로그램에서 사용할 수 있습니다.
내용 스크립트가 격리 된 환경에서 실행되는 다음 코드로 다른 모듈에서 유튜브 API를 사용할 수 있었다의 파일에 저장. 페이지 변수에 액세스하려면 [Chrome 확장 기능 만들기 - 콘텐츠 스크립트를 사용하여 페이지에 코드 삽입] (// stackoverflow.com/a/9517879)을 참조하십시오. – wOxxOm