SponsorBlock/ContentScript.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-01-18 20:49:43 +01:00
if(id = youtube_parser(document.URL)){ // Direct Links
SponsorsLookup(id);
}
2019-01-18 20:59:34 +01:00
chrome.runtime.onMessage.addListener( // Detect URL Changes
2019-01-15 19:29:25 +01:00
function(request, sender, sendResponse) {
2019-01-18 21:20:50 +01:00
if (request.message === 'ytvideoid') { // Message from background script
2019-01-15 20:04:10 +01:00
SponsorsLookup(request.id);
2019-01-15 19:29:25 +01:00
}
});
function SponsorsLookup(id) {
2019-01-18 20:59:34 +01:00
v = document.querySelector('video') // Youtube video player
var xmlhttp = new XMLHttpRequest();
2019-01-18 20:59:34 +01:00
xmlhttp.open('GET', 'https://officialnoob.github.io/YTSponsorSkip-Dataset/' + id, true); // Dataset lookup
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
Sponsors = JSON.parse(xmlhttp.responseText);
2019-01-18 20:59:34 +01:00
v.ontimeupdate = function () { // If exists add event to run on the videos "ontimeupdate"
2019-01-15 22:52:18 +01:00
SponsorCheck(Sponsors);
};
}
};
xmlhttp.send(null);
2018-06-23 16:06:18 +02:00
}
2019-01-18 20:49:43 +01:00
function SponsorCheck(Sponsors) { // Video skipping
2019-01-18 20:59:34 +01:00
Sponsors.forEach(function (el, index) { // Foreach Sponsor in video
if ((Math.floor(v.currentTime)) == el[0]) { // Check time has sponsor
v.currentTime = el[1]; // Set new time
}
});
}
2019-01-18 20:49:43 +01:00
2019-01-18 20:59:34 +01:00
function youtube_parser(url) { // Returns with video id else returns false
2019-01-18 20:49:43 +01:00
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}