mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-12 18:04:20 +01:00
Made everything now use the proper per video local storage
This commit is contained in:
parent
3869049088
commit
7baff32b55
3 changed files with 55 additions and 45 deletions
|
@ -12,28 +12,24 @@ chrome.tabs.onUpdated.addListener( // On tab update
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
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) {
|
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
||||||
console.log(request.message)
|
console.log(request.message)
|
||||||
if (request.message == "submitTimes") {
|
if (request.message == "submitTimes") {
|
||||||
submitTimes(request.videoID);
|
submitTimes(request.videoID);
|
||||||
|
|
||||||
|
callback({
|
||||||
|
success: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function submitTimes(videoID) {
|
function submitTimes(videoID) {
|
||||||
//get the video times from storage
|
//get the video times from storage
|
||||||
chrome.storage.local.get(['videoTimes' + videoID], function(result) {
|
let sponsorTimeKey = 'videoTimes' + videoID;
|
||||||
if (result.videoTimes != undefined && result.videoTimes != []) {
|
chrome.storage.local.get([sponsorTimeKey], function(result) {
|
||||||
let videoTimes = result.videoTimes;
|
let videoTimes = result[sponsorTimeKey];
|
||||||
|
|
||||||
//TODO: remove this, just temp
|
if (videoTimes != undefined && result.videoTimes != []) {
|
||||||
let videoID = "TEST";
|
|
||||||
|
|
||||||
//submit these times
|
//submit these times
|
||||||
for (let i = 0; i < videoTimes.length; i++) {
|
for (let i = 0; i < videoTimes.length; i++) {
|
||||||
let xmlhttp = new XMLHttpRequest();
|
let xmlhttp = new XMLHttpRequest();
|
||||||
|
@ -43,4 +39,10 @@ function submitTimes(videoID) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
|
@ -44,14 +44,14 @@ function sponsorsLookup(id) {
|
||||||
|
|
||||||
xmlhttp.onreadystatechange = function () {
|
xmlhttp.onreadystatechange = function () {
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
sponsorDataFound = true;
|
|
||||||
|
|
||||||
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
sponsorTimes = JSON.parse(xmlhttp.responseText).sponsorTimes;
|
||||||
|
|
||||||
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
// If the sponsor data exists, add the event to run on the videos "ontimeupdate"
|
||||||
v.ontimeupdate = function () {
|
v.ontimeupdate = function () {
|
||||||
sponsorCheck(sponsorTimes);
|
sponsorCheck(sponsorTimes);
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
sponsorDataFound = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xmlhttp.send(null);
|
xmlhttp.send(null);
|
||||||
|
|
70
popup.js
70
popup.js
|
@ -7,43 +7,41 @@ var startTimeChosen = false;
|
||||||
|
|
||||||
//the start and end time pairs (2d)
|
//the start and end time pairs (2d)
|
||||||
var videoTimes = [];
|
var videoTimes = [];
|
||||||
//load video times
|
|
||||||
chrome.storage.local.get(['videoTimes'], function(result) {
|
|
||||||
if (result.videoTimes != undefined && result.videoTimes != []) {
|
|
||||||
videoTimes = result.videoTimes;
|
|
||||||
|
|
||||||
if (videoTimes[videoTimes.length - 1]!= undefined && videoTimes[videoTimes.length - 1].length < 2) {
|
//current video ID of this tab
|
||||||
startTimeChosen = true;
|
var currentVideoID = null;
|
||||||
}
|
|
||||||
|
|
||||||
displayVideoTimes();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//check if this video's sponsors are known
|
|
||||||
chrome.tabs.query({
|
chrome.tabs.query({
|
||||||
active: true,
|
active: true,
|
||||||
currentWindow: true
|
currentWindow: true
|
||||||
}, tabs => {
|
}, loadTabData);
|
||||||
|
|
||||||
|
function loadTabData(tabs) {
|
||||||
|
//set current videoID
|
||||||
|
currentVideoID = getYouTubeVideoID(tabs[0].url);
|
||||||
|
|
||||||
|
//load video times for this video
|
||||||
|
let videoTimeKey = "videoTimes" + currentVideoID;
|
||||||
|
chrome.storage.local.get([videoTimeKey], function(result) {
|
||||||
|
videoTimes = result[videoTimeKey];
|
||||||
|
if (videoTimes != undefined && result.videoTimes != []) {
|
||||||
|
if (videoTimes[videoTimes.length - 1]!= undefined && videoTimes[videoTimes.length - 1].length < 2) {
|
||||||
|
startTimeChosen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
displayVideoTimes();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//check if this video's sponsors are known
|
||||||
chrome.tabs.sendMessage(
|
chrome.tabs.sendMessage(
|
||||||
tabs[0].id,
|
tabs[0].id,
|
||||||
{from: 'popup', message: 'isInfoFound'},
|
{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
|
||||||
|
@ -88,7 +86,8 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
||||||
|
|
||||||
videoTimes[videoTimesIndex][startTimeChosen ? 1 : 0] = request.time;
|
videoTimes[videoTimesIndex][startTimeChosen ? 1 : 0] = request.time;
|
||||||
|
|
||||||
chrome.storage.local.set({"videoTimes": videoTimes});
|
let videoTimeKey = "videoTimes" + currentVideoID;
|
||||||
|
chrome.storage.local.set({[videoTimeKey]: videoTimes});
|
||||||
|
|
||||||
//update startTimeChosen variable
|
//update startTimeChosen variable
|
||||||
if (!startTimeChosen) {
|
if (!startTimeChosen) {
|
||||||
|
@ -110,7 +109,6 @@ function displayVideoTimes() {
|
||||||
document.getElementById("sponsorMessageTimes").innerHTML = "";
|
document.getElementById("sponsorMessageTimes").innerHTML = "";
|
||||||
|
|
||||||
for (let i = 0; i < videoTimes.length; i++) {
|
for (let i = 0; i < videoTimes.length; i++) {
|
||||||
console.log(videoTimes)
|
|
||||||
for (let s = 0; s < videoTimes[i].length; s++) {
|
for (let s = 0; s < videoTimes[i].length; s++) {
|
||||||
let timeMessage = videoTimes[i][s] + "s";
|
let timeMessage = videoTimes[i][s] + "s";
|
||||||
//if this is an end time
|
//if this is an end time
|
||||||
|
@ -129,13 +127,23 @@ function displayVideoTimes() {
|
||||||
function clearTimes() {
|
function clearTimes() {
|
||||||
videoTimes = [];
|
videoTimes = [];
|
||||||
|
|
||||||
chrome.storage.local.set({"videoTimes": videoTimes});
|
let videoTimeKey = "videoTimes" + currentVideoID;
|
||||||
|
chrome.storage.local.set({[videoTimeKey]: videoTimes});
|
||||||
|
|
||||||
displayVideoTimes();
|
displayVideoTimes();
|
||||||
}
|
}
|
||||||
|
|
||||||
function submitTimes() {
|
function submitTimes() {
|
||||||
chrome.runtime.sendMessage({
|
chrome.runtime.sendMessage({
|
||||||
message: "submitTimes"
|
message: "submitTimes",
|
||||||
|
videoID: currentVideoID
|
||||||
|
}, function(request) {
|
||||||
|
clearTimes();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
Loading…
Reference in a new issue