diff --git a/README.md b/README.md index 70756792..72e4bb2e 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,6 @@ None at the moment # Credit +The awesome [Invidious API](https://github.com/omarroth/invidious/wiki/API) is used to grab the time the video was published. + Some i made by Gregor Cresnar from www.flaticon.com and are licensed by CC 3.0 BY \ No newline at end of file diff --git a/content.js b/content.js index f27120c5..eba49faf 100644 --- a/content.js +++ b/content.js @@ -131,8 +131,21 @@ function sponsorsLookup(id) { v.ontimeupdate = function () { sponsorCheck(sponsorTimes); }; - } else { + } else if (xmlhttp.readyState == 4) { sponsorDataFound = false; + + //check if this video was uploaded recently + //use the invidious api to get the time published + sendRequestToCustomServer('GET', "https://invidio.us/api/v1/videos/" + id, function(xmlhttp, error) { + if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { + let unixTimePublished = JSON.parse(xmlhttp.responseText).published; + + //if less than 3 days old + if ((Date.now() / 1000) - unixTimePublished < 259200) { + setTimeout(() => sponsorsLookup(id), 10000); + } + } + }); } }); } @@ -551,6 +564,25 @@ function sendRequestToServer(type, address, callback) { xmlhttp.send(); } +function sendRequestToCustomServer(type, fullAddress, callback) { + let xmlhttp = new XMLHttpRequest(); + + xmlhttp.open(type, fullAddress, true); + + if (callback != undefined) { + xmlhttp.onreadystatechange = function () { + callback(xmlhttp, false); + }; + + xmlhttp.onerror = function(ev) { + callback(xmlhttp, true); + }; + } + + //submit this request + xmlhttp.send(); +} + function getYouTubeVideoID(url) { // Returns with video id else returns false var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var match = url.match(regExp);