Skip to content

Instantly share code, notes, and snippets.

@stefansundin
Created March 3, 2017 22:01
Show Gist options
  • Save stefansundin/afd4eed7c5da35567a655c728aeb2fbb to your computer and use it in GitHub Desktop.
Save stefansundin/afd4eed7c5da35567a655c728aeb2fbb to your computer and use it in GitHub Desktop.
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