diff --git a/background.js b/background.js index 20ed74c3..2f839742 100644 --- a/background.js +++ b/background.js @@ -1,8 +1,7 @@ chrome.tabs.onUpdated.addListener( // On tab update function(tabId, changeInfo, tab) { if (changeInfo != undefined && changeInfo.url != undefined) { - console.log(changeInfo) - let id = youtube_parser(changeInfo.url); + let id = getYouTubeVideoID(changeInfo.url); if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id chrome.tabs.sendMessage( tabId, { message: 'ytvideoid', @@ -13,8 +12,35 @@ chrome.tabs.onUpdated.addListener( // On tab update } ); -function youtube_parser(url) { // Return video id or false +function getYouTubeVideoID(url) { // Return video id or false var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; var match = url.match(regExp); return (match && match[7].length == 11) ? match[7] : false; +} + +chrome.runtime.onMessage.addListener(function (request, sender, callback) { + console.log(request.message) + if (request.message == "submitTimes") { + submitTimes(request.videoID); + } +}); + +function submitTimes(videoID) { + //get the video times from storage + chrome.storage.local.get(['videoTimes' + videoID], function(result) { + if (result.videoTimes != undefined && result.videoTimes != []) { + let videoTimes = result.videoTimes; + + //TODO: remove this, just temp + let videoID = "TEST"; + + //submit these times + for (let i = 0; i < videoTimes.length; i++) { + let xmlhttp = new XMLHttpRequest(); + //submit the sponsorTime + xmlhttp.open('GET', 'http://localhost/api/postVideoSponsorTimes?videoID=' + videoID + "&startTime=" + videoTimes[i][0] + "&endTime=" + videoTimes[i][1], true); + xmlhttp.send(); + } + } + }); } \ No newline at end of file diff --git a/content.js b/content.js index 0d3951b3..361a10e5 100644 --- a/content.js +++ b/content.js @@ -22,11 +22,17 @@ chrome.runtime.onMessage.addListener( // Detect URL Changes sponsorMessageStarted(); } - if (request.message === 'infoFound') { + if (request.message === 'isInfoFound') { sendResponse({ found: sponsorDataFound }) } + + if (request.message === 'getVideoID') { + sendResponse({ + videoID: getYouTubeVideoID(document.URL) + }) + } }); function sponsorsLookup(id) { diff --git a/popup.html b/popup.html index d082acdf..c04c9415 100644 --- a/popup.html +++ b/popup.html @@ -27,6 +27,10 @@ + +
+ + diff --git a/popup.js b/popup.js index 58126584..b8984578 100644 --- a/popup.js +++ b/popup.js @@ -1,5 +1,6 @@ document.getElementById("sponsorStart").addEventListener("click", sendSponsorStartMessage); document.getElementById("clearTimes").addEventListener("click", clearTimes); +document.getElementById("submitTimes").addEventListener("click", submitTimes); //if true, the button now selects the end time var startTimeChosen = false; @@ -26,11 +27,24 @@ chrome.tabs.query({ }, tabs => { chrome.tabs.sendMessage( tabs[0].id, - {from: 'popup', message: 'infoFound'}, + {from: 'popup', message: 'isInfoFound'}, infoFound ); }) +// //get the tab's video ID +// var videoID = undefined; +// chrome.tabs.query({ +// active: true, +// currentWindow: true +// }, tabs => { +// chrome.tabs.sendMessage( +// tabs[0].id, +// {from: 'popup', message: 'getVideoID'}, +// setVideoID +// ); +// }) + function infoFound(request) { //if request is undefined, then the page currently being browsed is not YouTube if (request != undefined) { @@ -42,6 +56,13 @@ function infoFound(request) { } } +function setVideoID(request) { + //if request is undefined, then the page currently being browsed is not YouTube + if (request != undefined) { + videoID = request.videoID; + } +} + function sendSponsorStartMessage() { //the content script will get the message if a YouTube page is open chrome.tabs.query({ @@ -111,4 +132,10 @@ function clearTimes() { chrome.storage.local.set({"videoTimes": videoTimes}); displayVideoTimes(); +} + +function submitTimes() { + chrome.runtime.sendMessage({ + message: "submitTimes" + }); } \ No newline at end of file