Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Replace the TED player with the equivalent YouTube video.
{
"name": "TED YouTube embed",
"version": "1.0",
"description": "Replace the TED player with the equivalent YouTube video.",
"content_scripts": [
{
"matches": ["*://www.ted.com/talks/*"],
"js": ["replace.js"],
"run_at": "document_end"
}
],
"manifest_version": 2
}
// https://developers.google.com/youtube/v3/docs/search/list
var video = document.getElementById("player-hero");
var key = "AIzaSyD4v-XiK6_XDCDiMccTO6_Y1hNn03ft9Gc";
if (video) {
function callback() {
console.log(this.responseURL);
console.log(this);
var data = JSON.parse(this.responseText);
console.log(data);
if (data.error) {
console.log(data.error.message);
return;
}
if (!data.items || !data.items[0] || !data.items[0].id.videoId) {
console.log("Can't find video.");
return;
}
var item = data.items[0];
var video_id = item.id.videoId;
console.log(`https://www.youtube.com/watch?v=${video_id}`);
video.innerHTML = `<iframe width="853" height="480" src="https://www.youtube.com/embed/${video_id}?rel=0" frameborder="0" allowfullscreen style="display:block; margin:0 auto;"></iframe>`;
}
// Glenn Greenwald: Why privacy matters | TED Talk | TED.com
var q = document.title.split("|")[0].trim();
console.log(q);
var url = `https://www.googleapis.com/youtube/v3/search?key=${key}&part=id&q=${q}`;
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", callback);
xhr.open("GET", url);
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment