mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-06 11:37:02 +03:00
Added basic time submission code
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -27,6 +27,10 @@
|
||||
</b>
|
||||
|
||||
<button id="clearTimes">Clear Times</button>
|
||||
|
||||
<br/>
|
||||
|
||||
<button id="submitTimes">Submit Times</button>
|
||||
</center>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
29
popup.js
29
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"
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user