mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-06 19:47:04 +03:00
Added basic time submission code
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
chrome.tabs.onUpdated.addListener( // On tab update
|
chrome.tabs.onUpdated.addListener( // On tab update
|
||||||
function(tabId, changeInfo, tab) {
|
function(tabId, changeInfo, tab) {
|
||||||
if (changeInfo != undefined && changeInfo.url != undefined) {
|
if (changeInfo != undefined && changeInfo.url != undefined) {
|
||||||
console.log(changeInfo)
|
let id = getYouTubeVideoID(changeInfo.url);
|
||||||
let id = youtube_parser(changeInfo.url);
|
|
||||||
if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id
|
if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id
|
||||||
chrome.tabs.sendMessage( tabId, {
|
chrome.tabs.sendMessage( tabId, {
|
||||||
message: 'ytvideoid',
|
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 regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
|
||||||
var match = url.match(regExp);
|
var match = url.match(regExp);
|
||||||
return (match && match[7].length == 11) ? match[7] : false;
|
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();
|
sponsorMessageStarted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.message === 'infoFound') {
|
if (request.message === 'isInfoFound') {
|
||||||
sendResponse({
|
sendResponse({
|
||||||
found: sponsorDataFound
|
found: sponsorDataFound
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.message === 'getVideoID') {
|
||||||
|
sendResponse({
|
||||||
|
videoID: getYouTubeVideoID(document.URL)
|
||||||
|
})
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function sponsorsLookup(id) {
|
function sponsorsLookup(id) {
|
||||||
|
|||||||
@@ -27,6 +27,10 @@
|
|||||||
</b>
|
</b>
|
||||||
|
|
||||||
<button id="clearTimes">Clear Times</button>
|
<button id="clearTimes">Clear Times</button>
|
||||||
|
|
||||||
|
<br/>
|
||||||
|
|
||||||
|
<button id="submitTimes">Submit Times</button>
|
||||||
</center>
|
</center>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
29
popup.js
29
popup.js
@@ -1,5 +1,6 @@
|
|||||||
document.getElementById("sponsorStart").addEventListener("click", sendSponsorStartMessage);
|
document.getElementById("sponsorStart").addEventListener("click", sendSponsorStartMessage);
|
||||||
document.getElementById("clearTimes").addEventListener("click", clearTimes);
|
document.getElementById("clearTimes").addEventListener("click", clearTimes);
|
||||||
|
document.getElementById("submitTimes").addEventListener("click", submitTimes);
|
||||||
|
|
||||||
//if true, the button now selects the end time
|
//if true, the button now selects the end time
|
||||||
var startTimeChosen = false;
|
var startTimeChosen = false;
|
||||||
@@ -26,11 +27,24 @@ chrome.tabs.query({
|
|||||||
}, tabs => {
|
}, tabs => {
|
||||||
chrome.tabs.sendMessage(
|
chrome.tabs.sendMessage(
|
||||||
tabs[0].id,
|
tabs[0].id,
|
||||||
{from: 'popup', message: 'infoFound'},
|
{from: 'popup', message: 'isInfoFound'},
|
||||||
infoFound
|
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) {
|
function infoFound(request) {
|
||||||
//if request is undefined, then the page currently being browsed is not YouTube
|
//if request is undefined, then the page currently being browsed is not YouTube
|
||||||
if (request != undefined) {
|
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() {
|
function sendSponsorStartMessage() {
|
||||||
//the content script will get the message if a YouTube page is open
|
//the content script will get the message if a YouTube page is open
|
||||||
chrome.tabs.query({
|
chrome.tabs.query({
|
||||||
@@ -111,4 +132,10 @@ function clearTimes() {
|
|||||||
chrome.storage.local.set({"videoTimes": videoTimes});
|
chrome.storage.local.set({"videoTimes": videoTimes});
|
||||||
|
|
||||||
displayVideoTimes();
|
displayVideoTimes();
|
||||||
|
}
|
||||||
|
|
||||||
|
function submitTimes() {
|
||||||
|
chrome.runtime.sendMessage({
|
||||||
|
message: "submitTimes"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user