Last active
January 15, 2017 07:33
-
-
Save stefansundin/c74efc7c776d18986e75ecabaab12704 to your computer and use it in GitHub Desktop.
Replace YouPlay videos with YouTube on sweclockers.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "SweClockers YouTube embed", | |
"version": "1.1", | |
"description": "Trött på dålig videospelare?", | |
"content_scripts": [ | |
{ | |
"matches": ["*://www.sweclockers.com/*"], | |
"js": ["replace.js"], | |
"run_at": "document_end" | |
} | |
], | |
"manifest_version": 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developers.google.com/youtube/v3/docs/search/list | |
var videos = document.getElementsByClassName("bbVideoYouplay"); | |
var channel_id = "UCW64r1wPzfj0W1qbzzfCgFg"; | |
var key = "AIzaSyD4v-XiK6_XDCDiMccTO6_Y1hNn03ft9Gc"; | |
if (videos.length > 0) { | |
var div = videos[0].parentNode.parentNode; | |
function youtube_callback() { | |
console.log(this.responseURL); | |
console.log(this); | |
var data = this.response; | |
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.filter((i) => i.snippet.channelId == channel_id)[0]; | |
var item = data.items[0]; | |
var video_id = item.id.videoId; | |
console.log(`https://www.youtube.com/watch?v=${video_id}`); | |
div.style.height = '382px'; | |
div.innerHTML = `<iframe width="100%" height="382" src="https://www.youtube.com/embed/${video_id}?rel=0" frameborder="0" allowfullscreen></iframe>`; | |
} | |
function youplay_callback() { | |
console.log(this.responseURL); | |
console.log(this); | |
var data = this.response; | |
console.log(data); | |
var q = data.name; | |
console.log(q); | |
var url = `https://www.googleapis.com/youtube/v3/search?key=${key}&part=id&channelId=${channel_id}&q=${q}`; | |
var xhr = new XMLHttpRequest(); | |
xhr.responseType = "json"; | |
xhr.addEventListener("load", youtube_callback); | |
xhr.open("GET", url); | |
xhr.send(); | |
} | |
var script = div.querySelector("script[data-config]"); | |
if (script) { | |
var data = JSON.parse(script.getAttribute("data-config")); | |
console.log(data); | |
var url = `https://delivery.youplay.se/api/v2/videodata?zone_id=${data.zone_id}&part_id=${data.part_id}`; | |
var xhr = new XMLHttpRequest(); | |
xhr.responseType = "json"; | |
xhr.addEventListener("load", youplay_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