aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAjay Ramachandran <[email protected]>2020-04-19 20:37:30 -0400
committerAjay Ramachandran <[email protected]>2020-04-19 20:37:30 -0400
commit195ed1f5fcd593d237968a644b128b50edbe7e86 (patch)
treefb2b0355c6ee9e141fb792971c0d25d24a4301be
parent1b2bc6def43e60e84cd46ded93bfbd8539ae1d4d (diff)
downloadSponsorBlock-195ed1f5fcd593d237968a644b128b50edbe7e86.tar.gz
SponsorBlock-195ed1f5fcd593d237968a644b128b50edbe7e86.zip
Removed old submission method
-rw-r--r--src/content.ts413
1 files changed, 125 insertions, 288 deletions
diff --git a/src/content.ts b/src/content.ts
index 573dc071..09fca172 100644
--- a/src/content.ts
+++ b/src/content.ts
@@ -593,239 +593,122 @@ function sponsorsLookup(id: string, channelIDPromise?) {
//check database for sponsor times
//made true once a setTimeout has been created to try again after a server error
let recheckStarted = false;
- if (Config.config.testingServer) {
- // Create categories list
- let categories: string[] = [];
- for (const categorySelection of Config.config.categorySelections) {
- categories.push(categorySelection.name);
- }
+ // Create categories list
+ let categories: string[] = [];
+ for (const categorySelection of Config.config.categorySelections) {
+ categories.push(categorySelection.name);
+ }
- utils.asyncRequestToServer('GET', "/api/skipSegments", {
- videoID: id,
- categories
- }).then(async (response: Response) => {
- if (response.status === 200) {
- let recievedSegments: SponsorTime[] = await response.json();
- if (!recievedSegments.length) {
- console.error("[SponsorBlock] Server returned malformed response: " + JSON.stringify(recievedSegments));
- return;
+ utils.asyncRequestToServer('GET', "/api/skipSegments", {
+ videoID: id,
+ categories
+ }).then(async (response: Response) => {
+ if (response.status === 200) {
+ let recievedSegments: SponsorTime[] = await response.json();
+ if (!recievedSegments.length) {
+ console.error("[SponsorBlock] Server returned malformed response: " + JSON.stringify(recievedSegments));
+ return;
+ }
+
+ sponsorDataFound = true;
+
+ // Check if any old submissions should be kept
+ if (sponsorTimes !== null) {
+ for (let i = 0; i < sponsorTimes.length; i++) {
+ if (sponsorTimes[i].UUID === null) {
+ // This is a user submission, keep it
+ recievedSegments.push(sponsorTimes[i]);
+ }
}
+ }
- sponsorDataFound = true;
-
- // Check if any old submissions should be kept
- if (sponsorTimes !== null) {
- for (let i = 0; i < sponsorTimes.length; i++) {
- if (sponsorTimes[i].UUID === null) {
- // This is a user submission, keep it
- recievedSegments.push(sponsorTimes[i]);
- }
+ sponsorTimes = recievedSegments;
+
+ // Remove all submissions smaller than the minimum duration
+ if (Config.config.minDuration !== 0) {
+ let smallSegments: SponsorTime[] = [];
+
+ for (let i = 0; i < sponsorTimes.length; i++) {
+ if (sponsorTimes[i].segment[1] - sponsorTimes[i].segment[0] >= Config.config.minDuration) {
+ smallSegments.push(sponsorTimes[i]);
}
}
- sponsorTimes = recievedSegments;
-
- // Remove all submissions smaller than the minimum duration
- if (Config.config.minDuration !== 0) {
- let smallSegments: SponsorTime[] = [];
-
- for (let i = 0; i < sponsorTimes.length; i++) {
- if (sponsorTimes[i].segment[1] - sponsorTimes[i].segment[0] >= Config.config.minDuration) {
- smallSegments.push(sponsorTimes[i]);
- }
+ sponsorTimes = smallSegments;
+ }
+
+ if (!switchingVideos) {
+ // See if there are any starting sponsors
+ let startingSponsor: number = -1;
+ for (const time of sponsorTimes) {
+ if (time[0] <= video.currentTime && time.segment[0] > startingSponsor && time.segment[1] > video.currentTime) {
+ startingSponsor = time.segment[0];
+ break;
}
-
- sponsorTimes = smallSegments;
}
-
- if (!switchingVideos) {
- // See if there are any starting sponsors
- let startingSponsor: number = -1;
- for (const time of sponsorTimes) {
- if (time[0] <= video.currentTime && time.segment[0] > startingSponsor && time.segment[1] > video.currentTime) {
+ if (!startingSponsor) {
+ for (const time of sponsorTimesSubmitting) {
+ if (time.segment[0] <= video.currentTime && time.segment[0] > startingSponsor && time.segment[1] > video.currentTime) {
startingSponsor = time.segment[0];
break;
}
}
- if (!startingSponsor) {
- for (const time of sponsorTimesSubmitting) {
- if (time.segment[0] <= video.currentTime && time.segment[0] > startingSponsor && time.segment[1] > video.currentTime) {
- startingSponsor = time.segment[0];
- break;
- }
- }
- }
-
- if (startingSponsor !== -1) {
- startSponsorSchedule(false, startingSponsor);
- } else {
- startSponsorSchedule();
- }
}
-
- // Reset skip save
- sponsorSkipped = [];
-
- //update the preview bar
- //leave the type blank for now until categories are added
- if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(video.duration))) {
- //set it now
- //otherwise the listener can handle it
- updatePreviewBar();
+
+ if (startingSponsor !== -1) {
+ startSponsorSchedule(false, startingSponsor);
+ } else {
+ startSponsorSchedule();
}
-
- sponsorLookupRetries = 0;
- } else if (response.status === 404) {
- sponsorDataFound = false;
-
- //check if this video was uploaded recently
- //use the invidious api to get the time published
- sendRequestToCustomServer('GET', "https://www.youtube.com/get_video_info?video_id=" + id, function(xmlhttp, error) {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- let decodedData = decodeURIComponent(xmlhttp.responseText).match(/player_response=([^&]*)/)[1];
-
- if (decodedData === undefined) {
- console.error("[SB] Failed at getting video upload date info from YouTube.");
- return;
- }
+ }
- let dateUploaded = JSON.parse(decodedData).microformat.playerMicroformatRenderer.uploadDate;
+ // Reset skip save
+ sponsorSkipped = [];
- //if less than 3 days old
- if (Date.now() - new Date(dateUploaded).getTime() < 259200000) {
- //TODO lower when server becomes better
- setTimeout(() => sponsorsLookup(id, channelIDPromise), 180000);
- }
- }
- });
+ //update the preview bar
+ //leave the type blank for now until categories are added
+ if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(video.duration))) {
+ //set it now
+ //otherwise the listener can handle it
+ updatePreviewBar();
+ }
- sponsorLookupRetries = 0;
- } else if (sponsorLookupRetries < 90 && !recheckStarted) {
- recheckStarted = true;
+ sponsorLookupRetries = 0;
+ } else if (response.status === 404) {
+ sponsorDataFound = false;
- //TODO lower when server becomes better (back to 1 second)
- //some error occurred, try again in a second
- setTimeout(() => sponsorsLookup(id, channelIDPromise), 10000);
+ //check if this video was uploaded recently
+ //use the invidious api to get the time published
+ sendRequestToCustomServer('GET', "https://www.youtube.com/get_video_info?video_id=" + id, function(xmlhttp, error) {
+ if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
+ let decodedData = decodeURIComponent(xmlhttp.responseText).match(/player_response=([^&]*)/)[1];
- sponsorLookupRetries++;
- }
- });
- } else {
- utils.sendRequestToServer('GET', "/api/getVideoSponsorTimes?videoID=" + id, function(xmlhttp) {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- sponsorDataFound = true;
-
- let recievedSegments: number[][] = JSON.parse(xmlhttp.responseText).sponsorTimes;
- let recievedUUIDs: string[] = JSON.parse(xmlhttp.responseText).UUIDs;
-
- // Check if any old submissions should be kept
- if (sponsorTimes !== null) {
- for (let i = 0; i < sponsorTimes.length; i++) {
- if (sponsorTimes[i].UUID === null) {
- // This is a user submission, keep it
- recievedSegments.push(sponsorTimes[i].segment);
- }
- }
- }
-
- // Create formatted array
- sponsorTimes = [];
- for (let i = 0; i < recievedSegments.length; i++) {
- sponsorTimes.push({
- segment: recievedSegments[i],
- UUID: recievedUUIDs[i],
- // Force sponsor category for now
- //TODO: Remove this
- category: "sponsor"
- });
- }
-
- // Remove all submissions smaller than the minimum duration
- if (Config.config.minDuration !== 0) {
- let smallSegments: SponsorTime[] = [];
-
- for (let i = 0; i < sponsorTimes.length; i++) {
- if (sponsorTimes[i].segment[1] - sponsorTimes[i].segment[0] >= Config.config.minDuration) {
- smallSegments.push(sponsorTimes[i]);
- }
- }
-
- sponsorTimes = smallSegments;
- }
-
- if (!switchingVideos) {
- // See if there are any starting sponsors
- let startingSponsor: number = -1;
- for (const time of sponsorTimes) {
- if (time[0] <= video.currentTime && time.segment[0] > startingSponsor && time.segment[1] > video.currentTime) {
- startingSponsor = time.segment[0];
- break;
- }
- }
- if (!startingSponsor) {
- for (const time of sponsorTimesSubmitting) {
- if (time.segment[0] <= video.currentTime && time.segment[0] > startingSponsor && time.segment[1] > video.currentTime) {
- startingSponsor = time.segment[0];
- break;
- }
- }
+ if (decodedData === undefined) {
+ console.error("[SB] Failed at getting video upload date info from YouTube.");
+ return;
}
-
- if (startingSponsor !== -1) {
- startSponsorSchedule(false, startingSponsor);
- } else {
- startSponsorSchedule();
+
+ let dateUploaded = JSON.parse(decodedData).microformat.playerMicroformatRenderer.uploadDate;
+
+ //if less than 3 days old
+ if (Date.now() - new Date(dateUploaded).getTime() < 259200000) {
+ //TODO lower when server becomes better
+ setTimeout(() => sponsorsLookup(id, channelIDPromise), 180000);
}
}
-
- // Reset skip save
- sponsorSkipped = [];
-
- //update the preview bar
- //leave the type blank for now until categories are added
- if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(video.duration))) {
- //set it now
- //otherwise the listener can handle it
- updatePreviewBar();
- }
-
- sponsorLookupRetries = 0;
- } else if (xmlhttp.readyState == 4 && xmlhttp.status == 404) {
- sponsorDataFound = false;
-
- //check if this video was uploaded recently
- //use the invidious api to get the time published
- sendRequestToCustomServer('GET', "https://www.youtube.com/get_video_info?video_id=" + id, function(xmlhttp, error) {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- let decodedData = decodeURIComponent(xmlhttp.responseText).match(/player_response=([^&]*)/)[1];
-
- if (decodedData === undefined) {
- console.error("[SB] Failed at getting video upload date info from YouTube.");
- return;
- }
-
- let dateUploaded = JSON.parse(decodedData).microformat.playerMicroformatRenderer.uploadDate;
-
- //if less than 3 days old
- if (Date.now() - new Date(dateUploaded).getTime() < 259200000) {
- //TODO lower when server becomes better
- setTimeout(() => sponsorsLookup(id, channelIDPromise), 180000);
- }
- }
- });
-
- sponsorLookupRetries = 0;
- } else if (xmlhttp.readyState == 4 && sponsorLookupRetries < 90 && !recheckStarted) {
- recheckStarted = true;
-
- //TODO lower when server becomes better (back to 1 second)
- //some error occurred, try again in a second
- setTimeout(() => sponsorsLookup(id, channelIDPromise), 10000);
-
- sponsorLookupRetries++;
- }
- });
- }
+ });
+
+ sponsorLookupRetries = 0;
+ } else if (sponsorLookupRetries < 90 && !recheckStarted) {
+ recheckStarted = true;
+
+ //TODO lower when server becomes better (back to 1 second)
+ //some error occurred, try again in a second
+ setTimeout(() => sponsorsLookup(id, channelIDPromise), 10000);
+
+ sponsorLookupRetries++;
+ }
+ });
}
function getYouTubeVideoID(url: string) {
@@ -1577,92 +1460,46 @@ async function sendSubmitMessage(){
}
}
- if (Config.config.testingServer) {
- let response = await utils.asyncRequestToServer("POST", "/api/skipSegments", {
- videoID: sponsorVideoID,
- userID: Config.config.userID,
- segments: sponsorTimesSubmitting
- });
-
- if (response.status === 200) {
- //hide loading message
- let submitButton = document.getElementById("submitButton");
- submitButton.style.animation = "rotate 1s";
- //finish this animation
- //when the animation is over, hide the button
- let animationEndListener = function() {
- changeStartSponsorButton(true, false);
-
- submitButton.style.animation = "none";
+ let response = await utils.asyncRequestToServer("POST", "/api/skipSegments", {
+ videoID: sponsorVideoID,
+ userID: Config.config.userID,
+ segments: sponsorTimesSubmitting
+ });
- submitButton.removeEventListener("animationend", animationEndListener);
- };
+ if (response.status === 200) {
+ //hide loading message
+ let submitButton = document.getElementById("submitButton");
+ submitButton.style.animation = "rotate 1s";
+ //finish this animation
+ //when the animation is over, hide the button
+ let animationEndListener = function() {
+ changeStartSponsorButton(true, false);
- submitButton.addEventListener("animationend", animationEndListener);
+ submitButton.style.animation = "none";
- //clear the sponsor times
- Config.config.sponsorTimes.delete(sponsorVideoID);
+ submitButton.removeEventListener("animationend", animationEndListener);
+ };
- //add submissions to current sponsors list
- if (sponsorTimes === null) sponsorTimes = [];
-
- sponsorTimes = sponsorTimes.concat(sponsorTimesSubmitting);
+ submitButton.addEventListener("animationend", animationEndListener);
- // Empty the submitting times
- sponsorTimesSubmitting = [];
+ //clear the sponsor times
+ Config.config.sponsorTimes.delete(sponsorVideoID);
- updatePreviewBar();
- } else {
- //show that the upload failed
- document.getElementById("submitButton").style.animation = "unset";
- (<HTMLImageElement> document.getElementById("submitImage")).src = chrome.extension.getURL("icons/PlayerUploadFailedIconSponsorBlocker256px.png");
+ //add submissions to current sponsors list
+ if (sponsorTimes === null) sponsorTimes = [];
+
+ sponsorTimes = sponsorTimes.concat(sponsorTimesSubmitting);
- alert(utils.getErrorMessage(response.status) + "\n\n" + (await response.text()));
- }
+ // Empty the submitting times
+ sponsorTimesSubmitting = [];
+ updatePreviewBar();
} else {
- chrome.runtime.sendMessage({
- message: "submitTimes",
- videoID: sponsorVideoID
- }, function(response) {
- if (response != undefined) {
- if (response.statusCode == 200) {
- //hide loading message
- let submitButton = document.getElementById("submitButton");
- submitButton.style.animation = "rotate 1s";
- //finish this animation
- //when the animation is over, hide the button
- let animationEndListener = function() {
- changeStartSponsorButton(true, false);
-
- submitButton.style.animation = "none";
-
- submitButton.removeEventListener("animationend", animationEndListener);
- };
-
- submitButton.addEventListener("animationend", animationEndListener);
-
- //clear the sponsor times
- Config.config.sponsorTimes.delete(sponsorVideoID);
-
- //add submissions to current sponsors list
- if (sponsorTimes === null) sponsorTimes = [];
-
- sponsorTimes = sponsorTimes.concat(sponsorTimesSubmitting);
-
- // Empty the submitting times
- sponsorTimesSubmitting = [];
-
- updatePreviewBar();
- } else {
- //show that the upload failed
- document.getElementById("submitButton").style.animation = "unset";
- (<HTMLImageElement> document.getElementById("submitImage")).src = chrome.extension.getURL("icons/PlayerUploadFailedIconSponsorBlocker256px.png");
-
- alert(utils.getErrorMessage(response.statusCode) + "\n\n" + response.responseText);
- }
- }
- });
+ //show that the upload failed
+ document.getElementById("submitButton").style.animation = "unset";
+ (<HTMLImageElement> document.getElementById("submitImage")).src = chrome.extension.getURL("icons/PlayerUploadFailedIconSponsorBlocker256px.png");
+
+ alert(utils.getErrorMessage(response.status) + "\n\n" + (await response.text()));
}
}